add basic logging

This commit is contained in:
janderedev 2022-01-04 15:14:46 +01:00
parent 648fe39fe6
commit e718827432
Signed by: Lea
GPG key ID: 5D5E18ACB990F57A
2 changed files with 44 additions and 0 deletions

42
src/bot/logging.ts Normal file
View file

@ -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,
}

View file

@ -1,6 +1,7 @@
import * as Revolt from "@janderedev/revolt.js"; import * as Revolt from "@janderedev/revolt.js";
import { IMonkManager } from 'monk'; import { IMonkManager } from 'monk';
import logger from '../bot/logger'; import logger from '../bot/logger';
import { adminBotLog } from "../bot/logging";
class AutomodClient extends Revolt.Client { class AutomodClient extends Revolt.Client {
db: IMonkManager; db: IMonkManager;
@ -25,6 +26,7 @@ let login = (client: Revolt.Client): Promise<void> => new Promise((resolve, reje
client.once('ready', () => { client.once('ready', () => {
logger.done(`Bot logged in as ${client.user?.username}!`); logger.done(`Bot logged in as ${client.user?.username}!`);
adminBotLog({ message: 'Bot logged in', type: 'INFO' });
resolve(); resolve();
}); });
}); });