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
| Name | Type | Description |
|---|---|---|
pattern | T | The customId pattern, where |
Returns
Route<T>A route whose build takes a value for each param.
Throws
Error when the pattern cannot be read, as
@Commandwould throw for it.
Examples
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)] })
}