如何为discord.py bot添加消息之间的冷却时间



下面的代码工作得很好,但是我想添加一个等待时间,以便代码在一段时间内只工作一次,然后重置。

例如,如果有人发送"你好",我希望机器人回复。然而,如果有人发送"你好"信息,20秒后,我想让机器人忽略这条消息和任何其他的"hello";给定时间(例如2分钟)的消息。

@bot.event
async def on_message(message):
if bot.user == message.author:
return
if any(word in message.content.lower() for word in hello_message):
await message.channel.send(random.choice(hello_response)) 

添加一个冷却装饰符。应该看起来像这样:

@commands.cooldown(1, 120, commands.BucketType.guild)# the 120 is just 2 minutes, like you said.

应该没问题。