HandlerRegistry
class in meocord/core Since 4.1.0
class HandlerRegistryLists every handler the app registered, with the metadata declared on it.
Inject it to build what reads the app's own handlers: a help command, an admin page or generated docs. To act
on a handler's metadata while it runs, read it from ExecutionContext instead.
Examples
@Service()
export class HelpService {
constructor(private readonly handlers: HandlerRegistry) {}
// `!help` lists the message commands; `!help ban` shows one
messageHelp(command?: string) {
const commands = this.handlers.list({ kind: 'message' }).filter(entry => entry.command)
const one = command ? commands.find(entry => entry.matches(command)) : undefined
if (one) return [one.usage('!'), one.description].filter(Boolean).join('\n')
return commands.map(entry => `${entry.usage('!')}: ${entry.description ?? ''}`).join('\n')
}
}Members
constructor
new HandlerRegistry(
classes: readonly HandlerClass[],
messages?: MessageCommandOptions,
translator?: () => Translator<any> | undefined,
)Parameters
| Name | Type | Default | Since | Description |
|---|---|---|---|---|
classes | readonly HandlerClass[] | The app's classes to read handlers from. The factory fills the list
once the app is bound; entries are read on the first | ||
messages? | MessageCommandOptions | The app's | ||
messages.prefix? | | MessagePrefix
| ((
message: Message,
) =>
| MessagePrefix
| null
| undefined
| Promise<
MessagePrefix | null | undefined
>) | What a message starts with to reach a patterned handler: a prefix, a list of them, or a function
of the message returning them, such as a server's own prefix. Without one, a pattern matches the
message as it is. Where a function finds none, returning an empty list, | ||
messages.mention? | boolean | 'only' | false | Also accepts a mention of the bot, | |
messages.caseSensitive? | boolean | false | Matches the prefix, a pattern's literal words, its choice words and its flag names in the case written. A text param keeps the case the user typed either way. | |
messages.types? | Record<string, MessageParamType> | Param types of the app's own, used in patterns as | ||
messages.deleteUsageRepliesAfter? | number | 10 | How long a reply showing a command's usage, or a guard's or validation's reason, stays before it is deleted, in
seconds. | |
messages.replyEmoji? | boolean | false | Begins every text reply MeoCord sends to a message with the theme's | |
messages.dmOnError? | boolean | false | Tells the author of a message command, in a direct message, when it fails with an error no filter handled,
naming the command, the channel and the server. The error is logged as without it, and nothing is said in the
channel: a message cannot be answered privately there. A command sent in a direct message is answered in it.
The text is | |
messages.dmOnCooldown? | boolean | false | Tells the author of a message command, in a direct message, when a | |
messages.help? | boolean | MessageHelpOptions | false | Answers | |
messages.handlers? | 'sequential' | 'concurrent' | 'sequential'`; the next major version (5.0) may run them concurrently by default | 4.2.0 | How a message's handlers run: the patterned handler it matched, then every |
messages.slowHandlerWarning? | boolean | true` in a running bot, `false` in a `MeoCordTestingModule | 4.2.0 | Warns, once per handler, when a message's handler takes 5 seconds or more with listeners waiting after it, under
|
translator? | () => Translator<any> | undefined | The app's translator, read when |
list
list<K extends HandlerKind = HandlerKind>(
filter?: HandlerFilter<K>,
): Extract<HandlerEntry, { kind: K }>[]Lists the registered handlers.
Parameters
| Name | Type | Description |
|---|---|---|
filter? | HandlerFilter<K> | Narrows the list by kind, controller, or both. |
Returns
Extract<HandlerEntry, { kind: K }>[]The handlers, class by class in the order the app makes its classes, each after what it injects.
messageHelp
messageHelp(message: Message, query?: string): Promise<MessageHelp>Works out what the built-in help would answer a message, for a help command of your own.
It lists the message commands of the app's controllers that work where the message was sent, or describes the
one query names, with the same routes and rules the built-in follows: hidden and guarded handlers are left out
of lists, and shown when named. It works whether messages.help is on or off.
Parameters
| Name | Type | Description |
|---|---|---|
message | Message | The message asking for help; its start and where it was sent decide what is listed. |
query? | string | The command asked about, such as |
Returns
Promise<MessageHelp>The help, as the presenter's messageHelp receives it.
Examples
@MessageHandler('help {command...?}')
async help(message: Message, { command }: { command?: string }) {
const help = await this.handlers.messageHelp(message, command)
if (help.kind === 'list') await message.reply(help.commands.map(entry => `**${entry.usage}** ${entry.description ?? ''}`).join('\n'))
else await message.reply(help.kind === 'unknown' ? `No command is called ${help.query}.` : 'Ask a moderator.')
}See also
- HandlerEntry
- Handler discovery