Interaction Routing
Relevant source files
The following files were used as context for generating this wiki page:
Interaction Routing is the architectural mechanism within TrackerBot that processes and directs incoming Discord interactions (Slash Commands and Button clicks) to their respective logic handlers. The system ensures that requests are authenticated via role-based checks, validated for guild context, and executed within a safe error-handling wrapper.
The routing process begins at the entry point of the bot, where the client is initialized and global event listeners are registered to listen for the InteractionCreate event.
Sources: bot.js:1-25, src/handlers/interactionHandler.js:5-8
Core Architecture and Data Flow
The routing architecture follows a hierarchical structure where a primary entry handler delegatestasks to specialized sub-handlers based on the interaction type.
This diagram illustrates the high-level flow of an interaction from the Discord API through the bot's internal routing logic.
Sources: src/handlers/interactionHandler.js:7-30
Entry Point: Interaction Handler
The primary entry point is registerInteractionHandler, which attaches a listener to the Events.InteractionCreate event. This handler performs two critical initial checks:
- Guild Context: It verifies
interaction.inGuild()to ensure the command was not sent via Direct Messages (DMs). - Type Identification: It uses
interaction.isChatInputCommand()andinteraction.isButton()to route the request to the correct module.
Sources: src/handlers/interactionHandler.js:7-25
Slash Command Routing
Slash commands are managed through handleChatInputCommand. Before execution, the system performs a permission check by comparing the user's roles against the allowedCommandRoles defined in the configuration.
| Handler Function | Command Name | Purpose | Required Role |
|---|---|---|---|
handleMessageCommand | message | Sends a message as the bot | Discord Admin |
handleFaqCommand | faq1 | Posts website stability FAQ | Allowed Command Role |
handleUtdCommand | utd | Posts update notice | Allowed Command Role |
handleArchiveChannel | archive | Moves channel to Archive | Allowed Command Role |
handlePutMemberRole | put-member-role | Bulk assigns roles | Panel Admin |
executeWipe | wipe | Bulk deletes messages | Admin Role |
executeNuke | nuke | Clones/Deletes channel | Admin Role |
Sources: src/interactions/slashHandlers.js:14-53, src/config/constants.js:4-21
Command Registration
Commands must be registered with the Discord API using the registerSlashCommands utility, which utilizes the REST and Routes modules from discord.js.
export async function registerSlashCommands(client) {
const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN);
await rest.put(Routes.applicationCommands(client.user.id), {
body: slashCommandData
});
}
Sources: src/commands/registerSlashCommands.js:31-37
Button Interaction Routing
Button interactions are routed via handleButtonInteraction. This router primarily handles two types of interactions: ticket system triggers and notification role toggles.
The sequence diagram shows how the customId of a button determines which logic branch is executed.
Sources: src/interactions/buttonHandlers.js:11-23
Key Button Handlers
- Ticket Creation: Triggered by the
create_ticketID. It creates a private channel and configures permission overwrites for the user and admins. - Reaction Roles: Uses a
reactionRoleNameMapto matchcustomIdvalues to specific roles (e.g.,role_sitemapped to "site").
Sources: src/interactions/buttonHandlers.js:25-50, src/config/constants.js:29-37
Error Handling in Routing
The interaction routing system includes a safety layer within interactionHandler.js to prevent bot crashes from unhandled exceptions during command execution.
- Catch Block: All interaction processing is wrapped in a
try...catch. - User Feedback: If an error occurs, the system checks if the interaction was already deferred or replied to. If not, it sends an ephemeral "Something went wrong" message.
- Logging: Errors are logged to the console via
console.error.
Sources: src/handlers/interactionHandler.js:26-44
Summary
Interaction Routing in TrackerBot serves as the central traffic controller for user-driven events. By decoupling the event listener from the business logic of individual commands and buttons, the architecture maintains a clear separation of concerns, ensures consistent permission enforcement across all features, and provides a resilient environment for handling Discord's API interactions.