【问题标题】:How to add disable command in discord.py如何在 discord.py 中添加禁用命令
【发布时间】:2021-06-30 14:22:38
【问题描述】:

我希望当用户使用命令时禁用 {command_name} 。该命令变得可行,我不知道该怎么做,请帮帮我。 请分享一个例子

@bot.command()
async def hello(ctx):
       await ctx.send("hi")

如果我想禁用 hello 命令,那么......

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您可以使用command.update 禁用命令。您可以通过 command.update(enable=False) 禁用它。进一步的解释将在下面提供的代码中。

    请注意:下次您提出这样的问题时,提供您事先尝试过的内容,大​​多数人不会通过提供完整代码来取悦您。

    @bot.command()
    async def disable(ctx, command_name=None):
        if command_name == None: 
            # comes here if no command is given
            await ctx.send("Please give me a command you want to disable!")
            return
    
        try:
            # the bot will try to get the command
            command = bot.get_command(command_name)
            # then the bot will try to disable it
            command.update(enabled=False)
            # command.update(enabled=True) allows you to enable the command
        except:
            # this gets sent if the bot can't: a) get the command or b) disable the command
            await ctx.send("That isn't a valid command!")
            return
    
        # finally, if the command gets successfully disabled, it sends this message
        await ctx.send(f"I have disabled the command {command_name}")
    

    (PS:最后一条错误消息是我的机器人预先编程的,你可能想要一个错误处理程序)

    【讨论】:

    • 如果这有助于您回答您的问题,请单击此答案的赞成/反对票下方的勾号以接受它@akul07 ?
    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2018-11-07
    • 2021-07-06
    • 1970-01-01
    • 2021-07-27
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多