Skip to content

Discord Event Handler Decorators

This file provides a set of method decorators for Discord controller methods in the @sodacore/core Discord integration. These decorators allow you to register controller methods as handlers for various Discord events, such as commands, subcommands, buttons, select menus, events, autocomplete, and modal submissions. The decorators attach metadata for later discovery and routing by the Discord module.


Features

  • Event Handler Registration: Registers controller methods as handlers for Discord commands, subcommands, buttons, select menus, events, autocomplete, and modal submissions.
  • Unique Identification: Associates each handler with a unique identifier or event name for precise routing.
  • Metadata Management: Stores method metadata for each handler, enabling the framework to resolve and invoke the correct method at runtime.

Usage

typescript
import {
	Command,
	SubCommand,
	Button,
	SelectMenu,
	Event,
	Autocomplete,
	ModalSubmit
} from './decorator/on';

class MyDiscordController {
	@Command()
	async handleCommand(ctx) { /* ... */ }

	@SubCommand('settings')
	async handleSettings(ctx) { /* ... */ }

	@Button('confirmBtn')
	async handleButton(ctx) { /* ... */ }

	@SelectMenu('roleMenu')
	async handleSelectMenu(ctx) { /* ... */ }

	@Event('guildMemberAdd')
	async onGuildMemberAdd(ctx) { /* ... */ }

	@Autocomplete('search', 'sub')
	async handleAutocomplete(ctx) { /* ... */ }

	@ModalSubmit('feedbackModal')
	async handleModal(ctx) { /* ... */ }
}

API

Command(): MethodDecorator

Registers the method as a handler for a Discord slash command.

SubCommand(subCommand: string): MethodDecorator

Registers the method as a handler for a specific subcommand.

  • subCommand: The name of the subcommand.

Button(uniqueId: string): MethodDecorator

Registers the method as a handler for a button interaction.

  • uniqueId: The unique identifier for the button.

SelectMenu(uniqueId: string): MethodDecorator

Registers the method as a handler for a select menu interaction.

  • uniqueId: The unique identifier for the select menu.

Event(event: string): MethodDecorator

Registers the method as a handler for a Discord event.

  • event: The name of the Discord event.

Autocomplete(name: string, subCommand?: string): MethodDecorator

Registers the method as a handler for autocomplete interactions.

  • name: The name of the command or option.
  • subCommand: (Optional) The subcommand name.

ModalSubmit(unique: string): MethodDecorator

Registers the method as a handler for modal submission interactions.

  • unique: The unique identifier for the modal.

How It Works

  • Each decorator sets metadata on the decorated method, including its type, unique identifier, and any relevant subtypes.
  • The metadata is stored using Utils.setMeta and is later used by the Discord integration to discover and route events to the correct handler methods.

Notes

  • These decorators are intended for use within Discord controller classes in the Sodacore framework.
  • The unique identifiers and event names must match those used in your Discord bot's commands and components.
  • Only methods decorated with these decorators will be registered as event handlers.

Released under the Apache-2.0 License.