From 928175abb0ab78ee4beeb822fe9a5e0cf1fc96c2 Mon Sep 17 00:00:00 2001 From: Lea Date: Sat, 29 Apr 2023 08:47:57 +0200 Subject: [PATCH] discord webhook logging --- src/index.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 66c149d..45b70bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { exec } from 'child_process'; config(); -const { DISCORD_TOKEN, REVOLT_TOKEN, DISCORD_CHANNEL, REVOLT_CHANNEL, PORT, RESTART_COMMAND_BOT, RESTART_COMMAND_BRIDGE } = process.env; +const { DISCORD_TOKEN, REVOLT_TOKEN, DISCORD_CHANNEL, REVOLT_CHANNEL, PORT, RESTART_COMMAND_BOT, RESTART_COMMAND_BRIDGE, RESTART_WEBHOOK } = process.env; if (!DISCORD_TOKEN) throw '$DISCORD_TOKEN not set'; if (!REVOLT_TOKEN) throw '$REVOLT_TOKEN not set'; @@ -76,7 +76,9 @@ const periodicCheck = async () => { return response; } +const whClient = RESTART_WEBHOOK ? new Discord.WebhookClient({ url: RESTART_WEBHOOK }) : null; const runRestarts = async (status: Awaited>) => { + if (!status.watchdog.bot && !restarting.bot && RESTART_COMMAND_BOT) { console.log('Restarting bot'); try { @@ -84,12 +86,16 @@ const runRestarts = async (status: Awaited>) => const result = exec(RESTART_COMMAND_BOT!); result.stdout?.on('data', data => console.log(data)); result.stderr?.on('data', data => console.log(data)); - result.on('exit', code => { + result.on('exit', async (code) => { if (code != 0) { console.log('Bot: Restart command seems to have failed!'); restarting.bot = false; + await whClient?.send(`Bot restart command failed with status ${code}`); } + else await whClient?.send(`Bot restart command finished successfully!`); }); + + await whClient?.send('Restarting bot'); } catch(e) { console.log(e); } @@ -102,12 +108,16 @@ const runRestarts = async (status: Awaited>) => const result = exec(RESTART_COMMAND_BRIDGE!); result.stdout?.on('data', data => console.log(data)); result.stderr?.on('data', data => console.log(data)); - result.on('exit', code => { + result.on('exit', async (code) => { if (code != 0) { console.log('Bridge: Restart command seems to have failed!'); restarting.bridge = false; + await whClient?.send(`Bridge restart command failed with status ${code}`); } + else await whClient?.send(`Bridge restart command finished successfully!`); }); + + await whClient?.send('Restarting bridge'); } catch(e) { console.log(e); }