From e718827432a32babbb8870d0f3a3c8f0a387ab77 Mon Sep 17 00:00:00 2001 From: janderedev Date: Tue, 4 Jan 2022 15:14:46 +0100 Subject: [PATCH] add basic logging --- src/bot/logging.ts | 42 +++++++++++++++++++++++++++++++++++++ src/struct/AutomodClient.ts | 2 ++ 2 files changed, 44 insertions(+) create mode 100644 src/bot/logging.ts diff --git a/src/bot/logging.ts b/src/bot/logging.ts new file mode 100644 index 0000000..6c4767a --- /dev/null +++ b/src/bot/logging.ts @@ -0,0 +1,42 @@ +import { ColorResolvable, MessageEmbed, WebhookClient } from "discord.js"; +import logger from "./logger"; +import { client as bot } from '../index' + +let client: WebhookClient|undefined; + +if (process.env.LOG_WEBHOOK) { + try { + client = new WebhookClient({ url: process.env.LOG_WEBHOOK }); + } catch(e) { + console.error(e); + } +} + +async function adminBotLog(data: { message: string, type: 'INFO'|'WARN'|'ERROR' }) { + logger.info(`[${data.type}] Admin log: ${data.message}`); + try { + let color: ColorResolvable = '#ffffff'; + switch(data.type) { + case 'INFO': color = '#00ff73'; break; + case 'WARN': color = '#ffc823'; break; + case 'ERROR': color = '#ff4208'; break; + } + + let embed = new MessageEmbed() + .setDescription(data.message) + .setColor(color); + + await client?.send({ + embeds: [ embed ], + username: bot.user?.username, + avatarURL: bot.user?.generateAvatarURL({ size: 128 }) + }); + } catch(e) { + logger.error(`Failed to log: ${e}`); + } +} + +export { + client, + adminBotLog, +} diff --git a/src/struct/AutomodClient.ts b/src/struct/AutomodClient.ts index 2f36072..282703b 100644 --- a/src/struct/AutomodClient.ts +++ b/src/struct/AutomodClient.ts @@ -1,6 +1,7 @@ import * as Revolt from "@janderedev/revolt.js"; import { IMonkManager } from 'monk'; import logger from '../bot/logger'; +import { adminBotLog } from "../bot/logging"; class AutomodClient extends Revolt.Client { db: IMonkManager; @@ -25,6 +26,7 @@ let login = (client: Revolt.Client): Promise => new Promise((resolve, reje client.once('ready', () => { logger.done(`Bot logged in as ${client.user?.username}!`); + adminBotLog({ message: 'Bot logged in', type: 'INFO' }); resolve(); }); });