feat: upload debug files

This commit is contained in:
Jozef Steinhübl 2024-04-11 18:59:14 +02:00
parent b18eff081c
commit 791a779715
3 changed files with 62 additions and 25 deletions

View file

@ -6,7 +6,7 @@ import {
RouteBases, RouteBases,
Routes, Routes,
} from "discord-api-types/v10"; } from "discord-api-types/v10";
import respond from "../../utils/respond"; import respond, { respondAttachments } from "../../utils/respond";
import { Env } from "../../types"; import { Env } from "../../types";
export class Context { export class Context {
@ -23,6 +23,7 @@ export class Context {
} }
public respond = respond; public respond = respond;
public respondAttachments = respondAttachments;
public async editReply(content: APIInteractionResponseCallbackData) { public async editReply(content: APIInteractionResponseCallbackData) {
return await fetch( return await fetch(

View file

@ -5,3 +5,23 @@ export default function (response: APIInteractionResponse) {
headers: { "content-type": "application/json" }, headers: { "content-type": "application/json" },
}); });
} }
export function respondAttachments(
response: APIInteractionResponse,
attachments: Record<string, string>,
) {
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);
}

View file

@ -161,19 +161,25 @@ export default async function (ctx: Context, data: Data) {
} = await res.json(); } = await res.json();
const errors = parseErrors(json?.errors ?? {}); const errors = parseErrors(json?.errors ?? {});
return ctx.respond({ return ctx.respondAttachments(
type: InteractionResponseType.ChannelMessageWithSource, {
data: { type: InteractionResponseType.ChannelMessageWithSource,
content: `Error: ${json.message} (${json.code})${ data: {
errors.length > 0 content: `Error: ${json.message} (${json.code})${
? `\n${errors errors.length > 0
.map((e) => ` - ${e.message} (${e.code})`) ? `\n${errors
.join("\n")}` .map((e) => ` - ${e.message} (${e.code})`)
: "" .join("\n")}`
}`, : ""
flags: MessageFlags.Ephemeral, }`,
flags: MessageFlags.Ephemeral,
},
}, },
}); {
"data.json": JSON.stringify(data, null, "\t"),
"payload.json": JSON.stringify(payload, null, "\t"),
},
);
} }
return ctx.respond({ return ctx.respond({
@ -221,19 +227,29 @@ export default async function (ctx: Context, data: Data) {
} = await res.json(); } = await res.json();
const errors = parseErrors(json?.errors ?? {}); const errors = parseErrors(json?.errors ?? {});
return ctx.respond({ // @ts-expect-error
type: InteractionResponseType.ChannelMessageWithSource, // rome-ignore lint/performance/noDelete: <explanation>
data: { delete data.webhook;
content: `Error: ${json.message} (${json.code})${
errors.length > 0 return ctx.respondAttachments(
? `\n${errors {
.map((e) => ` - ${e.message} (${e.code})`) type: InteractionResponseType.ChannelMessageWithSource,
.join("\n")}` data: {
: "" content: `Error: ${json.message} (${json.code})${
}`, errors.length > 0
flags: MessageFlags.Ephemeral, ? `\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({ return ctx.respond({