Skip to content

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.

T extends ApplicationWebhookEventType

Discord webhook event type

Env extends object = { }

Environment bindings type

Variables extends BlankVariables = BlankVariables

Additional context variables

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

new WebhookEventHandler<T, Env, Variables, ForWorker, Data>(eventType, forWorker?): WebhookEventHandler<T, Env, Variables, ForWorker, Data>

Defined in: handlers/WebhookEventHandler.ts:33

T

ForWorker

WebhookEventHandler<T, Env, Variables, ForWorker, Data>

readonly eventType: T

Defined in: handlers/WebhookEventHandler.ts:26


readonly handlerType: "webhook" = "webhook"

Defined in: handlers/WebhookEventHandler.ts:25

get fetch(): ForWorker extends true ? 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
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 verification
const messageHandler = new WebhookEventHandler(
ApplicationWebhookEventType.MessageCreate
);
messageHandler.addHandler(async (c) => {
const message = c.var.data;
return c.json({ received: true });
});
// Use as a standalone endpoint
app.post("/discord-webhook", messageHandler.fetch);
export default app;

ForWorker extends true ? never : (request, env?, executionCtx?) => Response | Promise<Response>

A fetch-compatible handler function

addHandler(handlerFn): WebhookEventHandler<T, Env, Variables, ForWorker, Data>

Defined in: handlers/WebhookEventHandler.ts:109

Registers the handler function for this webhook event.

ForWorker extends true ? WebhookEventHandlerFnForWorkers<Data, Env, BlankVariables & object> : WebhookEventHandlerFnWithRequest<Data, Env, BlankVariables & object>

The function to execute when this webhook event is received

WebhookEventHandler<T, Env, Variables, ForWorker, Data>

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(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.

Data

The pre-verified webhook event data

Context<{ Bindings: Env; Variables: BlankVariables & object; }>

The Hono context

Promise<any>

The response from the handler function

// 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 internally
export default bot.getApp(); // POST /webhook

getApp(): ForWorker extends true ? 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.

ForWorker extends true ? never : Hono<{ Bindings: Env; Variables: Omit<Variables, "data"> & object; }, BlankSchema, "/">

A Hono app instance

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 prefix
app.route("/discord", messageHandler.getApp());
// Available at POST /discord
export default app;