Skip to content
GitHub

Route

interface in meocord/common Since 4.1.0

interface Route<T extends string = string>

A component's customId pattern with a typed build and match, as route makes it.

Pass it to @Command in place of the pattern's text, build the customIds that reach its handler, and match one to read its params back.

Members

pattern

readonly pattern: T

The pattern, as written.

build

build(
  ...values: [RouteParams<T>] extends [never] ? [] : [values: RouteValues<T>]
): string

A customId this route matches, with each param's value in its segment. / and % in a value are encoded, and the handler receives the value as it was given.

Parameters

NameType
...values[RouteParams<T>] extends [never] ? [] : [values: RouteValues<T>]

Returns

string

Throws

  • TypeError for a missing, empty or unknown value or one not of its param's type, and RangeError for an id that is empty or over 100 characters.

match Since 4.3.0

match(customId: string): RouteMatch<T> | undefined

Reads a customId's params as this route's handler receives them, for a customId the bot did not get as a handler call, such as another button on the message. / and % that build encoded come back as they were given, and a typed param comes back as its value.

Parameters

NameType
customIdstring

The customId to read.

Returns

RouteMatch<T> | undefined

The params, an empty object for a pattern without params, or undefined when the customId doesn't match the pattern or a segment isn't a value of its param's type.

Examples

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

closeTicket.match('ticket/42/close') // { id: 42 }
closeTicket.match('ticket/new/close') // undefined: 'new' isn't an int
closeTicket.match('poll/1') // undefined

toString

toString(): T

The pattern, so a route reads as its pattern in a template string.

Returns

T

See also