Skip to content

Commit b27fdb0

Browse files
Add example for dynamic cooldowns (#1646)
Co-authored-by: Dorukyum <[email protected]>
1 parent 5026759 commit b27fdb0

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

examples/cooldown.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
intents.message_content = True
88

99
bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"), debug_guilds=[...], intents=intents)
10-
10+
bypassing_users = [] # used in the dynamic cooldown below
1111

1212
# An application command with cooldown
1313
@bot.slash_command()
@@ -32,6 +32,22 @@ async def prefixed(ctx: commands.Context):
3232
await ctx.send("You can use this command again in 5 seconds.")
3333

3434

35+
# Dynamic cooldown function; allows for custom cooldown logic such as different cooldowns per-user
36+
def my_cooldown(message):
37+
if message.author.id in bypassing_users:
38+
return None # Let specific users bypass the cooldown entirely
39+
elif message.author.get_role(929080208148017242):
40+
return commands.Cooldown(2, 5) # Users with the above role ID can use the command twice in 5 seconds
41+
else:
42+
return commands.Cooldown(1, 10) # All other users can use the command once in 10 seconds
43+
44+
# A prefixed command with the dynamic cooldown defined above
45+
@bot.command()
46+
@commands.dynamic_cooldown(my_cooldown, commands.BucketType.user)
47+
async def dynamic(ctx: commands.Context):
48+
await ctx.send("You can use this command again soon.")
49+
50+
3551
# Prefixed command error handler
3652
@bot.event
3753
async def on_command_error(ctx: commands.Context, error: commands.CommandError):

0 commit comments

Comments
 (0)