好的,我正在用 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.")
答案 0 :(得分:0)
您可以使用带有超时 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!")