Skip to main content

Obtaining Command Arguments

Suppose, we need to find out how old the user is. Requesting arguments is available right out of the box in the module, all you have to do is specify which arguments are required.

Setting up command arguments

All setup occurs in the CommandHandler. To specify that we want to request the user's age, let's change the /start command code:

/src/commands/start.ts
import { Command, CommandHandler, MessageContext, ICommandExecuteData } from "evogram"

@CommandHandler({
name: "start",
args: {
// Argument acceptance method.
method: "stdin",
// In the array, specify which arguments we need.
// First, the argument name is passed, this is mandatory, and then the parameters to this argument (question is used with the stdin method), they are optional.
value: [["age", { question: "How old are you?" }]]
}
})
class StartCommand extends Command {
execute(message: MessageContext, data: ICommandExecuteData) {
if(!Number(data.args?.age)) return message.send("I didn't understand how old you are, please try again!");
message.send(Number(data.args.age) >= 18 ? `You are already an adult!` : `You are still a minor :(`);
}
}
info

You can read more about command arguments here