【问题标题】:How to wait some seconds only for a user discord bot python如何仅为用户不和谐机器人 python 等待几秒钟
【发布时间】:2021-05-10 18:25:38
【问题描述】:

好的,我正在 python 中制作货币机器人。我对python很流利(只是没有用过那么多协程)

我想要实现的是,如果用户在前 20 秒内发送了消息,机器人不会响应用户。 time.sleep 在这里不起作用,因为它会阻止所有代码(其他用户应该得到回复)。我只是不知道该怎么做。

async def on_message(message):
    if message.author==client.user:
        return
    await make_bank(message.author.id)
    if message.content=="A":
        await message.channel.send("Done! Wait 20 secs.")

【问题讨论】:

  • 请添加您的代码,也可以使用time.sleep代替asyncio.sleep
  • 我的代码中有这个。让我添加它

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


【解决方案1】:

您可以使用 wait_for 函数和超时 kwarg

if message.content == "A":
    await message.channel.send("Done! Wait 20 secs.")

    def check(m):
        return m.author == message.author

    try:
        await client.wait_for("message", check=check, timeout=20.0)
        # Code here will be executed if the user sends a message within those 20 seconds
        await message.channel.send("Uh oh, you were supposed to wait for 20 seconds")
    except asyncio.TimeoutError:
        # User didn't send a message in 20 seconds
        await message.channel.send("Thanks for waiting!")

参考:

【讨论】:

  • 它一直在发送“哦哦,你应该等 20 秒”
  • 我尝试了代码,它可以完美运行。你做错了什么
  • 我昨天正在阅读它并做了一些更改,但现在有 1 个问题。它不响应第一条消息并等待 20 秒然后响应
  • 我不太明白这里有什么问题
  • 我基本上想做一个像mee6这样的练级系统(如果用户留言,XP每分钟添加一次)我怎样才能做到这一点?
猜你喜欢
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 2021-11-15
  • 2021-10-30
  • 2021-05-27
  • 2021-07-15
  • 1970-01-01
相关资源
最近更新 更多