如何等待一些秒只为用户不和bot python



好了,我正在用python制作一个货币机器人。我对python很熟练(只是没怎么用协程)

我想要实现的是,如果用户在前20秒内发送了消息,bot不会响应用户。时间。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.")

您可以使用带有timeout kwarg的wait_for函数

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!")
参考:

  • Client.wait_for

最新更新