feat: enhance logging for Discord interactions with detailed user information
This commit is contained in:
parent
fa0584c550
commit
de09a84daa
@ -1,10 +1,11 @@
|
|||||||
import { Config } from "../config";
|
import { Config } from "../config";
|
||||||
|
import { logger } from "../logger";
|
||||||
|
|
||||||
export const fetchDiscord = async (
|
export const fetchDiscord = async (
|
||||||
path: string,
|
path: string,
|
||||||
init?: RequestInit,
|
init?: RequestInit,
|
||||||
): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
const response = await fetch(`${Config.DISCORD_API}${path}`, {
|
let response = await fetch(`${Config.DISCORD_API}${path}`, {
|
||||||
...init,
|
...init,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bot ${Config.BOT_TOKEN}`,
|
Authorization: `Bot ${Config.BOT_TOKEN}`,
|
||||||
@ -12,10 +13,22 @@ export const fetchDiscord = async (
|
|||||||
...(init?.headers || {}),
|
...(init?.headers || {}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.status === 429) {
|
if (response.status === 429) {
|
||||||
const data = await response.json().catch(() => ({}) as any);
|
const data = await response.json().catch(() => ({}) as any);
|
||||||
const retry = (data?.retry_after ? Number(data.retry_after) : 1) * 1000;
|
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));
|
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;
|
return response;
|
||||||
};
|
};
|
||||||
|
|||||||
10
src/index.ts
10
src/index.ts
@ -132,7 +132,15 @@ app.post("/interactions", async (c) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const interaction = JSON.parse(raw);
|
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;
|
const { type } = interaction;
|
||||||
|
|
||||||
if (type === 1) {
|
if (type === 1) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user