From d54bf5d286f4057db130901591b192fd4d1668c1 Mon Sep 17 00:00:00 2001 From: pat <73502164+nyapat@users.noreply.github.com> Date: Fri, 17 Jun 2022 16:25:46 -0500 Subject: [PATCH] fix(webhooks): revert webhook caching (and returning Message) (#8038) --- .../src/structures/InteractionWebhook.js | 18 +++++++++++++++- packages/discord.js/src/structures/Webhook.js | 21 ++++++++++++------- packages/discord.js/typings/index.d.ts | 18 +++++++++------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/packages/discord.js/src/structures/InteractionWebhook.js b/packages/discord.js/src/structures/InteractionWebhook.js index ddafbf0a2..977eb37bb 100644 --- a/packages/discord.js/src/structures/InteractionWebhook.js +++ b/packages/discord.js/src/structures/InteractionWebhook.js @@ -29,10 +29,26 @@ class InteractionWebhook { /** * Sends a message with this webhook. * @param {string|MessagePayload|InteractionReplyOptions} options The content for the reply - * @returns {Promise} + * @returns {Promise} */ + send() {} + + /** + * Gets a message that was sent by this webhook. + * @param {Snowflake|'@original'} message The id of the message to fetch + * @returns {Promise} Returns the message sent by this webhook + */ + fetchMessage() {} + + /** + * Edits a message that was sent by this webhook. + * @param {MessageResolvable|'@original'} message The message to edit + * @param {string|MessagePayload|WebhookEditMessageOptions} options The options to provide + * @returns {Promise} Returns the message edited by this webhook + */ + editMessage() {} deleteMessage() {} get url() {} diff --git a/packages/discord.js/src/structures/Webhook.js b/packages/discord.js/src/structures/Webhook.js index a19ee2571..eb75b8511 100644 --- a/packages/discord.js/src/structures/Webhook.js +++ b/packages/discord.js/src/structures/Webhook.js @@ -148,7 +148,7 @@ class Webhook { /** * Sends a message with this webhook. * @param {string|MessagePayload|WebhookMessageOptions} options The options to provide - * @returns {Promise} + * @returns {Promise} * @example * // Send a basic message * webhook.send('hello!') @@ -211,7 +211,9 @@ 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 }); - return this.client.channels?.cache.get(d.channel_id)?.messages._add(d, false) ?? new (getMessage())(this.client, d); + + if (!this.client.channels) return d; + return this.client.channels.cache.get(d.channel_id)?.messages._add(d, false) ?? new (getMessage())(this.client, d); } /** @@ -286,17 +288,19 @@ class Webhook { * Gets a message that was sent by this webhook. * @param {Snowflake|'@original'} message The id of the message to fetch * @param {WebhookFetchMessageOptions} [options={}] The options to provide to fetch the message. - * @returns {Promise} Returns the message sent by this webhook + * @returns {Promise} Returns the message sent by this webhook */ - async fetchMessage(message, { cache = true, threadId } = {}) { + async fetchMessage(message, { threadId } = {}) { if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); const data = await this.client.rest.get(Routes.webhookMessage(this.id, this.token, message), { query: threadId ? makeURLSearchParams({ thread_id: threadId }) : undefined, auth: false, }); + + if (!this.client.channels) return data; return ( - this.client.channels?.cache.get(data.channel_id)?.messages._add(data, cache) ?? + this.client.channels.cache.get(data.channel_id)?.messages._add(data, false) ?? new (getMessage())(this.client, data) ); } @@ -305,7 +309,7 @@ class Webhook { * Edits a message that was sent by this webhook. * @param {MessageResolvable|'@original'} message The message to edit * @param {string|MessagePayload|WebhookEditMessageOptions} options The options to provide - * @returns {Promise} Returns the message edited by this webhook + * @returns {Promise} Returns the message edited by this webhook */ async editMessage(message, options) { if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); @@ -329,7 +333,10 @@ class Webhook { }, ); - const messageManager = this.client.channels?.cache.get(d.channel_id)?.messages; + const channelManager = this.client.channels; + if (!channelManager) return d; + + const messageManager = channelManager.cache.get(d.channel_id)?.messages; if (!messageManager) return new (getMessage())(this.client, d); const existing = messageManager.cache.get(d.id); diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index da9532b40..0e1bf0d9f 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1564,6 +1564,11 @@ export class InteractionWebhook extends PartialWebhookMixin() { public constructor(client: Client, id: Snowflake, token: string); public token: string; public send(options: string | MessagePayload | InteractionReplyOptions): Promise; + public editMessage( + message: MessageResolvable, + options: string | MessagePayload | WebhookEditMessageOptions, + ): Promise; + public fetchMessage(message: string): Promise; } export class Invite extends Base { @@ -2801,9 +2806,9 @@ export class WebhookClient extends WebhookMixin(BaseClient) { public editMessage( message: MessageResolvable, options: string | MessagePayload | WebhookEditMessageOptions, - ): Promise; - public fetchMessage(message: Snowflake, options?: WebhookFetchMessageOptions): Promise; - public send(options: string | MessagePayload | WebhookMessageOptions): Promise; + ): Promise; + public fetchMessage(message: Snowflake, options?: WebhookFetchMessageOptions): Promise; + public send(options: string | MessagePayload | WebhookMessageOptions): Promise; } export class WebSocketManager extends EventEmitter { @@ -3488,9 +3493,9 @@ export interface PartialWebhookFields { editMessage( message: MessageResolvable | '@original', options: string | MessagePayload | WebhookEditMessageOptions, - ): Promise; - fetchMessage(message: Snowflake | '@original', options?: WebhookFetchMessageOptions): Promise; - send(options: string | MessagePayload | Omit): Promise; + ): Promise; + fetchMessage(message: Snowflake | '@original', options?: WebhookFetchMessageOptions): Promise; + send(options: string | MessagePayload | Omit): Promise; } export interface WebhookFields extends PartialWebhookFields { @@ -5320,7 +5325,6 @@ export type WebhookEditMessageOptions = Pick< >; export interface WebhookFetchMessageOptions { - cache?: boolean; threadId?: Snowflake; }