不协调津贴制度

2024-06-25 23:34:00 发布

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

我已经在我的机器人中实现了一个银行系统。例如,我想每周给用户一次津贴

有没有办法使用discord.py实现这一点,或者我需要时间库或其他什么


Tags: 用户py系统时间机器人银行津贴discord
1条回答
网友
1楼 · 发布于 2024-06-25 23:34:00

您可以使用commands.cooldown装饰器设置命令的冷却时间

它需要三个参数:

  • 使用次数
  • 重置前,使用有效期为多长时间
  • 是否对每个用户/公会/等进行冷却

下面是一个允许每24小时执行一次的命令示例:

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)

错误装饰器只取决于首选项-如果愿意,可以使用on_command_error创建一个错误处理程序


参考文献:

  • ^{}
  • ^{}
  • ^{}
  • ^{}-如果用户尝试运行命令的次数超过允许的次数,则引发的异常

相关问题 更多 >