在 Discord.py 中 On_Message 中以秒为单位显示冷却时间

Displaying Cooldown In Seconds in On_Message in Discord.py

编辑: 我解决了它,我只是简单地循环 asyncio.sleep 并设置一个冷却时间的变量(以秒为单位)。 :)

我是 discord.py 的新手,我刚开始开发一个机器人。

大多数像 Dank Memer 这样的机器人在 on_message 事件发生后都有冷却时间。 (不知道Dank Memer在不在discord.py)

所以我也想这样做,但我不知道如何以秒为单位显示冷却时间。 (在您可以输入另一个 on_message 事件之前)

到目前为止,这是我的代码的一部分:(这是冷却时间)

import discord,asyncio #and some other modules
cooldown = []

async def on_message(message):
  # Some Code

  cooldown.append(message.author.id)
  await asyncio.sleep(60)
  cooldown.remove(message.author.id)

此代码有效,它不会显示您有多少秒可以再次输入另一个命令。

我的代码其实很长,不想重写。 如果用户在冷却时间内输入相同的命令,有没有办法显示你还剩多少秒?

好的,我自己解决

一开始你做的:

cooldown = []
cooldownSec = 60 # How many seconds
cooldowntime = 0

然后,在 on_message 的事件之后:

cooldown.append(message.author.id)
for i in range(cooldownSec,-1,-1):
  if i == 0:
    cooldown.remove(message.author.id)
    break
  cooldowntime = i
  asyncio.sleep(1)

将这行代码放在 on_message 函数的开头:

global cooldowntime

然后,在消息事件开始时发生:

if message.content.lower().startswith('!test'):
  if message.author.id in cooldown:
      await message.reply(f'You have to wait for {cooldowntime} more seconds before you can use the commands again!')

应该可以。