feat: enhance logging for Discord interactions with detailed user information

This commit is contained in:
Sosokker 2025-09-02 21:20:37 +07:00
parent fa0584c550
commit de09a84daa
2 changed files with 23 additions and 2 deletions

View File

@ -1,10 +1,11 @@
import { Config } from "../config";
import { logger } from "../logger";
export const fetchDiscord = async (
path: string,
init?: RequestInit,
): Promise<Response> => {
const response = await fetch(`${Config.DISCORD_API}${path}`, {
let response = await fetch(`${Config.DISCORD_API}${path}`, {
...init,
headers: {
Authorization: `Bot ${Config.BOT_TOKEN}`,
@ -12,10 +13,22 @@ export const fetchDiscord = async (
...(init?.headers || {}),
},
});
if (response.status === 429) {
const data = await response.json().catch(() => ({}) as any);
const retry = (data?.retry_after ? Number(data.retry_after) : 1) * 1000;
logger.warn({ path, retry }, `Rate limited. Retrying after ${retry}ms.`);
await new Promise((r) => setTimeout(r, retry));
response = await fetch(`${Config.DISCORD_API}${path}`, {
...init,
headers: {
Authorization: `Bot ${Config.BOT_TOKEN}`,
"Content-Type": "application/json",
...(init?.headers || {}),
},
});
}
return response;
};

View File

@ -132,7 +132,15 @@ app.post("/interactions", async (c) => {
}
const interaction = JSON.parse(raw);
logger.info({ interaction }, "Received interaction");
logger.info(
{
user: interaction.user.id,
name: interaction.name,
guideId: interaction.channel.guild_id,
channelId: interaction.channel_id,
},
"Received interaction",
);
const { type } = interaction;
if (type === 1) {