在 discord.py wait_for 上带有 check() 的 TypeError

Posted

技术标签:

【中文标题】在 discord.py wait_for 上带有 check() 的 TypeError【英文标题】:TypeError with check() on discord.py wait_for 【发布时间】:2021-02-06 19:33:55 【问题描述】:

我有一个机器人,它将打开一个使用 wait_for(reaction_add) 功能的关闭民意调查。问题是,当我检查作者是否做出反应时,我得到了一个 TypeError。这是命令:

@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")

但我得到了错误:

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

【问题讨论】:

【参考方案1】:

check 应采用的参数数量取决于您等待的事件。 reaction_add 事件有两个参数,但你的函数只有一个。

尝试用两个参数定义一个检查函数。比如the documentation给出的例子:

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

【讨论】:

是的,谢谢,我不应该使用def check(m),而是应该使用def check(reaction, user)

以上是关于在 discord.py wait_for 上带有 check() 的 TypeError的主要内容,如果未能解决你的问题,请参考以下文章

Discord.py-rewrite wait_for() 我该如何使用?

discord.py 如何使用 wait_for 等待作者消息?

discord.py wait_for message... 如何取消息内容和写消息的人?

discord.py wait_for('reaction_add') 与直接消息的功能不同

Discord.py 按钮交互消息删除

Discord py 将在一个频道上发送的消息(带有嵌入)转发到另一个频道以及如何确定服务器中的成员数量?