Your First Command

Introduction#

The smallest Discapp command looks like this:

The most basic command
import { Command, BaseCommand } from "discapp";
@Command()
export class PingCommand extends BaseCommand {
public execute() {
return "pong";
}
}

This is the !ping command, the only thing it does is to send 'pong' whenever the user sents 'ping'.

It's useful to verify if your bot is working and comes by default in a Discapp project.

Concepts#

Location#

All the commands must be in path/to/your/app/Commands directory if you have setup your project with create-discapp.

This is where Discapp will look for commands, commands in this location are automatically loaded.

Code#

Every Discapp command has a "code". Think of a command's code as a name, in fact the code is basically a command name.

Every command must have an unique code, so it can be distinguished from other commands.

In Discapp, the code of a command can be either implicitly defined, for example:

Explicitly defined code
@Command("ping")
export class PingCommand extends BaseCommand {
// ...
}

In this case, the code of the command is ping as defined in @Command('ping').

A command code can also be implicit, as in the code in introduction. In this case the code is also ping, because if you name your class using the Discapp convention, Discapp will automatically infer the code:

  • PingCommand -> ping
  • HelpCommand -> help
  • ShowMeCommand -> show-me

Discapp basically removes the 'Command' suffix and convert the remaining to kebab-case.

The execute method#

Every command must have a public execute method, this is the method that will get invoked when the user sends a message with the code.

Returns of the execute method will be automatically send as replies by Discapp.

The execute method can also be async.