From 809582841ea701789520cb5cf17de6c681ad4bf3 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sat, 26 Mar 2022 22:08:13 +0200 Subject: [PATCH] webhook for errors --- src/middleware/Logger.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/middleware/Logger.js b/src/middleware/Logger.js index 4d4fb4d..1eef439 100644 --- a/src/middleware/Logger.js +++ b/src/middleware/Logger.js @@ -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'), '') + .replace(new RegExp(username, 'gu'), ''); + + 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);