From ddc6dab0db5e84e376b2afe25650ce850078444e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Wed, 26 Jun 2024 18:40:19 -0400 Subject: [PATCH] fix: embeds (#215) --- src/structures/Interaction.ts | 44 ++++++++++++++--------------------- src/structures/channels.ts | 28 ++++++++++++---------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/structures/Interaction.ts b/src/structures/Interaction.ts index 629f15b..bcba2af 100644 --- a/src/structures/Interaction.ts +++ b/src/structures/Interaction.ts @@ -133,26 +133,10 @@ export class BaseInteraction< return body; case InteractionResponseType.ChannelMessageWithSource: case InteractionResponseType.UpdateMessage: { - const poll = (body as InteractionCreateBodyRequest).poll; return { type: body.type, //@ts-ignore - data: { - //@ts-ignore - allowed_mentions: self.options?.allowedMentions, - ...(body.data ?? {}), - //@ts-ignore - components: body.data?.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)), - embeds: body.data?.embeds?.map(x => (x instanceof Embed ? x.toJSON() : x)), - attachments: - body.data && 'attachments' in body.data - ? body.data.attachments?.map((x, i) => ({ id: i, ...resolveAttachment(x) })) - : (files?.map((x, id) => ({ - id, - filename: x.name, - })) as RESTAPIAttachment[]), - poll: poll ? (poll instanceof PollBuilder ? poll.toJSON() : poll) : undefined, - }, + data: BaseInteraction.transformBodyRequest(body.data ?? {}, files, self), }; } case InteractionResponseType.Modal: @@ -188,20 +172,28 @@ export class BaseInteraction< ) { const poll = (body as MessageWebhookCreateBodyRequest).poll; - return { + const allow = { allowed_mentions: self.options?.allowedMentions, - attachments: - 'attachments' in body - ? body.attachments?.map((x, i) => ({ id: i, ...resolveAttachment(x) })) - : (files?.map((x, id) => ({ - id, - filename: x.name, - })) as RESTAPIAttachment[]), + ...body, components: body.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)), embeds: body?.embeds?.map(x => (x instanceof Embed ? x.toJSON() : x)), poll: poll ? (poll instanceof PollBuilder ? poll.toJSON() : poll) : undefined, - } as T; + }; + + if ('attachment' in body) { + allow.attachments = + body.attachments?.map((x, i) => ({ + id: i, + ...resolveAttachment(x), + })) ?? undefined; + } else if (files?.length) { + allow.attachments = files?.map((x, id) => ({ + id, + filename: x.name, + })) as RESTAPIAttachment[]; + } + return allow as unknown as T; } private async matchReplied(body: ReplyInteractionBody) { diff --git a/src/structures/channels.ts b/src/structures/channels.ts index c25fff6..73be71c 100644 --- a/src/structures/channels.ts +++ b/src/structures/channels.ts @@ -280,23 +280,27 @@ export class MessagesMethods extends DiscordBase { self: UsingClient, ) { const poll = (body as MessageCreateBodyRequest).poll; - return { + const allow = { allowed_mentions: self.options?.allowedMentions, ...body, components: body.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined, embeds: body.embeds?.map(x => (x instanceof Embed ? x.toJSON() : x)) ?? undefined, - attachments: - 'attachments' in body - ? body.attachments?.map((x, i) => ({ - id: i, - ...resolveAttachment(x), - })) ?? undefined - : (files?.map((x, id) => ({ - id, - filename: x.name, - })) as RESTAPIAttachment[]), poll: poll ? (poll instanceof PollBuilder ? poll.toJSON() : poll) : undefined, - } as T; + }; + + if ('attachment' in body) { + allow.attachments = + body.attachments?.map((x, i) => ({ + id: i, + ...resolveAttachment(x), + })) ?? undefined; + } else if (files?.length) { + allow.attachments = files?.map((x, id) => ({ + id, + filename: x.name, + })) as RESTAPIAttachment[]; + } + return allow as unknown as T; } }