Chadi Chaddi
Chadi Chaddi

Reputation: 25

Discord.py allowance system

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

Answers (1)

Diggy.
Diggy.

Reputation: 6944

You're able to set a cooldown for commands using the commands.cooldown decorator.

It takes three args:

  • The number of uses
  • Over what period of time the uses are valid for before resetting
  • Whether it's on a cooldown per user/guild/etc.

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:

Upvotes: 1

Related Questions