import datetime
from discord.ext import commands
on_cooldown = {} # A dictionary mapping user IDs to cooldown ends
def cooldown(seconds):
def predicate(context):
if (cooldown_end := on_cooldown.get(context.author.id)) is None or cooldown_end < datetime.datetime.now(): # If there's no cooldown or it's over
if context.valid and context.invoked_with in (*context.command.aliases, context.command.name): # If the command is being run as itself (not by help, which runs checks and would end up creating more cooldowns if this didn't exist)
on_cooldown[context.author.id] = datetime.datetime.now() + datetime.timedelta(seconds=seconds) # Add the datetime of the cooldown's end to the dictionary
return True # And allow the command to run
else:
raise commands.CommandOnCooldown(commands.BucketType.user, (cooldown_end - datetime.datetime.now()).seconds) # Otherwise, raise the cooldown error to say the command is on cooldown
return commands.check(predicate)
您可以使用custom check来确保这些命令在冷却时不被使用。
./q63262849/cooldowns.py
然后,可以导入该文件
./main.py
并用作命令的装饰器
./main.py
值得注意的是,这种方法仍然存在一些问题,特别是如果以后的另一次检查失败,此检查仍将使命令处于冷却状态,但是可以通过将此检查置于所有其他检查之后运行来解决这些问题
相关问题 更多 >
编程相关推荐