Search code examples
pythondiscorddiscord.pyundefined

'Check is not defined' in client.wait_for in discord.py


I want to read the message sent by the author after giving the command =hi to my discord bot. So I used this code I found online to get what the author sent:

msg = await client.wait_for('message', check=check) #error goes here, 'check' is not defined
await ctx.send(msg.content)

but the problem is the check is not defined when this function is run


Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'check' is not defined

How do I fix it? I haven't defined check in front this function, but how do I define check?


Solution

  • Check is this:

    def check(m):
        return m.author == ctx.author and m.channel == ctx.channel
    

    Just add this inside your command function and above

    msg = await client.wait_for('message', check=check)

    So:

    def check(m):
        return m.author == ctx.author and m.channel == ctx.channel
    
    msg = await client.wait_for('message', check=check)
    await ctx.send(msg.content)