From e514b4a8cc78dfbfa2b6bd3f0897fdf8c53f4160 Mon Sep 17 00:00:00 2001 From: Tarrgon <61888458+Tarrgon@users.noreply.github.com> Date: Thu, 26 Jun 2025 20:39:20 -0400 Subject: [PATCH] Add debug logs --- .env.sample | 3 ++- src/config.ts | 5 +++-- src/utils/debug-utils.ts | 5 +++++ src/webserver/index.ts | 14 +++++++++++++- 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 src/utils/debug-utils.ts diff --git a/.env.sample b/.env.sample index 319afdb..15a36e8 100644 --- a/.env.sample +++ b/.env.sample @@ -7,4 +7,5 @@ LINK_SECRET=super_secret_for_url_discord E621_BASE_URL=https://e621.net E926_BASE_URL=https://e926.net REDIS_URL=localhost:6379 -PORT=8000 \ No newline at end of file +PORT=8000 +DEBUG=false \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index d132e4a..93dcaef 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,7 +2,7 @@ import dotenv from 'dotenv'; dotenv.config(); -const { DISCORD_TOKEN, DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET, DISCORD_GUILD_ID, RELEASE_SECRET, LINK_SECRET, E621_BASE_URL, E926_BASE_URL, REDIS_URL, PORT } = process.env; +const { DISCORD_TOKEN, DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET, DISCORD_GUILD_ID, RELEASE_SECRET, LINK_SECRET, E621_BASE_URL, E926_BASE_URL, REDIS_URL, PORT, DEBUG } = process.env; export const config = { DISCORD_TOKEN, @@ -15,7 +15,8 @@ export const config = { E926_BASE_URL, PORT: parseInt(PORT as string), REDIS_URL, - DEV_MODE: process.env.npm_lifecycle_event == 'dev' + DEV_MODE: process.env.npm_lifecycle_event == 'dev', + DEBUG: DEBUG == 'true' }; for (const [key, val] of Object.entries(config)) { diff --git a/src/utils/debug-utils.ts b/src/utils/debug-utils.ts new file mode 100644 index 0000000..5a56de7 --- /dev/null +++ b/src/utils/debug-utils.ts @@ -0,0 +1,5 @@ +import { config } from '../config'; + +export function logDebug(message: string) { + if (config.DEBUG) console.log(`[DEBUG] ${message}`); +} \ No newline at end of file diff --git a/src/webserver/index.ts b/src/webserver/index.ts index 30e0fc4..3188f0e 100644 --- a/src/webserver/index.ts +++ b/src/webserver/index.ts @@ -10,6 +10,7 @@ import path from 'path'; import { Client } from 'discord.js'; import bodyParser from 'body-parser'; import { fixPings } from '../utils/github-user-utils'; +import { logDebug } from '../utils/debug-utils'; declare module 'express-session' { interface SessionData { @@ -152,16 +153,20 @@ function render(res: Response, code: number, title: string = '', message: string } async function handleGithubRelease(client: Client, req: Request, res: Response): Promise { + logDebug('Received github release webhook'); const signature = (req.headers['x-hub-signature-256'] as string).split('=')[1]; const computedSignature = crypto.createHmac('sha256', config.RELEASE_SECRET!).update(req.body).digest('hex'); if (signature !== computedSignature) { + console.error('Github release webhook signature mismatch'); return res.sendStatus(401); } res.sendStatus(200); const data = JSON.parse(req.body); + logDebug(`Release webhook data:\n${JSON.stringify(data, null, 4)}`); + if (data.action != 'published' || data.repository.id != GITHUB_REPO_ID) return; const settings = await Database.getGuildSettings(config.DISCORD_GUILD_ID!); @@ -170,7 +175,10 @@ async function handleGithubRelease(client: Client, req: Request, res: Response): const channel = await client.channels.fetch(settings.github_release_channel); - if (!channel || !channel.isSendable()) return; + if (!channel || !channel.isSendable()) { + console.error(`Github release channel ${channel ? 'sendable' : 'found'}`); + return; + } const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; @@ -178,8 +186,12 @@ async function handleGithubRelease(client: Client, req: Request, res: Response): const message = `## [${months[date.getUTCMonth()]} ${date.getUTCDate()}, ${date.getUTCFullYear()}](<${data.release.html_url}>)\n\n${await fixPings(data.release.body)}`; + logDebug('Sending github release message'); + const sentMessage = await channel.send(message); await sentMessage.startThread({ name: data.release.tag_name }); + + logDebug('Github webhook processed'); } export function initializeWebserver(client: Client) {