From 20088e927516e4d47c49e774881711a52ce02a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Fri, 19 Apr 2024 14:37:43 -0400 Subject: [PATCH] feat: Interactions shorters (#187) * feat: threads methods * fix: typing * fix: fixes * feat: interaction shorter * fix: files in followup --- src/client/base.ts | 2 ++ src/common/index.ts | 1 + src/common/shorters/interaction.ts | 56 ++++++++++++++++++++++++++++++ src/structures/Interaction.ts | 31 ++++------------- 4 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 src/common/shorters/interaction.ts diff --git a/src/client/base.ts b/src/client/base.ts index f7efb78..78c9052 100644 --- a/src/client/base.ts +++ b/src/client/base.ts @@ -9,6 +9,7 @@ import { ChannelShorter, EmojiShorter, GuildShorter, + InteractionShorter, LogLevels, Logger, MemberShorter, @@ -52,6 +53,7 @@ export class BaseClient { reactions = new ReactionShorter(this); emojis = new EmojiShorter(this); threads = new ThreadShorter(this); + interactions = new InteractionShorter(this); debugger?: Logger; diff --git a/src/common/index.ts b/src/common/index.ts index 229e420..0eee05b 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -14,6 +14,7 @@ export * from './shorters/templates'; export * from './shorters/users'; export * from './shorters/threads'; export * from './shorters/webhook'; +export * from './shorters/interaction'; export * from './types/options'; export * from './types/resolvables'; export * from './types/util'; diff --git a/src/common/shorters/interaction.ts b/src/common/shorters/interaction.ts new file mode 100644 index 0000000..1c572de --- /dev/null +++ b/src/common/shorters/interaction.ts @@ -0,0 +1,56 @@ +import { BaseInteraction, type RawFile, WebhookMessage, resolveFiles } from '../..'; +import type { InteractionMessageUpdateBodyRequest, MessageWebhookCreateBodyRequest } from '../types/write'; +import { BaseShorter } from './base'; + +export class InteractionShorter extends BaseShorter { + protected get appId() { + return this.client.applicationId; + } + + fetchResponse(token: string, messageId: string) { + return this.client.webhooks.fetchMessage(this.appId, token, messageId); + } + + fetchOriginal(token: string) { + return this.fetchResponse(token, '@original'); + } + + async editMessage(token: string, messageId: string, body: InteractionMessageUpdateBodyRequest) { + const { files, ...data } = body; + const apiMessage = await this.client.proxy + .webhooks(this.appId)(token) + .messages(messageId) + .patch({ + body: BaseInteraction.transformBody(data), + files: files ? await resolveFiles(files) : undefined, + }); + return new WebhookMessage(this.client, apiMessage, this.client.applicationId, token); + } + + editOriginal(token: string, body: InteractionMessageUpdateBodyRequest) { + return this.editMessage(token, '@original', body); + } + + deleteResponse(interactionId: string, token: string, messageId: string) { + return this.client.proxy + .webhooks(this.appId)(token) + .messages(messageId) + .delete() + .then(() => this.client.components?.onMessageDelete(messageId === '@original' ? interactionId : messageId)); + } + + deleteOriginal(interactionId: string, token: string) { + return this.deleteResponse(interactionId, token, '@original'); + } + + async followup(token: string, { files, ...body }: MessageWebhookCreateBodyRequest) { + files = files ? await resolveFiles(files) : undefined; + const apiMessage = await this.client.proxy + .webhooks(this.appId)(token) + .post({ + body: BaseInteraction.transformBody(body), + files: files as RawFile[] | undefined, + }); + return new WebhookMessage(this.client, apiMessage, this.appId, token); + } +} diff --git a/src/structures/Interaction.ts b/src/structures/Interaction.ts index 906c2c9..3f2babd 100644 --- a/src/structures/Interaction.ts +++ b/src/structures/Interaction.ts @@ -325,7 +325,7 @@ export class Interaction< Type extends APIInteraction = APIInteraction, > extends BaseInteraction { fetchMessage(messageId: string) { - return this.client.webhooks.fetchMessage(this.applicationId, this.token, messageId); + return this.client.interactions.fetchResponse(this.token, messageId); } fetchResponse() { @@ -363,16 +363,8 @@ export class Interaction< return this.write(body as InteractionCreateBodyRequest, fetchReply); } - async editMessage(messageId: string, body: InteractionMessageUpdateBodyRequest) { - const { files, ...data } = body; - const apiMessage = await this.api - .webhooks(this.applicationId)(this.token) - .messages(messageId) - .patch({ - body: BaseInteraction.transformBody(data), - files: files ? await resolveFiles(files) : undefined, - }); - return new Message(this.client, apiMessage); + editMessage(messageId: string, body: InteractionMessageUpdateBodyRequest) { + return this.client.interactions.editMessage(this.token, messageId, body); } editResponse(body: InteractionMessageUpdateBodyRequest) { @@ -384,22 +376,11 @@ export class Interaction< } deleteMessage(messageId: string) { - return this.api - .webhooks(this.applicationId)(this.token) - .messages(messageId) - .delete() - .then(() => this.client.components?.onMessageDelete(messageId === '@original' ? this.id : messageId)); + return this.client.interactions.deleteResponse(this.id, this.token, messageId); } - async createResponse({ files, ...body }: MessageWebhookCreateBodyRequest) { - files ??= files ? await resolveFiles(files) : undefined; - const apiMessage = await this.api - .webhooks(this.applicationId)(this.token) - .post({ - body: BaseInteraction.transformBody(body), - files: files as RawFile[] | undefined, - }); - return new Message(this.client, apiMessage); + async followup(body: MessageWebhookCreateBodyRequest) { + return this.client.interactions.followup(this.token, body); } }