webhook for errors

This commit is contained in:
Erik 2022-03-26 22:08:13 +02:00
parent e992a70e6c
commit 809582841e
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

@ -3,8 +3,11 @@ const chalk = require('chalk');
const moment = require('moment');
const path = require('path');
const fs = require('fs');
const { WebhookClient } = require('discord.js');
const os = require('os');
const { username } = os.userInfo();
const Util = require('../Util.js');
const { Util } = require('../utilities');
const Constants = {
Types: [
@ -32,6 +35,8 @@ class Logger {
this.manager = manager;
this.shardingManager = manager.shardingManager;
this._webhook = new WebhookClient({ url: process.env.ERROR_WEBHOOK_URL });
this._options = options;
this._writeStream = this._loadFile();
@ -56,17 +61,34 @@ class Logger {
const maximumCharacters = Math.max(...Constants.Types.map((t) => t.length));
const spacers = maximumCharacters - type.length;
const text = `${chalk[color](type)}${" ".repeat(spacers)} ${header} : ${string}`;
const strippedText = text.replace(stripRegex, '');
console.log(text); //eslint-disable-line no-console
if (type === 'error') this.webhook(strippedText);
if(this[type === 'error' ? '_errorWriteStream' : '_writeStream']) {
const strippedText = text.replace(stripRegex, '');
this[type === 'error' ? '_errorWriteStream' : '_writeStream']
const stream = type === 'error' ? '_errorWriteStream' : '_writeStream';
if(this[stream]) {
this[stream]
.write(`\n${strippedText}`);
}
}
webhook(text) {
const message = text.replace(new RegExp(process.env.DISCORD_TOKEN, 'gu'), '<redacted>')
.replace(new RegExp(username, 'gu'), '<redacted>');
const embed = {
color: 0xe88388,
timestamp: new Date(),
description: `\`\`\`${message}\`\`\``,
};
this._webhook.send({ embeds: [embed], username: `ENV: ${process.env.NODE_ENV}` });
}
_loadFile(type = null) {
const filename = `${moment().format('YYYY-MM-DD')}${type ? `-${type}` : ''}`;
const directory = path.join(process.cwd(), this._options.directory);