繁体   English   中英

Discord.py 如何使用 discord.ext.commands 禁用命令

[英]Discord.py how to disable commands with discord.ext.commands

我想禁用一个命令,但我不知道它是如何工作的

这是我要禁用的命令

@client.command(description="Sends an random gif", aliases=['gifje', 'GIF', 'Gif'], brief="Sends an random gif (NO NSFW)")
discord.ext.commands.Command(name="gif", cls=None, enabled=False)
#@commands.cooldown(1, 10, commands.BucketType.user)
async def gif(ctx):
    links = ["https://gph.is/1N1s5AR",
             "https://gph.is/2nmNhuw",
             "https://gph.is/g/ajWp6mj",
             "https://gph.is/g/apbGw0O",
             "https://gph.is/g/Z5YMP9Q",
             "https://gph.is/g/aQOvqQ5",
             "https://gph.is/g/ajW9Nx8",
             "https://gph.is/2CF8W7r",
             "http://gph.is/17GL4ua",
             "https://gph.is/12kQg0y"]
    await ctx.send(random.choice(links))

我相信我必须用 discord.ext.commands 来做,但我不知道如何(我不想使用齿轮)

.update(enabled=False)

您可以通过Bot.commands找到您要查找的命令来获取Command object。 找到正确的命令 object 后,您可以使用其update方法来编辑enabled的属性。

将此属性设置为False将导致在调用命令时引发DisabledCommand错误,您可以根据需要进行处理。

client.remove_command(name)

您可以创建一个运行此命令的命令,并且在重新添加或重新启动机器人之前它将不可用。

https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.remove_command

    @commands.command(name="toggle", description="Enable or disable a command!")
    @commands.is_owner()
    async def toggle(self, ctx, *, command):
        command = self.client.get_command(command)

        if command is None:
            embed = discord.Embed(title="ERROR", description="I can't find a command with that name!", color=0xff0000)
            await ctx.send(embed=embed)

        elif ctx.command == command:
            embed = discord.Embed(title="ERROR", description="You cannot disable this command.", color=0xff0000)
            await ctx.send(embed=embed)

        else:
            command.enabled = not command.enabled
            ternary = "enabled" if command.enabled else "disabled"
            embed = discord.Embed(title="Toggle", description=f"I have {ternary} {command.qualified_name} for you!", color=0xff00c8)
            await ctx.send(embed=embed)

这是我用来禁用/启用命令的最简单方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM