-2

I wanted to set a cooldown for my bot in discord.py, I tried to do this with time.sleep(30), but it didn't work because it stops the full bot, but I need that you can do other commands in the time. Please in content.split and not discord ext. Can someone help me?

Nurqm
  • 4,052
  • 2
  • 6
  • 30

3 Answers3

1

What you want is to use a decorator on your command. The decorator for a cooldown is @commands.cooldown

@bot.command()
@commands.cooldown(x, y, commands.BucketType.z)
#your code here
...

x is how many times a command can be used in a given timeframe

y is that timeframe (in seconds)

z is who this applies to (a user, a channel, a server, or even global(default))

So: ...cooldown(2, 10, commands.BucketType.user) will allow every user to use the command 2 times within 10 seconds.

Good luck!

You can read up on it here: Discord.py commands.cooldown

As a sidenote: time.sleep() is something you can use to "pause" the bot within a command

print("hi")
time.sleep(30)
print("there")

This will put a delay between the two messages, instead of having them be printed immediately, one after the other. You should test this out in!

AbdurJ
  • 780
  • 2
  • 2
  • 13
1

I think you can use asyncio for this:

import asyncio 

asyncio.sleep(seconds)

asyncio.sleep works like time.sleep, but time.sleep is blocking the entire code and asyncio.sleep stops only one event. You can read more about the difference between time.sleep and asyncio.sleep here.

RiveN
  • 338
  • 10
0

this is because time.sleep is blocking, you can use

@commands.cooldown(1, 5, commands.BucketType.user)