【问题标题】:Discord.py allowance systemDiscord.py 津贴系统
【发布时间】:2020-06-08 04:36:04
【问题描述】:
我已经在我的机器人中实施了银行系统。例如,我想每周给用户一次津贴。
有没有办法使用 discord.py 来做到这一点,或者我需要时间库或其他东西吗?
【问题讨论】:
标签:
python
bots
discord.py
【解决方案1】:
您可以使用 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 创建错误处理程序。
参考资料: