【问题标题】:'Check is not defined' in client.wait_for in discord.pydiscord.py 中的 client.wait_for 中的“检查未定义”
【发布时间】:2021-11-24 13:45:24
【问题描述】:

我想在给我的 discord bot 发出命令=hi 后阅读作者发送的消息。所以我用我在网上找到的这段代码来获取作者发送的内容:

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

但问题是check在这个函数运行时没有定义


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

我该如何解决? 这个函数前面我没有定义check,但是check怎么定义呢?

【问题讨论】:

  • 你在这行之前定义了检查吗?
  • 您好,可以出示您的代码吗?
  • @MoeedAzhar 不,但是如何?
  • 这个函数前面没有定义check,怎么定义check?

标签: python discord discord.py undefined


【解决方案1】:

检查是这样的:

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

只需将其添加到您的命令函数中及上方

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

所以:

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)

【讨论】:

  • 是的,这个答案对我有用,谢谢。
  • 没问题很高兴帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 2019-09-21
  • 2021-07-15
  • 1970-01-01
  • 2017-04-05
  • 2013-02-06
相关资源
最近更新 更多