命令冷却忽略用户

时间:2018-06-12 04:38:33

标签: python python-3.x discord discord.py

我添加了一个命令冷却,但是如何让它仅适用于选定的用户。

@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)

1 个答案:

答案 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,这是完成任务比处理错误更简单的方法。