Message Command Options
Reciple provides a built-in message command parser that can be used to parse command options and arguments from a message.
Simple String Option
import { MessageCommandBuilder } from 'reciple';
new MessageCommandBuilder()
.setName('test')
.setDescription('A test command')
.addOption(option => option
.setName('option')
.setDescription('A test option')
.setRequired(false)
)
.setExecute(async ({ message, options }) => {
const option = options.getOptionValue('option');
await message.reply(`Received: **${option ?? 'Nothing'}**`);
}) Command Usage
Validate & Resolve Command Option Values
You make your own option value validator and Resolver by using MessageCommandOptionBuilder#setValidator() and MessageCommandOptionBuilder#setresolvevalue()
import { MessageCommandBuilder } from 'reciple';
new MessageCommandBuilder()
.setName('test')
.setDescription('A test command')
.addOption(option => option
.setName('option')
.setDescription('A test option')
.setRequired(false)
.setResolveValue(({ value }) => Number(value))
.setValidator(({ value }) => !isNaN(Number(value)))
)
.setExecute(async ({ message, options }) => {
const option = await options.getOptionValue('option', { resolveValue: true });
await message.reply(`Received: **${option ?? 'Nothing'}**`);
}) INFO
When using
setResolveValue, you must use MessageCommandOptionManager#getOptionValue() with the resolveValue option set to true. This will make the return value of the option a Promise of the resolved value.Command Usage
INFO
You can handle invalid arguments by creating a command halt that handles the
CommandHaltReason.InvalidArguments.Custom Option Builders
Custom option builders allows you to use their prebuilt validators and value resolvers. For example, we will use the option builders from @reciple/message-command-utils.
import { MessageCommandBuilder } from 'reciple';
import { MessageCommandUserOptionBuilder } from '@reciple/message-command-utils'
new MessageCommandBuilder()
.setName('test')
.setDescription('A test command')
.addOption(new MessageCommandUserOptionBuilder()
.setName('user')
.setDescription('A user option')
.setRequired(false)
)
.setExecute(async ({ message, options }) => {
const user = await MessageCommandUserOptionBuilder.resolveOption('option', options);
await message.reply(user
? `**${user.displayName}** ${user.id}`
: 'No user'
);
}) Command Usage
More
@reciple/message-command-utilsusage examples: Here