From decbce401062af75f633e6acacc88207b115a719 Mon Sep 17 00:00:00 2001 From: MrMythicalYT <91077061+MrMythicalYT@users.noreply.github.com> Date: Sat, 26 Nov 2022 07:31:05 -0500 Subject: [PATCH] feat(Webhook): add `channel` property (#8812) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(Webhook): add `channel` property * fix: allow ForumChannel type * fix: disallow thread channel type * fix: formatting * Apply suggestions from code review Co-authored-by: Aura Román Co-authored-by: Aura Román --- packages/discord.js/src/structures/Webhook.js | 29 ++++++++++++++++--- packages/discord.js/typings/index.d.ts | 1 + 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/discord.js/src/structures/Webhook.js b/packages/discord.js/src/structures/Webhook.js index e6eccaed4134..4ac020a41872 100644 --- a/packages/discord.js/src/structures/Webhook.js +++ b/packages/discord.js/src/structures/Webhook.js @@ -39,7 +39,11 @@ class Webhook { * @name Webhook#token * @type {?string} */ - Object.defineProperty(this, 'token', { value: data.token ?? null, writable: true, configurable: true }); + Object.defineProperty(this, 'token', { + value: data.token ?? null, + writable: true, + configurable: true, + }); if ('avatar' in data) { /** @@ -73,7 +77,7 @@ class Webhook { if ('channel_id' in data) { /** - * The channel the webhook belongs to + * The id of the channel the webhook belongs to * @type {Snowflake} */ this.channelId = data.channel_id; @@ -141,6 +145,15 @@ class Webhook { * For interaction webhooks, this property is ignored */ + /** + * The channel the webhook belongs to + * @type {?(TextChannel|VoiceChannel|NewsChannel|ForumChannel)} + * @readonly + */ + get channel() { + return this.client.channels.resolve(this.channelId); + } + /** * Sends a message with this webhook. * @param {string|MessagePayload|WebhookCreateMessageOptions} options The options to provide @@ -206,7 +219,12 @@ class Webhook { }); const { body, files } = await messagePayload.resolveFiles(); - const d = await this.client.rest.post(Routes.webhook(this.id, this.token), { body, files, query, auth: false }); + const d = await this.client.rest.post(Routes.webhook(this.id, this.token), { + body, + files, + query, + auth: false, + }); if (!this.client.channels) return d; return this.client.channels.cache.get(d.channel_id)?.messages._add(d, false) ?? new (getMessage())(this.client, d); @@ -349,7 +367,10 @@ class Webhook { * @returns {Promise} */ async delete(reason) { - await this.client.rest.delete(Routes.webhook(this.id, this.token), { reason, auth: !this.token }); + await this.client.rest.delete(Routes.webhook(this.id, this.token), { + reason, + auth: !this.token, + }); } /** diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 211c15268494..e1918f124747 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -3179,6 +3179,7 @@ export class Webhook extends WebhookMixin() { public token: string | null; public type: WebhookType; public applicationId: Snowflake | null; + public get channel(): TextChannel | VoiceChannel | NewsChannel | ForumChannel | null; public isUserCreated(): this is this & { type: WebhookType.Incoming; applicationId: null;