Tanuj KS
Tanuj KS

Reputation: 165

TypeError with check() on discord.py wait_for

I have a bot that will open an close polls which uses the wait_for(reaction_add) feature. The problem is, when I'm checking whether the author made the reaction i get a TypeError. Here is the command:

@bot.command()
async def closepoll(ctx):
    if not ctx.author.guild_permissions.mute_members:
        await ctx.send("You cannot use this")
    else:
        await ctx.send("React to the poll I must close")
        def check(m):
            return m.author == ctx.author
        try:
            reaction, user = await bot.wait_for('reaction_add', timeout=120.0, check=check)
        except asyncio.TimeoutError:
            await ctx.send('Did not receive any reaction')
        else:
            message = reaction.message
            if message.content.startswith("Poll:") and str(user) == str(ctx.author):
                await message.edit(content="This poll is now closed.")
            else:
                await ctx.send("That is not a poll")

But I get the error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "hypixel.py", line 138, in closepoll
    except asyncio.TimeoutError:
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py", line 483, in wait_for
    return fut.result()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 339, in dispatch
    result = condition(*args)
TypeError: check() takes 1 positional argument but 2 were given

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/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: TypeError: check() takes 1 positional argument but 2 were given

Upvotes: 0

Views: 324

Answers (1)

Kevin
Kevin

Reputation: 76184

The number of arguments that check should take varies depending on what event you're waiting for. The reaction_add event takes two arguments, but your function only has one.

Try defining a check function with two arguments instead. For example, the documentation gives the example:

def check(reaction, user):
    return user == message.author and str(reaction.emoji) == '👍'

Upvotes: 3

Related Questions