forked from Pycord-Development/pycord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a cooldown example (Pycord-Development#677)
- Loading branch information
1 parent
73fbb2d
commit 7d1fb46
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |