Message Command Options
Message commands can have options as positional arguments.
You can use .addOption to add an option to a message command. This provides a similar syntax to SlashCommandBuilder
class ExampleCommand extends MessageCommandModule {
data = new MessageCommandBuilder()
.setName('example')
.setDescription('An example command')
.addOption(option => option
.setName('option')
.setDescription('An option')
.setRequired(true)
);
async execute({ message, options }) {
const option = options.getOptionValue('option', true);
await message.reply(`Option value: ${option.value}`);
}
}
export default new ExampleCommand(); To access the options value, reciple provides an options object which is an instance of MessageCommandOptionValueManager.
To access the unresolved value (or the string value) of the option, you can use getOptionValue.
const optionValue = options.getOptionValue('option', true); The second parameter is whether the option is required or not. If the option is optional, the value will be
nullif the option is not provided.
Resolved Values
By default all options can only be a string, you can add a resolvers to the option using setResolve method of MessageCommandOptionBuilder.
class ExampleCommand extends MessageCommandModule {
data = new MessageCommandBuilder()
.setName('example')
.setDescription('An example command')
.addOption(option => option
.setName('option')
.setDescription('An option')
.setResolve(data => Number(data.value))
.setRequired(true)
);
async execute({ message, options }) {
const number = await options.getOptionResolvedValue('option', true);
await message.reply(`Option value: ${number}`);
}
}
export default new ExampleCommand(); To access the resolved value, you can use getOptionResolvedValue method. This method will resolve the value of the option asynchronously based on the resolver function.
Validation
You can add a validation function to an option using setValidate method of MessageCommandOptionBuilder. The value that is passed to the function is an unresolved value of the option.
class ExampleCommand extends MessageCommandModule {
data = new MessageCommandBuilder()
.setName('example')
.setDescription('An example command')
.addOption(option => option
.setName('option')
.setDescription('An option')
.setResolve(data => Number(data.value))
.setValidate(data => isNaN(Number(data.value)) ? false : true)
.setRequired(true)
);
async execute({ message, options }) {
const number = await options.getOptionValue('option', true);
await message.reply(`Option value: ${number}`);
}
}
export default new ExampleCommand(); The validate function should return a boolean value. If the value is valid, it should return
true, otherwise it should returnfalse.
The validation of message command options is done before the command is executed, the precondition MessageCommandOptionValidatePrecondition is the default precondition for validating message command options.
Invalid Options
If the option is invalid, the command will not be executed. Postconditions will be executed with an execute data in the structure of CommandPostcondition.InvalidArgsExecuteData.
By default, when an option is invalid, the command will simply not be executed without any feedback to the user. The postconditions will be responsible for handling the invalid option.
Missing Options
If the option is required but not provided, the command will not be executed. This will also trigger the postconditions with an execute data in the structure of CommandPostcondition.MissingArgsExecuteData.
Similar to invalid options, the postconditions will be responsible for handling the missing option.