Router
This file defines the Router
class, which is responsible for routing all Discord interactions and events to the appropriate controller methods in the @sodacore/core
Discord integration. It dynamically discovers Discord controllers, resolves method arguments, verifies authentication, and handles responses for commands, subcommands, buttons, select menus, context menus, autocomplete, modal submissions, and generic events.
Features
- Dynamic Controller Discovery: Finds all registered Discord controllers and their methods using metadata and the registry.
- Comprehensive Interaction Routing: Handles slash commands, subcommands, buttons, select menus, context menus, autocomplete, modal submissions, and generic Discord events.
- Authentication Guards: Supports per-method authentication via custom guards and role checks.
- Flexible Argument Injection: Resolves and injects method arguments based on parameter decorators and interaction context.
- Unified Response Handling: Formats and sends responses to Discord interactions, supporting various result types (string, boolean, object, array).
- Error Logging: Logs errors encountered during handler execution for easier debugging.
Usage
The Router
is used internally by the DiscordService
to route all Discord events and interactions.
import Router from './service/router';
const router = new Router(logger);
await router.init();
// Called automatically by DiscordService on relevant events/interactions
API
Constructor
constructor(logger: Logger)
- logger: An instance of the Sodacore
Logger
for error and info logging.
Methods
async init(): Promise<void>
Discovers all Discord controllers and their methods, storing them for routing.
async onCommand(interaction: ChatInputCommandInteraction): Promise<void>
Handles a slash command interaction, resolves the controller and method, verifies authentication, injects arguments, and executes the handler.
async onSubCommand(interaction: ChatInputCommandInteraction): Promise<void>
Handles a subcommand interaction similarly to onCommand
.
async onButton(interaction: ButtonInteraction): Promise<void>
Handles a button interaction, routing to the correct controller and method by unique ID.
async onSelectMenu(interaction: StringSelectMenuInteraction): Promise<void>
Handles a select menu interaction, routing by unique ID.
async onContextMenu(interaction: ContextMenuCommandInteraction): Promise<void>
Handles a context menu interaction, routing by unique command name.
async onAutocomplete(interaction: AutocompleteInteraction): Promise<void>
Handles autocomplete interactions for command options.
async onModalSubmit(interaction: ModalSubmitInteraction): Promise<void>
Handles modal submit interactions, routing by unique ID.
async onEvent(event: keyof ClientEventTypes, ...data: unknown[]): Promise<void>
Handles generic Discord events, routing to any controller methods registered for the event.
Private Methods
getMethodArguments(...)
Resolves and injects arguments for a controller method based on parameter decorators and interaction context.
getController(command: string)
Finds a controller by command name.
getControllerMethod(controller, unique, type?)
Finds a method in a controller by unique identifier and type.
getControllerMethodByMultiple(controller, type, unique, subType?)
Finds a method by multiple criteria (type, unique, subType).
getControllerbyUnique(type, uniqueId)
Finds a controller and method by unique ID and type.
sendResponse(interaction, result)
Formats and sends a response to the interaction, supporting various result types.
getReplyMethod(interaction, result)
Determines the correct reply method for the interaction (reply, editReply, followUp).
async verifyAuthentication(client, auth, user, guildId)
Verifies all authentication guards for a method, supporting both user and guild member checks.
How It Works
- Initialization: Discovers all Discord controllers and their methods, storing them for fast lookup.
- Routing: For each interaction or event, finds the appropriate controller and method, verifies authentication, resolves arguments, and executes the handler.
- Argument Injection: Uses parameter metadata to inject the correct values (interaction, user, guild, channel, options, fields, etc.) into handler methods.
- Response Handling: Formats and sends responses to Discord, handling all supported result types.
- Authentication: Executes all authentication guards before invoking the handler.
Notes
- This router is designed to be used internally by the Discord service and is not intended for direct use.
- Supports extensibility via metadata and custom decorators for new interaction types or authentication strategies.
- Requires
discord.js
as a peer dependency.