我添加了一个命令冷却,但是如何让它仅适用于选定的用户。
@bot.command(pass_context=True)
@commands.cooldown(1, 30, commands.BucketType.user)
async def ping(ctx):
msg = "Pong {0.author.mention}".format(ctx.message)
await bot.say(msg)
答案 0 :(得分:1)
没有官方支持的方法来执行此操作,但您可以定义自己的Command
子类来处理此问题,因为在异步分支中,prepare()
方法中会检查冷却时间:
admin_ids = ['1234567', '234567876543', '123454321']
class CommandWithCooldown(commands.Command):
async def prepare(self, ctx):
try:
return await super().prepare(ctx)
except commands.CommandOnCooldown as e:
if ctx.message.author.id in admin_ids:
return
else:
raise e
@bot.command(pass_context=True , cls=CommandWithCooldown)
@commands.cooldown(1, 30, commands.BucketType.user)
async def ping(ctx):
msg = "Pong {0.author.mention}".format(ctx.message)
await bot.say(msg)
IMO,这是完成任务比处理错误更简单的方法。