# SubCommands

SubCommands are a way to have custom behavior when the first argument (or multiple here) is a determined string. Each commands can have SubCommands and each SubCommand should be able to have SubCommands. Here it's done differently, the name of a SubCommand can include spaces, it will slice the arguments correctly.&#x20;

## Defining SubCommands

SubCommands can be defined for commands, to register a SubCommand you have to overwrite the `registerSubCommands` method of your command class then use the `subCommand` method of the `Command` class.

Example :

{% code title="MyCommand.js" %}

```javascript
class MyCommand extends Command {
    name = 'test';
    
    registerSubCommands() {
        this.subCommand('name', (ctx) => {
            ctx.send('subCommand');
        });
    }
    
    async run(ctx) {
        ctx.send('normal command');
    }
}
```

{% endcode %}

{% hint style="info" %}
You can use `CommandContext#isCallingASubCommand` to know if you are calling a SubCommand, because calling a SubCommand will still execute the `run` method.
{% endhint %}

### Passing options to your SubCommand

You can set the same options of a command to your SubCommands by passing an object before the callback :

```javascript
this.subCommand('name', 
    {
        aliases: ['other'],
        description: 'something',
        tags: ['guildOnly']
    },
    (ctx) => {
        // Code goes here     
    }
});
```

{% hint style="info" %}
Note that your SubCommands and SubCommands aliases can contains spaces in them, it will work.
{% endhint %}

{% hint style="warning" %}
If you don't put a description for your SubCommand, it will not be shown into the default help command.
{% endhint %}

## [Documentation](https://advanced-command-handler.github.io/docs/classes/Command.html#subCommand)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ayfri.gitbook.io/advanced-command-handler/concepts/commands/subcommands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
