I want to make a command for daily rewards for discord.py economy. I have the code but the problem is is that it displays in secounds
@client.command(aliases=['d'])
@commands.cooldown(1, 86400, commands.BucketType.user)
@is_registered
async def daily(ctx: commands.Context):
random_amount = random.randint(50, 150)
await economy.add_money(ctx.message.author.id, "wallet", random_amount)
embed = discord.Embed(
colour=discord.Color.from_rgb(244, 182, 89)
)
embed.add_field(name=f"Reward", value=f"Successfully claimed daily!")
await ctx.send(embed=embed)
I checked the documention in https://discordpy.readthedocs.io/en/stable/ on how to use cooldown but there does not seem to be a way to make it do days instead of seconds. Here is what happens when you run the code:
You are on cooldown. Try again in 86396.55s.
error image If any of you know how to turn this to 1 day it would be greatly appreciated. Thank you. P.S. If it helps this is the library I am using for the economy system:
https://pypi.org/project/DiscordEconomy/
>Solution :
You can define an error handler. It will fire whenever there is a cooldown error, which is discord.CommandOnCooldown:
@client.event
async def on_command_error(ctx, err):
if err.__class__ is commands.CommandOnCooldown:
cd: int = int(err.retry_after)
# send an error message, you can customize this
await ctx.send(f'Sorry, you are on cooldown, which ends in **{cd//86400}d {(cd//3600)%24}h {(cd//60)%60}m {cd % 60}s**.')
return
# more error handling...
# You should probably uncomment this
#await ctx.send(err)
This works without changing anything to the standard cooldown handler, so you can use a command like this:
@client.command(name='cooldown')
@commands.cooldown(1, 100000.0, commands.BucketType.user)
async def cooldown_command(ctx):
await ctx.send('Success!')
Example output message (you can customize what is displayed):
