【问题标题】:What's a method for catching and responding to timeouts from discord.py wait_for_messages?从 discord.py wait_for_messages 捕获和响应超时的方法是什么?
【发布时间】:2019-09-25 18:55:42
【问题描述】:

在我的 discord bot 中,我正在处理一个命令,您可以在其中执行类似 !command 的操作,然后该 bot 回复“执行命令?[y/n]”并听您说“y”,然后执行某物。我正在尝试向此函数添加超时,但我无法捕捉到此超时何时发生并触发事件,例如当超时发生时,机器人说“确认超时”。并继续。

我已经尝试过elif userinput is None:elif userinput is '':,两者都没有触发。 我在 Raspbian 拉伸上使用 discord.py 0.16.12 和 Python 3.6.1。

这是我目前拥有的:

msgconfirm = await client.say(f"[{context.message.author.mention}] Confirm? [y/n]")
userinput = await client.wait_for_message(timeout=5, author=context.message.author)
if userinput.clean_content.lower() == 'y':
    do something
elif userinput is None:
    do something
else:
    do something

目前,当超时触发时,它只是忽略我发送的下一条消息,但不会触发elif userinput is None:。我正在寻找它来触发该块,所以我可以添加类似await client.say(f"[{context.message.author.mention}] Confirmation timed out.

我已经尝试使用this 页面上的答案,但它们都不起作用。该页面上的代码格式也不正确。

【问题讨论】:

    标签: python-3.x discord discord.py


    【解决方案1】:

    在移动了一些东西之后,我发现如果你只是将if userinput == None:`` at the top of the if section, and move theif userinput,clean_content.lower() == 'y':` 放到当前的 elif 行,一切正常。工作示例:

    if userinput is None:
        do something
    elif userinput.clean_content.lower() == 'y':
        do something
    else:
        do something
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      相关资源
      最近更新 更多