Skip to content

Discord Controller Decorators

This file provides a set of class decorators for registering Discord controllers in the @sodacore/core Discord integration. These decorators attach metadata to controller classes, enabling them to be discovered and registered as Discord slash commands, context menus, events, or handlers.


Features

  • Slash Command Registration: Use @Command to register a class as a Discord slash command controller with a provided command builder.

  • Context Menu Registration: Use @ContextMenu to register a class as a Discord context menu controller.

  • Event Registration: Use @Event to register a class as a Discord event controller.

  • Handler Registration: Use @Handler to register a class as a Discord event handler.

  • Metadata Management: All decorators set the appropriate metadata for type, service, and builder, enabling automatic discovery and registration by the framework.


Usage

typescript
import { Command, ContextMenu, Event, Handler } from './decorator/command';
import { SlashCommandBuilder } from 'discord.js';

@Command(new SlashCommandBuilder().setName('ping').setDescription('Replies with Pong!'))
class PingCommand {
	// Command logic here
}

@ContextMenu()
class MyContextMenu {
	// Context menu logic here
}

@Event()
class ReadyEvent {
	// Event logic here
}

@Handler()
class MessageHandler {
	// Handler logic here
}

API

Command(builder: SharedSlashCommand): ClassDecorator

Registers a class as a Discord slash command controller.

  • builder: A SharedSlashCommand instance defining the command.

ContextMenu(): ClassDecorator

Registers a class as a Discord context menu controller.

Event(): ClassDecorator

Registers a class as a Discord event controller.

Handler(): ClassDecorator

Registers a class as a Discord event handler (alias for event controller).


How It Works

  • Each decorator sets the type metadata for both autowire and discord to identify the controller type.
  • The services metadata is updated to include 'discord', marking the class as a Discord service.
  • The Command decorator also sets the builder metadata for command registration.

Notes

  • These decorators are intended for use within Discord controller classes.
  • The metadata is used by the framework to discover and register controllers with Discord's API.
  • Requires discord.js as a peer dependency.

Released under the Apache-2.0 License.