Reputation: 25
I've implemented a banking system in my bot. I want to give an allowance to users once a week, for example.
Is there a way to do this using discord.py, or would I need the time library or something else?
Upvotes: 1
Views: 208
Reputation: 6944
You're able to set a cooldown for commands using the commands.cooldown
decorator.
It takes three args:
Here's an example for a command that's allowed to be executed once every 24 hours:
from discord.ext import commands
# this decorator is saying 1 command execution per user per day (time counted in seconds)
@commands.cooldown(1, 86400, commands.BucketType.user)
@bot.command()
async def daily(ctx):
# do something
@daily.error
async def daily_err(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(error) # tell the user when they can next use the command
else:
print(error)
The error decorator is simply down to preference - you can create an error handler with on_command_error
if you'd rather.
References:
commands.cooldown()
Command.error
on_command_error()
Commands.CommandOnCooldown()
- The exception that's thrown if the user tries to run the command more than they're allowed.Upvotes: 1