给命令添加冷却时间[Discord.py] [英] Add cooldown to command[Discord.py]

查看:63
本文介绍了给命令添加冷却时间[Discord.py]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习python,不了解如何对命令进行冷却。我需要用户每12小时获取硬币。如果时间还没有过去,我需要显示剩余时间。

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)


推荐答案

您需要我们在命令上方的以下行:
@ commands.cooldown(1,43200,命令。BucketType.user)

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

所以您会得到:

@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)

其中1是可以调用的次数每个间隔命令,43200(60 * 60 * 12)是间隔,以秒为单位,commands.buckettype.user定义限制是每个用户的。

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.

这篇关于给命令添加冷却时间[Discord.py]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆