【问题标题】:discord.py / message with timeoutdiscord.py / 超时消息
【发布时间】:2022-01-11 07:00:17
【问题描述】:

我正在尝试构建一个不和谐的机器人,它应该等待用户输入 5 秒。如果给出了输入,它应该回复“准时”,如果没有给出输入,它应该说“太晚了”。

我的代码的问题在于,即使给出输入,我也总是以“为时已晚”而告终。 5 秒后它总是发送“为时已晚”。有谁知道我做错了什么? :) 感谢您的帮助!

try:
    msg = await bot.wait_for("msg",timeout=5)
    if msg:
        await ctx.send("on time!")
except asyncio.TimeoutError:
    await ctx.send("Too late!")

【问题讨论】:

  • 没有像 msg 这样的事件,你正在寻找 message.. 不知道你从哪里得到它,在文档中很清楚。
  • 您还需要考虑添加check 参数以将接收到的消息限制为仅来自命令作者的消息。

标签: python discord discord.py python-asyncio


【解决方案1】:

基于 Lukasz 和 metro 的 cmets,您的代码应如下所示:

try:
    msg = await bot.wait_for("message", check=lambda message: message.author == ctx.author, timeout=5)
    if msg:
        await ctx.send("on time!")
except asyncio.TimeoutError:
    await ctx.send("Too late!")

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2018-06-29
    • 1970-01-01
    • 2021-09-14
    • 2021-05-17
    • 2021-07-08
    • 2017-06-23
    • 2021-03-17
    • 2021-07-18
    相关资源
    最近更新 更多