Commands

Sub-Zero (Sub Commands)

We've covered the basics of creating a command, but what if we want to create a command that has subcommands? For example, we want to create a command with a create subcommand and a delete subcommand. This is where subcommands come into play.

Creating a Subcommand

To create a subcommand, we need to create a new class that extends SubCommand and implement the run method. Then, we need to add the subcommand to the main command.

Let's assume you have the following directory structure:

src
commands
index.ts
seyfert.config.mjs
package.json
tsconfig.json

With this file structure, you can use @Autoload() and Seyfert will automatically add the commands for you. But make sure to export the subcommands by default.

import { Declare, Command, Options } from "seyfert";
import { CreateCommand } from "./create.command";
import { DeleteCommand } from "./delete.command";
 
@Declare({
	name: "account",
	description: "account command"
})
// Being in the same folder with @AutoLoad() you can save this step
@Options([CreateCommand, DeleteCommand])
export default class AccountCommand extends Command {}

In the example above, we created a create subcommand and a delete subcommand. Then, we added the subcommands to the main command using the @Options decorator.

Note that we essentially treat the subcommand as an option of the main command. (In fact, this is how Discord handles subcommands too)

Subcommand Groups

For a more divided structure, you can create groups for subcommands using the @Group and @Groups decorators in the main command.

import { , , ,  } from 'seyfert';
import  from './sub';
 
@({
    : 'parent',
    : 'My main command',
})
@([])
@({
    'my-group': {
        : 'A subcommand group',
    }
})
export default class  extends  {}

On this page