From dff87e4b5e0ba37f7f299cbc942dc595396d9b1c Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Sun, 21 Apr 2024 12:21:27 -0400 Subject: [PATCH] feat: interactions.reply method --- src/common/shorters/interaction.ts | 26 ++++++++++++++++++-------- src/structures/Interaction.ts | 21 ++++----------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/common/shorters/interaction.ts b/src/common/shorters/interaction.ts index 1c572de..5d7057e 100644 --- a/src/common/shorters/interaction.ts +++ b/src/common/shorters/interaction.ts @@ -1,14 +1,24 @@ -import { BaseInteraction, type RawFile, WebhookMessage, resolveFiles } from '../..'; +import { BaseInteraction, type RawFile, WebhookMessage, resolveFiles, type ReplyInteractionBody } from '../..'; import type { InteractionMessageUpdateBodyRequest, MessageWebhookCreateBodyRequest } from '../types/write'; import { BaseShorter } from './base'; export class InteractionShorter extends BaseShorter { - protected get appId() { - return this.client.applicationId; + async reply(id: string, token: string, body: ReplyInteractionBody) { + //@ts-expect-error + const { files, ...data } = body.data ?? {}; + return this.client.proxy + .interactions(id)(token) + .callback.post({ + body: BaseInteraction.transformBodyRequest({ + type: body.type, + data, + }), + files: files ? await resolveFiles(files) : undefined, + }); } fetchResponse(token: string, messageId: string) { - return this.client.webhooks.fetchMessage(this.appId, token, messageId); + return this.client.webhooks.fetchMessage(this.client.applicationId, token, messageId); } fetchOriginal(token: string) { @@ -18,7 +28,7 @@ export class InteractionShorter extends BaseShorter { async editMessage(token: string, messageId: string, body: InteractionMessageUpdateBodyRequest) { const { files, ...data } = body; const apiMessage = await this.client.proxy - .webhooks(this.appId)(token) + .webhooks(this.client.applicationId)(token) .messages(messageId) .patch({ body: BaseInteraction.transformBody(data), @@ -33,7 +43,7 @@ export class InteractionShorter extends BaseShorter { deleteResponse(interactionId: string, token: string, messageId: string) { return this.client.proxy - .webhooks(this.appId)(token) + .webhooks(this.client.applicationId)(token) .messages(messageId) .delete() .then(() => this.client.components?.onMessageDelete(messageId === '@original' ? interactionId : messageId)); @@ -46,11 +56,11 @@ export class InteractionShorter extends BaseShorter { async followup(token: string, { files, ...body }: MessageWebhookCreateBodyRequest) { files = files ? await resolveFiles(files) : undefined; const apiMessage = await this.client.proxy - .webhooks(this.appId)(token) + .webhooks(this.client.applicationId)(token) .post({ body: BaseInteraction.transformBody(body), files: files as RawFile[] | undefined, }); - return new WebhookMessage(this.client, apiMessage, this.appId, token); + return new WebhookMessage(this.client, apiMessage, this.client.applicationId, token); } } diff --git a/src/structures/Interaction.ts b/src/structures/Interaction.ts index 07f3694..5a76b0f 100644 --- a/src/structures/Interaction.ts +++ b/src/structures/Interaction.ts @@ -38,7 +38,7 @@ import { } from 'discord-api-types/v10'; import { mix } from 'ts-mixer'; import type { RawFile } from '../api'; -import { ActionRow, Embed, Modal, resolveAttachment, resolveFiles } from '../builders'; +import { ActionRow, Embed, Modal, resolveAttachment } from '../builders'; import { OptionResolver, type ContextOptionsResolved, type UsingClient } from '../commands'; import type { ObjectToLower, OmitInsert, ToClass, When } from '../common'; import type { @@ -170,32 +170,19 @@ export class BaseInteraction< } as T; } - private matchReplied(data: ReplyInteractionBody, type: InteractionResponseType, filesParsed: RawFile[] | undefined) { - this.replied = (this.__reply ?? this.api.interactions(this.id)(this.token).callback.post)({ - // @ts-expect-error - body: BaseInteraction.transformBodyRequest({ data, type }), - files: filesParsed, - }).then(() => (this.replied = true)); + private matchReplied(data: ReplyInteractionBody) { + return (this.replied = this.client.interactions.reply(this.id, this.token, data).then(() => (this.replied = true))); } async reply(body: ReplyInteractionBody) { if (this.replied) { throw new Error('Interaction already replied'); } - - // @ts-expect-error - if (body.data?.files) { - // @ts-expect-error - const { files, ...rest } = body.data; - - this.matchReplied(rest, body.type, await resolveFiles(files)); - // @ts-expect-error - } else this.matchReplied(body.data, body.type); + await this.matchReplied(body); // @ts-expect-error if (body.data instanceof Modal && body.data.__exec) // @ts-expect-error this.client.components.modals.set(this.user.id, (body.data as Modal).__exec); - await this.replied; } deferReply(flags?: MessageFlags) {