meshcore.js
Commands

Arguments

The eight argument types, their rules, and how ctx.args gets its type.

Each argument is declared with one method taking a builder callback.

MethodParsed type
addStringArgstring
addIntegerArgnumber
addNumberArgnumber
addBooleanArgboolean
addChoiceArgThe union given to setChoices()
addContactArgA Contact resolved from the radio's table
addRoleArgA Role resolved from the registered roles
addChannelArgA Channel resolved from the radio's table
commands/forecast.ts
export const forecast = new CommandBuilder()
  .setName('forecast')
  .setDescription('Weather forecast for a city')
  .addStringArg((arg) => arg.setName('city').setDescription('City name').setDefault('here'))
  .addIntegerArg((arg) => arg.setName('days').setDescription('Days ahead').setMin(1).setMax(5).setDefault(2))
  .addChoiceArg((arg) => arg.setName('unit').setDescription('Temperature unit').setChoices('c', 'f').setDefault('c'))
  .setHandler((ctx) => {
    // ctx.args is inferred as { city: string; days: number; unit: 'c' | 'f' }
    return ctx.reply(`${ctx.args.city}, ${ctx.args.days}d, °${ctx.args.unit.toUpperCase()}`);
  });

Rules

These rules are checked at build().

  • Argument names are unique.
  • A required argument cannot follow an optional one.
  • setDefault() makes an argument optional.
  • setRest() takes the rest of the line. String arguments only, last argument only.

Values are split on whitespace. Double quotes keep a value with spaces together.

Usage errors

A missing, invalid or extra value never reaches the handler. The bot answers with missingArgument, invalidArgument or tooManyArguments from client.replies, followed by the usage line. setUsageErrorHandler() replaces that reply for one command.

Reference

On this page