akul07
akul07

Reputation: 27

How to add disable command in discord.py

I want that when user uses the command disable {command_name} . the command gets diable i dont know how to do this please help me out. please share with a example

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

if i want the the hello command to be disabled then ......

Upvotes: 0

Views: 890

Answers (1)

Bagle
Bagle

Reputation: 2346

You can disable a command by using command.update. You can disable it by doing command.update(enable=False). Further explanations will be in the code provided below.

Please note: Next time you ask a question like this, provide what you have tried beforehand, most people will not entertain you with providing the full code.

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

Command working as seen here

(PS: The last error message was something programmed into my bot beforehand, you may want to have an error-handler)

Upvotes: 1

Related Questions