Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cooldown For Command On Discord Bot Python

@client.command(pass_context = True)
async def getalt(ctx):
    msg = ["[email protected]:Cyber123", "[email protected]:culillo123", "[email protected]:Albakortoci1", "[email protected]:toysale22", "[email protected]:nich918273645", "[email protected]:Lodelode1", "[email protected]:emolover123", "[email protected]:rube541632789mk", "[email protected]:fryckman22", "[email protected]:blackout541", "[email protected]:ploopy101"]
    await client.send_message(ctx.message.author, random.choice(msg))
    await client.send_message(ctx.message.channel, "Alt Has Been Seen To Your DMs")
    await client.purge_from(ctx.message.channel, limit=2)
    await client.send_message(ctx.message.author, "Please Wait 30 Seconds Before Using This Command Again.")

I want to set a 30 sec cooldown for this command.

like image 1000
Jacob W. Avatar asked Sep 07 '17 03:09

Jacob W.


People also ask

How do you set a cooldown on discord?

To get this set up, navigate to your Channel Settings by clicking on the cog icon to Edit Channel > Overview. You'll see the Slowmode option along with a slider where you can adjust the time intervals.

How do you make a discord.py bot wait?

python discord bot wait for response # Use Client. wait_for to wait for on_message event. await ctx. send("Say hello!")


2 Answers

You should decorate your command with

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

This will add a ratelimit of 1 use per 30 seconds per user. docs, example

You can change the BucketType to default, channel or server to create a global, channel or server ratelimit instead, but you can only have 1 cooldown on a command.

Note: In discord.py rewrite (v1.0+) instead of BucketType.server, you have to use BucketType.guild.

This will also cause a CommandOnCooldown exception in on_command_error

like image 131
Sam Rockett Avatar answered Sep 30 '22 16:09

Sam Rockett


I know a method of sending in the channel that cooldown is in process.

@command_name.error
    async def command_name_error(ctx, error):
        if isinstance(error, commands.CommandOnCooldown):
            em = discord.Embed(title=f"Slow it down bro!",description=f"Try again in {error.retry_after:.2f}s.", color=color_code_here)
            await ctx.send(embed=em)

make sure you have imported bucket type. If not -

from discord.ext.commands import cooldown, BucketType

NOTE - Make sure the command cooldown event always has a different name and has to be the_command_name_here.error (don't make it the_command_name_here , insert the ACTUAL command name there.)

like image 43
TigerNinja Avatar answered Sep 30 '22 18:09

TigerNinja