Command Configuration
The CommandHandler
decorator is used to declare a command, add it to the command handler and specify its parameters. It uses overloading, allowing you to pass a string as an argument that will be the name of the command for activation or an object with parameters.
Simple Configuration
@CommandHandler("example")
class ExampleCommand extends Command {}
This example is the simplest - we created the example
command. Now, the bot will refer to this class if the user writes /example
.
Advanced Configuration
@CommandHandler({
name: "example",
args: {
method: "stdin",
value: [
["argument1", { question: "Enter your first argument:" }],
["argument2", { question: "Enter your second argument:" }]
]
},
description: [
{ text: "It's a test command", language: "en" },
{ text: "Это тестовая команда", language: "ru" },
]
})
class ExampleCommand extends Command {}
Here are all the possible command settings. The command, like in the previous example, responds to /example
, but with additional functions.
Interface Parameters Accepted by the Decorator
interface ICommandParams {
// The name of the command the bot will react to
name: string;
// Arguments required for executing the command
args?: ICommandArguments;
// You can specify a description of the command in different languages, which will be sent to Telegram for installation on the list of commands
description?: ICommandDescription[];
}
interface ICommandDescription {
// A command description (1-256 characters)
text: string;
// Two-letter ISO 639-1 language code. If empty, the command will apply to all users in the area for whom there are no special commands.
language?: string;
}
type ICommandArgumentType = "parameterized" | "space" | "stdin" | "fulltext";
interface ICommandArguments {
/**
* Specifies how arguments will be passed to the command
*
* @param parameterized - Command arguments are passed as --key=value
* @example
* User: /command --param1=value --param2=value
*
* @param space - Command arguments are passed as values separated by spaces
* @example
* User: /command value1 value2
*
* @param stdin - Command arguments are requested after the command is called, they are accepted as separate messages which are requested by the bot
* @example
* User: /command
* Bot: What value do you want to specify?
* User: value
*
* @param fulltext - Accepts only one argument and returns the entire text of the message
* @example
* User: /command here's a lot of text that is fully transmitted as one argument
*/
method: ICommandArgumentType | ICommandArgumentType[];
/**
* @type {string[]} An array consisting of the enumeration of argument names to get
* @type {[string, { question?: string }][]} An array of arrays, where the argument name is passed first, then its additional parameters
*
* @param question The message text to request an argument. If the text is not specified, the standard message will be sent
*/
value: string[] | [string, {
question?: string
}][];
}