Skip to content
GitHub

route

function in meocord/common Since 4.1.0

route<const T extends string>(pattern: T): Route<T>

Makes a typed route from a customId pattern, so one declaration serves the handler and the ids that reach it.

Use it for a button, select menu or modal whose customId carries values, such as a ticket's ID. A plain string pattern works too, with nothing to check the ids a builder writes.

Parameters

NameTypeDescription
patternT

The customId pattern, where {name} captures one /-separated segment and {name:int} a typed one.

Returns

Route<T>

A route whose build takes a value for each param.

Throws

  • Error when the pattern cannot be read, as @Command would throw for it.

Examples

TypeScript
const closeTicket = route('ticket/{id:int}/close')

@Command(closeTicket, CommandType.BUTTON)
async close(interaction: ButtonInteraction, { id }: { id: number }) {
  await interaction.reply(`Closed ticket #${id}.`)
}

@Command('ticket', CommandType.SLASH)
async open(interaction: ChatInputCommandInteraction) {
  const close = new ButtonBuilder().setCustomId(closeTicket.build({ id: 42 })).setLabel('Close').setStyle(ButtonStyle.Danger)
  await interaction.reply({ components: [new ActionRowBuilder<ButtonBuilder>().addComponents(close)] })
}