Skip to content

Commit decbce4

Browse files
feat(Webhook): add channel property (#8812)
* 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 <kyradiscord@gmail.com> Co-authored-by: Aura Román <kyradiscord@gmail.com>
1 parent 9922151 commit decbce4

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

packages/discord.js/src/structures/Webhook.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ class Webhook {
3939
* @name Webhook#token
4040
* @type {?string}
4141
*/
42-
Object.defineProperty(this, 'token', { value: data.token ?? null, writable: true, configurable: true });
42+
Object.defineProperty(this, 'token', {
43+
value: data.token ?? null,
44+
writable: true,
45+
configurable: true,
46+
});
4347

4448
if ('avatar' in data) {
4549
/**
@@ -73,7 +77,7 @@ class Webhook {
7377

7478
if ('channel_id' in data) {
7579
/**
76-
* The channel the webhook belongs to
80+
* The id of the channel the webhook belongs to
7781
* @type {Snowflake}
7882
*/
7983
this.channelId = data.channel_id;
@@ -141,6 +145,15 @@ class Webhook {
141145
* <info>For interaction webhooks, this property is ignored</info>
142146
*/
143147

148+
/**
149+
* The channel the webhook belongs to
150+
* @type {?(TextChannel|VoiceChannel|NewsChannel|ForumChannel)}
151+
* @readonly
152+
*/
153+
get channel() {
154+
return this.client.channels.resolve(this.channelId);
155+
}
156+
144157
/**
145158
* Sends a message with this webhook.
146159
* @param {string|MessagePayload|WebhookCreateMessageOptions} options The options to provide
@@ -206,7 +219,12 @@ class Webhook {
206219
});
207220

208221
const { body, files } = await messagePayload.resolveFiles();
209-
const d = await this.client.rest.post(Routes.webhook(this.id, this.token), { body, files, query, auth: false });
222+
const d = await this.client.rest.post(Routes.webhook(this.id, this.token), {
223+
body,
224+
files,
225+
query,
226+
auth: false,
227+
});
210228

211229
if (!this.client.channels) return d;
212230
return this.client.channels.cache.get(d.channel_id)?.messages._add(d, false) ?? new (getMessage())(this.client, d);
@@ -349,7 +367,10 @@ class Webhook {
349367
* @returns {Promise<void>}
350368
*/
351369
async delete(reason) {
352-
await this.client.rest.delete(Routes.webhook(this.id, this.token), { reason, auth: !this.token });
370+
await this.client.rest.delete(Routes.webhook(this.id, this.token), {
371+
reason,
372+
auth: !this.token,
373+
});
353374
}
354375

355376
/**

packages/discord.js/typings/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,6 +3179,7 @@ export class Webhook extends WebhookMixin() {
31793179
public token: string | null;
31803180
public type: WebhookType;
31813181
public applicationId: Snowflake | null;
3182+
public get channel(): TextChannel | VoiceChannel | NewsChannel | ForumChannel | null;
31823183
public isUserCreated(): this is this & {
31833184
type: WebhookType.Incoming;
31843185
applicationId: null;

0 commit comments

Comments
 (0)