Skip to content

Commit

Permalink
Add a cooldown example (Pycord-Development#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorukyum authored and VincentRPS committed Jan 22, 2022
1 parent 73fbb2d commit 7d1fb46
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/cooldown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import discord
from discord.ext import commands


bot = commands.Bot()


# an application command with cooldown
@bot.slash_command()
@commands.cooldown(1, 5, commands.BucketType.user) # the command can only be used once in 5 seconds
async def slash(ctx):
await ctx.respond("You can't use this command again in 5 seconds.")


# error handler
@bot.event
async def on_application_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.respond("This command is currently on cooldown.")
else:
raise error # raise other errors so they aren't ignored


# a prefixed command with cooldown
@bot.command()
@commands.cooldown(1, 5, commands.BucketType.user)
async def prefixed(ctx):
await ctx.send("You can't use this command again in 5 seconds.")


@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send("This command is currently on cooldown.")
else:
raise error

0 comments on commit 7d1fb46

Please sign in to comment.