【问题标题】:discord bot (python) how to use wait_for to wait for a message or a reactiondiscord bot (python) 如何使用 wait_for 等待消息或反应
【发布时间】:2021-08-13 19:22:06
【问题描述】:

我正在为我的 discord 机器人制作一种琐事命令,用户会得到一个缺少字母的随机单词,他们必须猜测该单词是什么才能获胜。

不过,我还希望在机器人的琐事消息上放置一个反应,用户可以按下它来立即显示单词是什么,这样他们就可以输入它并获胜。

我基本上在寻找的是一种使用 wait_for 协程等待反应的方法,该反应将显示单词和提示获胜者的消息。 (反应的使用是可选的,因此并不总是保证在仅仅猜测单词时使用它。

这是我目前所拥有的。


        wordChosen = random.choice(randomWords)

        def check(m): # Checks if the user entered the correct word.
            if m.content.lower() != wordChosen:
                return False
            elif m.channel != channel:
                return False
            else: # User met the requirements.
                return True


        try:
            msg = await self.client.wait_for(
                "message",
                timeout=60,
                check=check
            )
        except:
            asyncio.TimeoutError
            await wordMessage.edit(content=f"You guys took too long!. The word was {wordChosen}.")
        else:
            if msg:
               await msg.channel.send(f"{msg.author.mention} guessed the word!")

【问题讨论】:

  • 你能仔细检查你的缩进并正确格式化它们吗?而且,您希望它同时等待消息或反应?
  • 是的,差不多
  • 您的 except 子句将不起作用并返回错误顺便说一句
  • 确实有效,我之前试过了
  • 在您向我们展示的代码中,它应该返回一个错误......(asyncio.TimeoutError 应该在: 后面)

标签: python discord discord.py


【解决方案1】:

except 部分之后,您应该添加asyncio.TimeoutError。而不是把它放在: 之后。

意思是:

except asyncio.TimeoutError:
            await wordMessage.edit(content=f"You guys took too long!. The word was {wordChosen}.")

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 2019-07-10
    • 2021-08-20
    • 1970-01-01
    • 2019-06-26
    • 2021-12-07
    • 2021-06-20
    • 1970-01-01
    相关资源
    最近更新 更多