Skip to content

Commit

Permalink
Add example for dynamic cooldowns (Pycord-Development#1646)
Browse files Browse the repository at this point in the history
Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
  • Loading branch information
NeloBlivion and Dorukyum authored Sep 23, 2022
1 parent 5026759 commit b27fdb0
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion examples/cooldown.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
intents.message_content = True

bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"), debug_guilds=[...], intents=intents)

bypassing_users = [] # used in the dynamic cooldown below

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


# Dynamic cooldown function; allows for custom cooldown logic such as different cooldowns per-user
def my_cooldown(message):
if message.author.id in bypassing_users:
return None # Let specific users bypass the cooldown entirely
elif message.author.get_role(929080208148017242):
return commands.Cooldown(2, 5) # Users with the above role ID can use the command twice in 5 seconds
else:
return commands.Cooldown(1, 10) # All other users can use the command once in 10 seconds

# A prefixed command with the dynamic cooldown defined above
@bot.command()
@commands.dynamic_cooldown(my_cooldown, commands.BucketType.user)
async def dynamic(ctx: commands.Context):
await ctx.send("You can use this command again soon.")


# Prefixed command error handler
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
Expand Down

0 comments on commit b27fdb0

Please sign in to comment.