diff --git a/packages/bot/src/structs/contexts/Context.ts b/packages/bot/src/structs/contexts/Context.ts index 3f1d8eb..fe52e5a 100644 --- a/packages/bot/src/structs/contexts/Context.ts +++ b/packages/bot/src/structs/contexts/Context.ts @@ -6,7 +6,7 @@ import { RouteBases, Routes, } from "discord-api-types/v10"; -import respond from "../../utils/respond"; +import respond, { respondAttachments } from "../../utils/respond"; import { Env } from "../../types"; export class Context { @@ -23,6 +23,7 @@ export class Context { } public respond = respond; + public respondAttachments = respondAttachments; public async editReply(content: APIInteractionResponseCallbackData) { return await fetch( diff --git a/packages/bot/src/utils/respond.ts b/packages/bot/src/utils/respond.ts index 868b00c..8d7d482 100644 --- a/packages/bot/src/utils/respond.ts +++ b/packages/bot/src/utils/respond.ts @@ -5,3 +5,23 @@ export default function (response: APIInteractionResponse) { headers: { "content-type": "application/json" }, }); } + +export function respondAttachments( + response: APIInteractionResponse, + attachments: Record, +) { + const body = new FormData(); + body.append("payload_json", JSON.stringify(response)); + + let i = 0; + for (const [name, content] of Object.entries(attachments)) { + body.append( + `files[${i}]`, + new Blob([content], { type: "text/plain" }), + name, + ); + i++; + } + + return new Response(body); +} diff --git a/packages/bot/src/utils/sendFinal.ts b/packages/bot/src/utils/sendFinal.ts index e02afa2..817a023 100644 --- a/packages/bot/src/utils/sendFinal.ts +++ b/packages/bot/src/utils/sendFinal.ts @@ -161,19 +161,25 @@ export default async function (ctx: Context, data: Data) { } = await res.json(); const errors = parseErrors(json?.errors ?? {}); - return ctx.respond({ - type: InteractionResponseType.ChannelMessageWithSource, - data: { - content: `Error: ${json.message} (${json.code})${ - errors.length > 0 - ? `\n${errors - .map((e) => ` - ${e.message} (${e.code})`) - .join("\n")}` - : "" - }`, - flags: MessageFlags.Ephemeral, + return ctx.respondAttachments( + { + type: InteractionResponseType.ChannelMessageWithSource, + data: { + content: `Error: ${json.message} (${json.code})${ + errors.length > 0 + ? `\n${errors + .map((e) => ` - ${e.message} (${e.code})`) + .join("\n")}` + : "" + }`, + flags: MessageFlags.Ephemeral, + }, }, - }); + { + "data.json": JSON.stringify(data, null, "\t"), + "payload.json": JSON.stringify(payload, null, "\t"), + }, + ); } return ctx.respond({ @@ -221,19 +227,29 @@ export default async function (ctx: Context, data: Data) { } = await res.json(); const errors = parseErrors(json?.errors ?? {}); - return ctx.respond({ - type: InteractionResponseType.ChannelMessageWithSource, - data: { - content: `Error: ${json.message} (${json.code})${ - errors.length > 0 - ? `\n${errors - .map((e) => ` - ${e.message} (${e.code})`) - .join("\n")}` - : "" - }`, - flags: MessageFlags.Ephemeral, + // @ts-expect-error + // rome-ignore lint/performance/noDelete: + delete data.webhook; + + return ctx.respondAttachments( + { + type: InteractionResponseType.ChannelMessageWithSource, + data: { + content: `Error: ${json.message} (${json.code})${ + errors.length > 0 + ? `\n${errors + .map((e) => ` - ${e.message} (${e.code})`) + .join("\n")}` + : "" + }`, + flags: MessageFlags.Ephemeral, + }, }, - }); + { + "data.json": JSON.stringify(data, null, "\t"), + "payload.json": JSON.stringify(payload, null, "\t"), + }, + ); } return ctx.respond({