Webhook events
Receive app authorizations, entitlements and other webhook events.
Besides interactions, Discord can send some events about your app over HTTP: webhook events. For example, when a user authorizes or deauthorizes your app, or when an entitlement is created.
They're sent to a separate URL, the Webhook Events URL, which you set on the Developer Portal under Webhooks, along with the events you want to receive.
Mounting
client.getWebhookEventHandler() returns a handler for that URL, which works with the same adapters as the client:
import { } from '@nyx-discord/http';
.('/interactions', ());
.('/webhook-events', (.()));The handler verifies requests like the client does, and answers every event (and Discord's PING) with a 204.
Subscribing
Each event is emitted on the client's emitter under its type, one of the ApplicationWebhookEventType values. An HttpBot's client event bus accepts them, so write an AbstractWebhookEventSubscriber and subscribe it like any other client subscriber:
import type { APIWebhookEventApplicationAuthorizedData } from '@discordjs/core';
import { } from '@discordjs/core';
import { , type , type } from '@nyx-discord/http';
class extends <.> {
protected readonly = .;
public (
: ,
{ , }: <APIWebhookEventApplicationAuthorizedData>,
) {
.(`${.user.} authorized the app at ${}`); }
}
await .(new ());Since they're client subscribers, the event loader picks them up too, and their metadata has the same fields as other client events, like TypedFields.HttpRequest.
The payload is the usual { data, api, shardId }, where data is the event's data (APIWebhookEvent...Data from discord-api-types), plus the event's timestamp. Webhook events aren't realtime and can arrive late or out of order, so use the timestamp when order matters.
Discord retries an event for up to 10 minutes if your endpoint doesn't answer, and stops sending events if it fails too often. Since the handler answers as soon as the event is emitted, slow subscribers don't delay the response.
Last updated on
