Discord Router Service
This file defines the Router
class for the @sodacore/core
Discord integration. The router is responsible for discovering Discord controllers, routing Discord interactions (commands, subcommands, buttons, select menus, context menus, autocomplete, modal submissions, and events) to the correct handler methods, and managing authentication and response formatting.
Features
- Controller Discovery: Automatically collects all registered Discord controllers and their methods from the DI registry.
- Interaction Routing: Handles routing for all major Discord interaction types:
- Slash commands
- Subcommands
- Buttons
- Select menus
- Context menus
- Autocomplete
- Modal submissions
- Events
- Authentication Guards: Supports per-method authentication via callback functions.
- Flexible Argument Injection: Injects context, user, guild, channel, client, query, options, and fields into handler methods based on decorator metadata.
- Unified Response Handling: Formats and sends responses to Discord interactions, supporting strings, booleans, objects, and arrays.
- Error Logging: Logs errors for failed handler execution or routing issues.
Usage
The Router
is used internally by the Sodacore Discord plugin/service. It is not typically instantiated directly.
import Router from './service/router';
const router = new Router(logger);
await router.init();
// Called by the Discord service on interaction events:
await router.onCommand(interaction);
await router.onButton(interaction);
// etc.
API
Methods
async init()
- Discovers and registers all Discord controllers and their methods.
async onCommand(interaction)
- Handles slash command interactions.
async onSubCommand(interaction)
- Handles subcommand interactions.
async onButton(interaction)
- Handles button interactions.
async onSelectMenu(interaction)
- Handles select menu interactions.
async onContextMenu(interaction)
- Handles context menu command interactions.
async onAutocomplete(interaction)
- Handles autocomplete interactions.
async onModalSubmit(interaction)
- Handles modal submission interactions.
async onEvent(event, ...data)
- Handles generic Discord events.
Internal Methods
getMethodArguments(interaction, handler, methodName, type, args, query?)
- Resolves and injects method arguments based on decorator metadata.
getController(command)
- 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 in a controller by multiple criteria.
getControllerbyUnique(type, uniqueId)
- Finds a controller and method by unique ID and type.
sendResponse(interaction, result)
- Formats and sends a response to the Discord interaction.
getReplyMethod(interaction, result)
- Determines the correct reply method for the interaction.
async verifyAuthentication(client, auth, user, guildId)
- Runs authentication guards for a method.
How It Works
Initialization:
- Scans all modules in the registry for Discord controllers.
- Collects their command names, methods, and metadata for routing.
Interaction Handling:
- For each Discord interaction, finds the appropriate controller and method.
- Verifies authentication guards if present.
- Resolves method arguments using decorator metadata.
- Executes the handler and sends a formatted response.
Response Formatting:
- Handles different result types (null, boolean, string, object, array) and sends the appropriate Discord response.
Authentication:
- Supports per-method authentication via callback functions that can check user or guild member permissions.
Notes
- This router is designed for internal use by the Sodacore Discord system.
- Controllers and methods must be registered using the appropriate decorators for routing to work.
- Error handling and logging are built-in for easier debugging.
- Supports advanced Discord features like autocomplete and modal submissions.