WebhookEventHandler
Class: WebhookEventHandler<T, Env, Variables, ForWorker, Data>
Section titled “Class: WebhookEventHandler<T, Env, Variables, ForWorker, Data>”Defined in: handlers/WebhookEventHandler.ts:18
Represents a webhook event handler to be used by an Honocord instance or standalone fetch handler or Hono app.
Type Parameters
Section titled “Type Parameters”T extends ApplicationWebhookEventType
Discord webhook event type
Env extends object = { }
Environment bindings type
Variables
Section titled “Variables”Variables extends BlankVariables = BlankVariables
Additional context variables
ForWorker
Section titled “ForWorker”ForWorker extends boolean = false
Set to true for Cloudflare Workers mode (no return type required), false for standard mode (must return Response)
Data extends APIWebhookEventPayload<T> = APIWebhookEventPayload<T>
Typed webhook event data
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new WebhookEventHandler<
T,Env,Variables,ForWorker,Data>(eventType,forWorker?):WebhookEventHandler<T,Env,Variables,ForWorker,Data>
Defined in: handlers/WebhookEventHandler.ts:33
Parameters
Section titled “Parameters”eventType
Section titled “eventType”T
forWorker?
Section titled “forWorker?”ForWorker
Returns
Section titled “Returns”WebhookEventHandler<T, Env, Variables, ForWorker, Data>
Properties
Section titled “Properties”eventType
Section titled “eventType”
readonlyeventType:T
Defined in: handlers/WebhookEventHandler.ts:26
handlerType
Section titled “handlerType”
readonlyhandlerType:"webhook"="webhook"
Defined in: handlers/WebhookEventHandler.ts:25
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get fetch():
ForWorkerextendstrue?never: (request,env?,executionCtx?) =>Response|Promise<Response>
Defined in: handlers/WebhookEventHandler.ts:201
Returns the fetch handler for standalone usage.
Note: This method is only available in standard mode (ForWorker = false).
When using worker mode, this method returns never and will throw a runtime error.
When to use: Use this when you want a self-contained webhook endpoint that handles its own Discord request verification and ping events, independent of a Honocord instance.
This is ideal for:
- Microservices architecture where webhooks are separate from interaction handlers
- Multiple bots with different webhook endpoints
- Testing individual webhook handlers in isolation
- Deploying webhooks on different paths or domains
Example
Section titled “Example”import { Hono } from "hono";import { WebhookEventHandler } from "honocord/handlers";import { ApplicationWebhookEventType } from "discord-api-types/v10";
const app = new Hono();
// Standalone webhook handler with built-in verificationconst messageHandler = new WebhookEventHandler( ApplicationWebhookEventType.MessageCreate);
messageHandler.addHandler(async (c) => { const message = c.var.data; return c.json({ received: true });});
// Use as a standalone endpointapp.post("/discord-webhook", messageHandler.fetch);
export default app;Returns
Section titled “Returns”ForWorker extends true ? never : (request, env?, executionCtx?) => Response | Promise<Response>
A fetch-compatible handler function
Methods
Section titled “Methods”addHandler()
Section titled “addHandler()”addHandler(
handlerFn):WebhookEventHandler<T,Env,Variables,ForWorker,Data>
Defined in: handlers/WebhookEventHandler.ts:109
Registers the handler function for this webhook event.
Parameters
Section titled “Parameters”handlerFn
Section titled “handlerFn”ForWorker extends true ? WebhookEventHandlerFnForWorkers<Data, Env, BlankVariables & object> : WebhookEventHandlerFnWithRequest<Data, Env, BlankVariables & object>
The function to execute when this webhook event is received
Returns
Section titled “Returns”WebhookEventHandler<T, Env, Variables, ForWorker, Data>
Examples
Section titled “Examples”Standard mode (must return Response):
const handler = new WebhookEventHandler(ApplicationWebhookEventType.MessageCreate);
handler.addHandler(async (c) => { const message = c.var.data; console.log("Received message:", message.content); return c.json({ success: true }); // Must return Response});Worker mode (no return required):
const handler = new WebhookEventHandler(ApplicationWebhookEventType.MessageCreate, true);
handler.addHandler(async (c) => { const message = c.var.data; console.log("Received message:", message.content); // No return required in worker mode});execute()
Section titled “execute()”execute(
eventData,c):Promise<any>
Defined in: handlers/WebhookEventHandler.ts:144
Execute the handler with pre-verified event data.
When to use: This method is automatically called when the handler is registered with Honocord via loadHandlers().
The Honocord instance handles request verification and ping events once, then delegates to this method.
You typically don’t call this directly - it’s used internally by Honocord’s webhookHandler.
Parameters
Section titled “Parameters”eventData
Section titled “eventData”Data
The pre-verified webhook event data
Context<{ Bindings: Env; Variables: BlankVariables & object; }>
The Hono context
Returns
Section titled “Returns”Promise<any>
The response from the handler function
Example
Section titled “Example”// This is handled automatically when using Honocord:const bot = new Honocord();const handler = new WebhookEventHandler(ApplicationWebhookEventType.MessageCreate);handler.addHandler(async (c) => c.json({ ok: true }));
bot.loadHandlers(handler); // execute() is called internallyexport default bot.getApp(); // POST /webhookgetApp()
Section titled “getApp()”getApp():
ForWorkerextendstrue?never:Hono<{Bindings:Env;Variables:Omit<Variables,"data"> &object; },BlankSchema,"/">
Defined in: handlers/WebhookEventHandler.ts:244
Returns the internal Hono app for standalone usage.
Note: This method is only available in standard mode (ForWorker = false).
When using worker mode, this method returns never and will throw a runtime error.
When to use: Similar to fetch, but allows you to mount the handler on a route prefix.
This provides the same self-contained verification as fetch.
Returns
Section titled “Returns”ForWorker extends true ? never : Hono<{ Bindings: Env; Variables: Omit<Variables, "data"> & object; }, BlankSchema, "/">
A Hono app instance
Example
Section titled “Example”import { Hono } from "hono";import { WebhookEventHandler } from "honocord/handlers";import { ApplicationWebhookEventType } from "discord-api-types/v10";
const app = new Hono();
const messageHandler = new WebhookEventHandler( ApplicationWebhookEventType.MessageCreate);
messageHandler.addHandler(async (c) => { return c.json({ ok: true });});
// Mount on a prefixapp.route("/discord", messageHandler.getApp());// Available at POST /discord
export default app;