From 107822d28d9de04f6c14fea74647c46fe2fd1016 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Sat, 2 Oct 2021 12:42:25 +0100 Subject: [PATCH] feat: Allow webhooks to fetch, edit and delete messages in threads (#6695) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/structures/Webhook.js | 47 +++++++++++++++++++++++++++++++++------ typings/index.d.ts | 17 ++++++++++++-- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index fb7991f16..31db5249a 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -105,6 +105,8 @@ class Webhook { * @property {MessageAttachment[]} [attachments] Attachments to send with the message * @property {MessageActionRow[]|MessageActionRowOptions[]} [components] * Action rows containing interactive components for the message (buttons, select menus) + * @property {Snowflake} [threadId] The id of the thread this message belongs to + * For interaction webhooks, this property is ignored */ /** @@ -235,18 +237,38 @@ class Webhook { return this; } + /** + * Options that can be passed into fetchMessage. + * @typedef {options} WebhookFetchMessageOptions + * @property {boolean} [cache=true] Whether to cache the message. + * @property {Snowflake} [threadId] The id of the thread this message belongs to. + * For interaction webhooks, this property is ignored + */ + /** * Gets a message that was sent by this webhook. * @param {Snowflake|'@original'} message The id of the message to fetch - * @param {boolean} [cache=true] Whether to cache the message + * @param {WebhookFetchMessageOptions|boolean} [cacheOrOptions={}] The options to provide to fetch the message. + * A **deprecated** boolean may be passed instead to specify whether to cache the message. * @returns {Promise} Returns the raw message data if the webhook was instantiated as a * {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned */ - async fetchMessage(message, cache = true) { + async fetchMessage(message, cacheOrOptions = { cache: true }) { + if (typeof cacheOrOptions === 'boolean') { + cacheOrOptions = { cache: cacheOrOptions }; + } + if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); - const data = await this.client.api.webhooks(this.id, this.token).messages(message).get(); - return this.client.channels?.cache.get(data.channel_id)?.messages._add(data, cache) ?? data; + const data = await this.client.api + .webhooks(this.id, this.token) + .messages(message) + .get({ + query: { + thread_id: cacheOrOptions.threadId, + }, + }); + return this.client.channels?.cache.get(data.channel_id)?.messages._add(data, cacheOrOptions.cache) ?? data; } /** @@ -269,7 +291,13 @@ class Webhook { const d = await this.client.api .webhooks(this.id, this.token) .messages(typeof message === 'string' ? message : message.id) - .patch({ data, files }); + .patch({ + data, + files, + query: { + thread_id: messagePayload.options.threadId, + }, + }); const messageManager = this.client.channels?.cache.get(d.channel_id)?.messages; if (!messageManager) return d; @@ -294,15 +322,20 @@ class Webhook { /** * Delete a message that was sent by this webhook. * @param {MessageResolvable|'@original'} message The message to delete + * @param {Snowflake} [threadId] The id of the thread this message belongs to * @returns {Promise} */ - async deleteMessage(message) { + async deleteMessage(message, threadId) { if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); await this.client.api .webhooks(this.id, this.token) .messages(typeof message === 'string' ? message : message.id) - .delete(); + .delete({ + query: { + thread_id: threadId, + }, + }); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index 9770d48bc..81409da77 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2122,7 +2122,11 @@ export class WebhookClient extends WebhookMixin(BaseClient) { message: MessageResolvable, options: string | MessagePayload | WebhookEditMessageOptions, ): Promise; + public fetchMessage(message: Snowflake, options?: WebhookFetchMessageOptions): Promise; + /* tslint:disable:unified-signatures */ + /** @deprecated */ public fetchMessage(message: Snowflake, cache?: boolean): Promise; + /* tslint:enable:unified-signatures */ public send(options: string | MessagePayload | WebhookMessageOptions): Promise; } @@ -2836,12 +2840,16 @@ export function WebhookMixin(Base?: Constructable): Constructable; + deleteMessage(message: MessageResolvable | APIMessage | '@original', threadId?: Snowflake): Promise; editMessage( message: MessageResolvable | '@original', options: string | MessagePayload | WebhookEditMessageOptions, ): Promise; + fetchMessage(message: Snowflake | '@original', options?: WebhookFetchMessageOptions): Promise; + /* tslint:disable:unified-signatures */ + /** @deprecated */ fetchMessage(message: Snowflake | '@original', cache?: boolean): Promise; + /* tslint:enable:unified-signatures */ send(options: string | MessagePayload | WebhookMessageOptions): Promise; } @@ -4869,9 +4877,14 @@ export interface WebhookEditData { export type WebhookEditMessageOptions = Pick< WebhookMessageOptions, - 'content' | 'embeds' | 'files' | 'allowedMentions' | 'components' | 'attachments' + 'content' | 'embeds' | 'files' | 'allowedMentions' | 'components' | 'attachments' | 'threadId' >; +export interface WebhookFetchMessageOptions { + cache?: boolean; + threadId?: Snowflake; +} + export interface WebhookMessageOptions extends Omit { username?: string; avatarURL?: string;