Files
discord.js/packages/structures/src/webhooks/Webhook.ts
T
Asad Humayun a81ed8a306 feat(structures): add Presences structures (#11411)
* feat(structures): presence structures

* feat(structures): add GatewayPresenceActivityTimestamps substructure

* feat(structures): add GatewayPresenceActivitySecrets

* feat(structures): add GatewayPresenceActivityParty

* feat(structures): add GatewayPresenceActivityEmoji

* feat(structures): add GatewayPresenceActivityButton

* feat(structures): add GatewayPresenceActivityAssets

* feat(structures): add ActivityFlagBitFields

* feat(structures): add GatewayPresenceActivity structure

* docs(structures): update class remark on GatewayPresenceActivityButton

* feat(structures): add GatewayPresenceClientStatus

* feat(structures): add GatewayPresenceUpdate

* docs(structures): update `GatewayPresenceActivity#flags` for clarity

* fix(structures): return correct value for getter on ActivitySecrets

* test(structures): add tests & use correct type on ActivityTimestamps

* docs: rm todo comment

* fix: correct incorrect implementation of party#id

* chore: rename structures

* feat(structures): update tests, comments, date getters

* feat: split `size` into 2 different getters

* test: update tests for new getters

* refactor(structures)!: use xDate convention in favour of xAt

* fix(structures): add `inviteCoverImage` getter + update tests

* test(structures): apply review feedback

* chore: use `dateToDiscordISOTimestamp` and cleanup tests

* feat: isArrayFieldSet typeguard & tidy up

* feat(structures): add URL getters, typeguards, update tests

* test(presence): review feedback

* test(presence): use realistic values

This uses values from the Discord API docs.

* test: rename tests to Presence instead of GatewayPresence

* test(structures): review feedback

* docs: use new discord docs links in jsdoc

* fix: address feedback for tests

Make some changes to all tests;
- use a standard beforeEach block to use a new instance on each test for robustness;
- check that the patched result contains the expected patched values as opposed to testing for any change across the structure, for accuracy.
2026-08-29 06:39:46 +00:00

109 lines
2.6 KiB
TypeScript

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<Omitted extends keyof APIWebhook | '' = ''> extends Structure<APIWebhook, Omitted> {
/**
* The template used for removing data from the raw data stored for each webhook
*/
public static override readonly DataTemplate: Partial<APIWebhook> = {};
/**
* @param data - The raw data received from the API for the webhook
*/
public constructor(data: Partialize<APIWebhook, Omitted>) {
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 createdDate() {
const createdTimestamp = this.createdTimestamp;
return createdTimestamp ? new Date(createdTimestamp) : null;
}
}