3
votes
@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.

2

2 Answers

21
votes

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

1
votes

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.)