将冷却时间添加到命令[不和谐.py]

2024-04-19 16:45:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近开始学习python,不知道如何对命令进行冷却。我需要用户每12小时获得一次硬币。如果时间还没有过去,我需要显示剩余的时间。你知道吗

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)

Tags: importinfojsondatasavedefaswith
1条回答
网友
1楼 · 发布于 2024-04-19 16:45:18

您需要在命令上方使用此行: @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)是以秒为单位的间隔命令.buckettype.user定义每个用户的限制。你知道吗

相关问题 更多 >