Handlers
Learn how to use handlers in Honocord.
Handlers are the core of Honocord. They allow you to define both the metadata for Discord (like command names, descriptions, and options) and the logic that should run when an interaction occurs.
Honocord provides specialized handler classes for different types of interactions.
Explicit Type Arguments
The type argument is used for IDE IntelliSense and type safety (ensuring ctx is of type RoleSelectInteraction for example), while the constructor parameter is used for internal validation and routing.
Automatic inference is currently bugged and being worked on.
Slash Commands
SlashCommandHandler is used for chat input commands. It extends the @discordjs/builders SlashCommandBuilder, so you can use all the familiar methods to define your command’s structure.
Options and Subcommands
Since it extends SlashCommandBuilder, you can add options and subcommands directly.
Autocomplete
You can handle autocomplete interactions by adding an autocomplete handler to your command.
Autocomplete Helper
Honocord provides an AutocompleteHelper utility to simplify filtering choices based on user input.
Context Menu Commands
Use ContextCommandHandler to create User or Message context menu commands.
Message Components
ComponentHandler manages interactions from buttons and select menus. It uses a prefix-based matching system for custom_ids.
Select Menus
You can create handlers for all types of select menus by specifying the correct ComponentType.
Modals
ModalHandler works similarly to component handlers, matching the custom_id of a submitted modal.
Guild-Specific Commands
By default, commands are registered globally. You can restrict a command to specific guilds using .setGuildIds().
Need to handle commands whose name isn’t known at load time (e.g. per-guild custom commands)? See the Dynamic Commands guide.
Loading Handlers
Finally, you must load your handlers into the Honocord instance for them to be processed.
Or if you have many handlers, you can load them from an array:
Webhook Events
WebhookEventHandler manages Discord webhook events. Unlike interactions, webhooks are one-way notifications from Discord about events happening in your app.
Standard Mode (Default)
In standard mode, handlers must return a Response object. This is ideal for non-Cloudflare Workers environments.
Worker Mode (Cloudflare Workers)
For Cloudflare Workers, set the second constructor parameter to true. In worker mode:
- No return value is required from your handler
- Honocord automatically responds with
200 OKto Discord - Processing happens asynchronously via
waitUntil, extending the worker’s lifetime fetch()andgetApp()methods are disabled (type error + runtime error)
Webhook vs Interactions
| Feature | Interactions | Webhooks |
|---|---|---|
| Trigger | User actions (slash commands, buttons) | Discord events (entitlements, lobby messages) |
| Response | Required within 3 seconds | Must respond within 3 seconds (204 or 200) |
| Reply Method | interaction.reply() | Use REST API separately |
| Route | / or /interactions | /webhook |
| Setup | Interactions Endpoint URL | Webhook Events URL in Discord Developer Portal |
| CF Workers | Async via waitUntil | Async via waitUntil (worker mode) |
Webhook Handler Setup
When using WebhookEventHandler with Honocord:
- Load handlers using
bot.loadHandlers() - The webhook endpoint is automatically mounted at
/webhook - Configure your Discord webhook URL to point to
https://your-domain.com/webhook
Tip
Standalone Webhook Handlers
Webhook handlers can run independently of Honocord. This is useful for microservices or separate webhook endpoints.
Standard Mode Only: The fetch() and getApp() methods are only available in standard mode (when forWorker is false or omitted).
Warning
Available Webhook Events
Discord supports various webhook event types. Common ones include:
APPLICATION_AUTHORIZED- App authorized by userENTITLEMENT_CREATE,ENTITLEMENT_UPDATE,ENTITLEMENT_DELETE- Monetization eventsLOBBY_MESSAGE_CREATE,LOBBY_MESSAGE_UPDATE,LOBBY_MESSAGE_DELETE- Lobby messagesGAME_DIRECT_MESSAGE_CREATE,GAME_DIRECT_MESSAGE_UPDATE,GAME_DIRECT_MESSAGE_DELETE- Game DMs
Refer to the Discord Webhook Events documentation for the complete list.