Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Webhook): add channel property #8812

Merged
merged 5 commits into from Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions packages/discord.js/src/structures/Webhook.js
Expand Up @@ -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) {
/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -140,7 +144,14 @@ class Webhook {
* @property {Snowflake} [threadId] The id of the thread this message belongs to
* <info>For interaction webhooks, this property is ignored</info>
*/

/**
MrMythicalYT marked this conversation as resolved.
Show resolved Hide resolved
* The channel that this webhook belongs to
MrMythicalYT marked this conversation as resolved.
Show resolved Hide resolved
* @type {?(TextChannel|VoiceChannel|NewsChannel|ForumChannel)}
* @readonly
*/
get channel() {
return this.client.channels.resolve(this.channelId);
}
MrMythicalYT marked this conversation as resolved.
Show resolved Hide resolved
/**
* Sends a message with this webhook.
* @param {string|MessagePayload|WebhookCreateMessageOptions} options The options to provide
Expand Down Expand Up @@ -206,7 +217,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);
Expand Down Expand Up @@ -349,7 +365,10 @@ class Webhook {
* @returns {Promise<void>}
*/
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,
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -2908,6 +2908,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;
Expand Down