Add debug logs
This commit is contained in:
@@ -8,3 +8,4 @@ E621_BASE_URL=https://e621.net
|
||||
E926_BASE_URL=https://e926.net
|
||||
REDIS_URL=localhost:6379
|
||||
PORT=8000
|
||||
DEBUG=false
|
||||
+3
-2
@@ -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)) {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { config } from '../config';
|
||||
|
||||
export function logDebug(message: string) {
|
||||
if (config.DEBUG) console.log(`[DEBUG] ${message}`);
|
||||
}
|
||||
+13
-1
@@ -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<any> {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user