Webhook events from Discord
Discord can send some events to your app as signed HTTP requests, with no
Gateway connection. These are
webhook events,
such as APPLICATION_AUTHORIZED when someone installs the app, and
ENTITLEMENT_CREATE when someone buys a SKU. On a serverless host, they’re the
only way to receive events. Handle one with app.event():
app.event("APPLICATION_AUTHORIZED", (c) => { const where = c.data.guild ? `in ${c.data.guild.name}` : "to their account"; return c.send( process.env.INSTALLS_CHANNEL_ID!, `${c.data.user.username} installed the app ${where}`, );});
app.event("ENTITLEMENT_CREATE", (c) => { console.log(`User ${c.data.user_id} bought SKU ${c.data.sku_id}`);});c.data is typed by the event type. Types without typed data, such as
LOBBY_MESSAGE_CREATE, are accepted too, with unknown data.
Set the Webhook Events URL
Section titled “Set the Webhook Events URL”Discord sends webhook events to their own URL, apart from the Interactions
Endpoint URL. The app serves them at /events once it has an app.event()
handler.
-
Deploy.
discord0 deployroutes/eventson every host. On a hosted adapter, it prints the URL next to the Interactions endpoint:Interactions endpoint: https://my-bot.example.workers.dev/interactionsWebhook Events URL: https://my-bot.example.workers.dev/eventsOn Node.js and Bun,
serve()logs the URL when the server starts. -
In the Developer Portal, open your app’s Webhooks page, paste that URL into Endpoint URL, and save. Discord sends a PING, which the app answers.
-
Turn on Events, and enable each event type you handle. Deploy prints the list.
For local development, discord0 dev logs the events URL too. Give Discord the
same URL through your HTTPS tunnel, as for interactions.
To serve events at another path, set eventsPath:
const app = new Discord({ eventsPath: "/discord/events" });How events are handled
Section titled “How events are handled”- Each request is checked against the app’s public key, like an interaction,
with the same Ed25519 signature and timestamp window. Requests that fail
answer
401. - Discord’s PING answers
204. - An event answers
204at once, before its handlers run. They then finish through the host’swaitUntil, as deferred work does, so the same limits apply on each host. - An event type may have several handlers, such as one per
sub-app. They run together, and an event with no
handler is still answered
204. - The app logs a handler’s error. Discord retries only events that go unanswered, so it doesn’t resend an event whose handler failed.
The event context
Section titled “The event context”| Member | Description |
|---|---|
c.type |
The event type, such as APPLICATION_AUTHORIZED. |
c.data |
The event’s data, typed by its type. |
c.timestamp |
When the event occurred, as a Date. |
c.applicationId |
The ID of the app the event is for. |
c.event |
The event object as Discord sent it. |
c.send() |
Posts a message to a channel as the bot, and returns it. |
c.executeWebhook() |
Posts through a Discord webhook, by its URL or { id, token }. |
Testing
Section titled “Testing”app.dispatchEvent() runs the handlers for an event body, skipping the
signature check, and resolves when they finish. Handler errors reject:
await app.dispatchEvent({ version: 1, application_id: "123", type: 1, event: { type: "APPLICATION_AUTHORIZED", timestamp: new Date().toISOString(), data: { user: { id: "456", username: "tester", discriminator: "0", global_name: null, avatar: null, }, scopes: ["applications.commands"], }, },});