Skip to content
GitHub

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>,
) => void

Examples

TypeScript
@MessageHandler()
async log(message: Message) {
  console.log(`${message.author.username}: ${message.content}`)
}

Parameters

NameTypeDefaultSinceDescription
patternPattern4.1.0

The words to match, such as 'roll {sides:int} {note...?}'. An empty pattern runs for every message, as @MessageHandler() does, and logs a warning: it is deprecated, and refused in 5.0.

options?MessageHandlerOptions4.1.0

The handler's own start, case, aliases, description and scope; see MessageHandlerOptions.

options.prefix?false | MessagePrefix4.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. false matches the message as it is, with no prefix or mention. Where a message also fits a handler with the same pattern that takes the app's prefixes, as it can when the app's prefix is a function, this handler runs, whatever order the controllers are listed in.

options.mention?'only'4.1.0

'only' starts the command in a server with a mention of the bot and nothing else, whatever the app's prefixes, so it needs no MessageContent intent. In a direct message it starts as usual, after its own prefix or the app's.

options.caseSensitive?boolean4.1.0

Overrides the app's caseSensitive for this handler.

options.aliases?readonly string[]4.1.0

Other words for the command, each in place of the words the pattern begins with: with ['b'], ban {target:member} also takes !b @ana. HandlerRegistry lists the handler once, with its aliases.

options.description?string4.1.0

What the command does, shown by the built-in help and by HandlerRegistry.

options.scope?MessageScope4.1.0

Where the command works; 'any' by default. A message sent elsewhere gets a usage reply saying where it works, and the handler does not run. A command with a member, role or channel param works in servers only; 'dm' with one is refused as the bot starts.

options.hidden?booleanfalse4.1.0

Leaves the command out of the built-in help's list and of a parent's list of subcommands. It is not a secret: it still runs, a misuse still gets its usage, and !help <command> still shows it when named.

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

TypeScript
@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))
}