'Displaying Cooldown In Seconds in On_Message in Discord.py

EDIT: I solved it, I just simply looped the asyncio.sleep and set a variable for the cooldown in seconds. :)

I'm new to discord.py and I just started developing a bot.

Most bots like Dank Memer has cooldowns after the on_message events happens. (I don't know if Dank Memer is in discord.py or not)

So I want to do the same, but I do not know how to display the cooldown in seconds. (Before you can enter another on_message event)

This is part of my code so far: (This is the cooldown)

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)

This code works, it doesn't show how many seconds you have before you can enter another command again.

My code is actually pretty long, so I don't want to rewrite it. Is there a way to display how many seconds you have got left if the user enters the same command within the cooldown?



Solution 1:[1]

Ok, I solve it myself.

At the start you do:

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

Then, after the events from the 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)

Put this line of code at the start of the on_message function:

global cooldowntime

Then, at the start of a message event happens:

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!')

It should work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ender Dangered