From c460a920ac4f08ab9f3d2ae6be927b8e1de3810c Mon Sep 17 00:00:00 2001 From: Asad <105254706+AsadHumayun@users.noreply.github.com> Date: Fri, 6 Feb 2026 16:57:49 +0000 Subject: [PATCH] feat(structure): add Webhook structure (#11401) * feat(structure): add barrel exports for Webhook structure * feat(structure): add Webhook structure * chore(structures): lib exports consistency * docs: update docs --- packages/structures/src/index.ts | 1 + packages/structures/src/webhooks/Webhook.ts | 108 ++++++++++++++++++++ packages/structures/src/webhooks/index.ts | 1 + 3 files changed, 110 insertions(+) create mode 100644 packages/structures/src/webhooks/Webhook.ts create mode 100644 packages/structures/src/webhooks/index.ts diff --git a/packages/structures/src/index.ts b/packages/structures/src/index.ts index f5bfbcecd..7ace22f7d 100644 --- a/packages/structures/src/index.ts +++ b/packages/structures/src/index.ts @@ -13,6 +13,7 @@ export * from './stageInstances/index.js'; export * from './stickers/index.js'; export * from './teams/index.js'; export * from './users/index.js'; +export * from './webhooks/index.js'; export * from './voice/index.js'; export * from './Structure.js'; export * from './subscriptions/index.js'; diff --git a/packages/structures/src/webhooks/Webhook.ts b/packages/structures/src/webhooks/Webhook.ts new file mode 100644 index 000000000..8b8336f9e --- /dev/null +++ b/packages/structures/src/webhooks/Webhook.ts @@ -0,0 +1,108 @@ +import { DiscordSnowflake } from '@sapphire/snowflake'; +import type { APIWebhook } from 'discord-api-types/v10'; +import { Structure } from '../Structure.js'; +import { kData } from '../utils/symbols.js'; +import { isIdSet } from '../utils/type-guards.js'; +import type { Partialize } from '../utils/types.js'; + +/** + * Represents any webhook on Discord. + * + * @typeParam Omitted - Specify the properties that will not be stored in the raw data field as a union, implement via `DataTemplate` + * @remarks has substructures `User`, `Guild`, `Channel` which need to be instantiated and stored by an extending class using it + */ +export class Webhook extends Structure { + /** + * The template used for removing data from the raw data stored for each webhook + */ + public static override readonly DataTemplate: Partial = {}; + + /** + * @param data - The raw data received from the API for the webhook + */ + public constructor(data: Partialize) { + super(data); + } + + /** + * The id of the webhook + */ + public get id() { + return this[kData].id; + } + + /** + * The type of the webhook + * + * @see {@link https://discord.com/developers/docs/resources/webhook#webhook-object-webhook-types} + */ + public get type() { + return this[kData].type; + } + + /** + * The guild id this webhook is for, if any + */ + public get guildId() { + return this[kData].guild_id; + } + + /** + * The channel id this webhook is for, if any + */ + public get channelId() { + return this[kData].channel_id; + } + + /** + * The default name of the webhook + */ + public get name() { + return this[kData].name; + } + + /** + * The default user avatar hash of the webhook + * + * @see {@link https://discord.com/developers/docs/reference#image-formatting} + */ + public get avatar() { + return this[kData].avatar; + } + + /** + * The secure token of the webhook (returned for incoming webhooks) + */ + public get token() { + return this[kData].token; + } + + /** + * The id of the bot/OAuth2 application that created this webhook + */ + public get applicationId() { + return this[kData].application_id; + } + + /** + * The url used for executing the webhook (returned by the webhooks OAuth2 flow) + */ + public get url() { + return this[kData].url; + } + + /** + * The timestamp the webhook was created at + */ + public get createdTimestamp() { + return isIdSet(this.id) ? DiscordSnowflake.timestampFrom(this.id) : null; + } + + /** + * The time the webhook was created at + */ + public get createdAt() { + const createdTimestamp = this.createdTimestamp; + return createdTimestamp ? new Date(createdTimestamp) : null; + } +} diff --git a/packages/structures/src/webhooks/index.ts b/packages/structures/src/webhooks/index.ts new file mode 100644 index 000000000..2b2083246 --- /dev/null +++ b/packages/structures/src/webhooks/index.ts @@ -0,0 +1 @@ +export * from './Webhook.js';