【问题标题】:How to add cooldown between messages for discord.py bot如何在 discord.py bot 的消息之间添加冷却时间
【发布时间】:2021-10-14 03:06:18
【问题描述】:

以下代码工作正常,但我想添加一个等待时间,以便代码在设定的时间段内只工作一次然后重置。

例如,如果有人发消息“你好”,我希望机器人回复。但是,如果有人在 20 秒后发送“你好”消息,我希望机器人在给定时间(比如 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)) 

【问题讨论】:

  • 用你最后一次触发的时间戳存储一个变量,并检查最后一个是否超过 2 分钟前
  • 如果您使用命令很容易,因为您可以在这里从一个命令到另一个命令的时间,您需要听我上面的人的意见,只需使用日期时间或任何带有时间戳的东西

标签: python discord discord.py


【解决方案1】:

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

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

应该没问题。

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2018-10-21
    • 2021-05-02
    相关资源
    最近更新 更多