【问题标题】:Python discord bot, wait for message is not working as expectedPython discord bot,等待消息未按预期工作
【发布时间】:2021-08-20 16:15:35
【问题描述】:

我正在 discord.py 上发出命令。命令正在从不和谐服务器的管理员用户那里获取一些参数。当您调用命令时,它会从每个渠道获取每个用户消息。我只想做,从调用者用户和他写的文本频道获取参数。同时,错误代码不起作用。当有人尝试调用不是管理员的命令时,它不会发送消息。但如果用户不是管理员,但我想通过 bot 向他们发送消息,则用户无法使用此命令。

这是我的命令,

@Bot.command(pass_context=True)
@has_permissions(administrator=True)
async def ekle(ctx):
    
  
    await ctx.channel.send("send 4 parametres")
    
    msg = await Bot.wait_for('message',(ctx.author))
    response = (msg.content)
    bos = response.split(' ')
@ekle.error
async def ekle_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "you cant use this command ! {}".format(ctx.message.author.mention)  
        await Bot.send_message(ctx.message.channel, msg)

代码不工作。控制台没有给出任何错误,但代码不和谐。 我该如何解决这个问题?

【问题讨论】:

  • 试试MissingPermissions 而不是CheckFailure
  • 另外,pass_context=True 在更高版本的 discordpy 中是多余的。默认为 True。
  • 你能把MissingPermissions 的任何例子发给我吗? 这不是一个问题。也有一个。
  • 您的wait_for 电话中没有check。请看docs
  • HereMissingPermissions 的示例

标签: python discord discord.py


【解决方案1】:

bot.wait_for 采用一个必需参数(事件)和两个可选关键字参数(检查和超时)。

您正在传递第二个位置参数,该参数不起作用。正确的用法是,

msg = await Bot.wait_for('message', check=lambda x: x.author.id == ctx.author.id)

参考资料:

【讨论】:

  • 您需要指定check 参数。这是一个关键字参数(后跟asterick)
  • 我的错,和OP一样的错误
  • :/我一开始没注意到...我认为check是一个必需的参数,而不是可选的...每个人都会犯错误:P
  • 它给出了这个错误:msg = await Bot.wait_for('message', lambda x: x.author.id == ctx.author.id) TypeError: wait_for() 需要 2位置参数,但给出了 3 个
猜你喜欢
  • 2019-05-25
  • 2018-06-05
  • 2021-03-31
  • 2019-04-19
  • 2021-08-13
  • 2019-07-31
  • 2020-03-04
  • 2013-12-10
  • 2019-04-07
相关资源
最近更新 更多