MessageHandler
function in meocord/decorator Since 4.0.0
MessageHandler<
T extends OmitPartialGroupDMChannel<Message<boolean>>,
R,
>(): (
target: object,
propertyKey: string,
_descriptor:
| TypedPropertyDescriptor<(message: T) => R>
| TypedPropertyDescriptor<() => R>,
) => void
MessageHandler<
T extends OmitPartialGroupDMChannel<Message<boolean>>,
R,
const Pattern extends string = string,
>(
pattern: Pattern,
options?: MessageHandlerOptions,
): PatternedMessageHandlerDecorator<T, R, Pattern>Runs the method it decorates for every message a user sends, whatever it says.
Use it for work on all chat, such as logging, auto-moderation or counting activity. For a command a user
types, such as !roll 20, give @MessageHandler a pattern instead.
Where it runs
- The handler — after every stage the call passed
- Parse — the pattern's words, before the guards
Returns
Returns a method decorator.
(
target: object,
propertyKey: string,
_descriptor:
| TypedPropertyDescriptor<(message: T) => R>
| TypedPropertyDescriptor<() => R>,
) => voidExamples
@MessageHandler()
async log(message: Message) {
console.log(`${message.author.username}: ${message.content}`)
}Parameters
| Name | Type | Default | Since | Description |
|---|---|---|---|---|
pattern | Pattern | 4.1.0 | The words to match, such as | |
options? | MessageHandlerOptions | 4.1.0 | The handler's own start, case, aliases, description and scope; see | |
options.prefix? | false | MessagePrefix | 4.1.0 | The handler's own prefixes, in place of the app's; a mention of the bot still counts when the app
accepts one. | |
options.mention? | 'only' | 4.1.0 |
| |
options.caseSensitive? | boolean | 4.1.0 | Overrides the app's | |
options.aliases? | readonly string[] | 4.1.0 | Other words for the command, each in place of the words the pattern begins with: with | |
options.description? | string | 4.1.0 | What the command does, shown by the built-in help and by | |
options.scope? | MessageScope | 4.1.0 | Where the command works; |
Returns
PatternedMessageHandlerDecorator<T, R, Pattern>Throws
Error at startup for a pattern that cannot be read, and for two patterns that match the same messages.
Examples
@MessageHandler('roll {sides:int} {note...?}', { aliases: ['r'] })
async roll(message: Message, { sides, note }: { sides: number; note?: string }) {
const result = 1 + Math.floor(Math.random() * sides)
await message.reply(note ? `${result} (${note})` : String(result))
}