Arguments

Introduction#

In the previous chapter we learn how to code the most basic Discapp command.

In fact, by only reading the Hello World command you can already code a lot of commands, maybe most of the commands you'll need.

In this chapter we will learn how to handle arguments.

Defining arguments#

Sometimes, our commands needs additional context to be executed. For example, think of the command greet that will send Hello, {{ name }}.

But for doing this, our command needs to know the name of the user he will greet. This is the additional context we talking about.

In this case we need an argument that is the name of the user.

Writing GreetCommand#

path/to/your/app/Commands/GreetCommand.ts
import { Command, BaseCommand, Argument } from "discapp";
@Command()
export class GreetCommand extends BaseCommand {
@Argument()
public name: string;
public execute() {
return `Hello ${this.name}`;
}
}
info

Arguments are required by default.

See the Optional Arguments for non required arguments.

Points to note from this code:

  • Commands are declared by defining properties in your command class and marking them with @Argument that is coming from Discapp.
  • Command can have multiple arguments.
  • Argument may have other types that aren't string, you can learn more in the advanced Arguments examples.