Multiple word command in discord.py (commands extension)

Viewed 298

I am using the discord.ext.commands module for my Discord bot and I'd like to have a multiple word command called "setup prefix".

When I'm using the command, it throws an CommandNotFound error, which says that "setup" isn't a command. So it seems like discord.py is only checking for the first word.

Is there a way to fix that?

This is a snippet of my code:

@commands.command(name="setup prefix")
async def set_prefix(self, ctx: Context, prefix: str):
    pass

And yeah I know, I could take "prefix" as an additional argument. But then I have to do all setup commands using one function.

I would appreciate your help :)

2 Answers

You probably want to use command groups


@commands.group(invoke_without_subcommand=True)
async def setup(self, ctx, *args):
    # general functionality, help, or whatever.
    pass

@setup.command()
async def prefix(self, ctx, prefix):
    #logic
    pass

Please check the docs for more info and examples

https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=commands%20group#discord.ext.commands.group

Also read the library FAQ it has some examples for this use case. https://discordpy.readthedocs.io/en/latest/faq.html

Note: forgive ugly links I'm on mobile, I'll edit them as references when i get to the computer

Just writing up my comment as a proposal. I don't think you can make a multi-name command. Probably by design, as I think it would be a security risk and add a lot of processing. On the backend, each message would have to know in advance how many parts there were to the command.

I propose you do

And yeah I know, I could take "prefix" as an additional argument.

But not

But then I have to do all setup commands using one function.

Which would be something like this:

# all of this is in the same class whatever that's called. 

async def handle_setup_giraffe(self, ctx: Context):
    pass

@commands.command(name="setup")
async def setup(self, ctx: Context):
    setup_command = ctx.args[0]
    if hasattr(self, f"handle_setup_{setup_command}"):
        return getattr(self, f"handle_setup_{setup_command}")(ctx)
    else:
        raise SomeException("Setup command not found", setup_command)
Related