1

I started learning python recently and don’t understand how to make a cooldown for a command.I need the user to get coins every 12 hours. If the time has not passed yet, I need to display the remaining time.

import datetime
import json

def save_data(users):
    with open('files/users_info.json', 'w') as f:
        json.dump(users, f)

async def add_money(users, user, money):
    users[str(user.id)]['money'] += money

@commands.command(name='daily')
    async def daily(self, ctx):
        with open('files/users_info.json', 'r') as f:
            users = json.load(f)
        # timer...
        #
        # if time >= 43200 (seconds)
        #     await add_money(users, ctx.author, 1000)
        #     await ctx.send('Gave 1000 coins')
        # else:
        #     hours...
        #     min...
        #     sec...
        #     await ctx.send(f'Left {hours}, {min}, {sec}')

        save_data(users)
ddd_ht
  • 113
  • 3
  • See this answer for an example of how to display the remaining time: https://stackoverflow.com/questions/52298850/discord-py-bot-getting-cooldown-time-remaining-of-command – Patrick Haugh Nov 06 '19 at 20:41
  • Is it possible to display the time in hours, minutes and seconds? – ddd_ht Nov 07 '19 at 18:09
  • Sure: https://stackoverflow.com/questions/4048651/python-function-to-convert-seconds-into-minutes-hours-and-days – Patrick Haugh Nov 07 '19 at 18:13

1 Answers1

0

You need to use this line above the command: @commands.cooldown(1, 43200, commands.BucketType.user).

So you get:

@commands.cooldown(1, 43200, commands.BucketType.user)
@commands.command(name='daily')
    async def daily(self, ctx):
        with open('files/users_info.json', 'r') as f:
            users = json.load(f)

Where 1 is the amount of times you can call the command per interval, 43200 (60*60*12) is the interval in seconds and commands.buckettype.user defines that the restriction is per user.

Miguel
  • 1,295
  • 12
  • 25