Setting arguments in command
Evogram provides a convenient tool for retrieving the necessary data from the user for a command. At the moment, there are 4 built-in types of argument acceptance which we will explore. Let's also look at how to specify command arguments.
To set command arguments, you need to specify args in CommandHandler, which is responsible for command arguments. Let's take a look at an example:
@CommandHandler({
name: "example",
args: {
// Argument acceptance method. Required parameter
method: "parameterized",
// A list of arguments to be obtained. Data is passed with the same name as specified. Required parameter
value: ["argument1", "argument2"]
}
})
class ExampleCommand extends Command {
execute(message: MessageContext, data: ICommandExecuteData) {
message.send(`You specified the following parameters:\n- argument1: ${data.args.value1}\n- argument2: ${data.args.value2}`);
}
}
We have set a request for two arguments (value1, value2) in our command, as well as specified the argument request method: parameterized. When executing a command with specified arguments, the bot will send us back what we specified.
The requested data can be obtained in the second argument of the execute method using the args key. All received arguments from the user are included in the object.
execute(message: MessageContext, data: ICommandExecuteData) {
console.log(data.args["argumentName"]);
}
Multiple argument acceptance methods can be specified by specifying an array instead of a string with a list of the required methods.
@CommandHandler({
name: "example",
args: {
method: ["parameterized", "stdin"],
value: ["argument1", "argument2"]
}
})
Argument Acceptance Methods
There are 4 argument acceptance methods, each of which we will now consider.
Parameterized
This method allows you to accept arguments in the format --param=value.
- User: /example --argument1=value1 --argument2=value2
- Bot: You have provided the following parameters:
- argument1: value1
- argument2: value2
Space
With this method, you can get arguments that are listed with spaces.
- User: /example value1 value2
- Bot: You have provided the following parameters:
- argument1: value1
- argument2: value2
FullText
The simplest method of accepting an argument. It only accepts one argument, which is the entire string that comes after the command.
- User: /example here goes the text, which is one argument
- Bot: You have provided the following parameters:
- argument1: here goes the text, which is one argument
STDIN
When the user enters a command, they do not need to specify certain arguments after the name. Your bot itself will ask for all the necessary arguments according to the list and then execute the commands.
- User: /example
- Bot: The "argument1" parameter is required for the command to work. Send the value in the following message
- User: value1
- Bot: The "argument2" parameter is required for the command to work. Send the value in the following message
- User: value2
- Bot: You have provided the following parameters:
- argument1: value1
- argument2: value2
Setting Your Argument Request Message
You can set your own text message by passing an array instead of a string, consisting of the argument name and its additional parameters.
@CommandHandler({
name: "example",
args: {
method: "stdin",
value: [["argument1", { question: "What value would you like to pass?" }]]
}
})
At the moment, you can only specify question, which changes the argument request text.
With this setting, the command call will look like this:
- User: /example
- Bot: What value would you like to pass?
- User: value1
- Bot: You have provided the following parameters:
- argument1: value1