Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for dynamic cooldowns #1646

Merged
merged 4 commits into from
Sep 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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