From 32d7ca7f6b711898ec1e31c14f4190013f1dd587 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Mon, 14 Jan 2019 12:21:15 +0100 Subject: [PATCH] Log the error in a txt file --- event/listeners/error.js | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 event/listeners/error.js diff --git a/event/listeners/error.js b/event/listeners/error.js new file mode 100644 index 00000000..4ff8f5cc --- /dev/null +++ b/event/listeners/error.js @@ -0,0 +1,48 @@ +const { Listener } = require('discord-akairo'); +const mkdirp = require('mkdirp'); +const fs = require('fs'); + +class errorListener extends Listener { + constructor() { + super('error', { + emitter: 'commandHandler', + event: 'error' + }); + } + + async exec(message, error, command) { + console.error(`Error happenend on the command: ${command.id}\n${message}\nOn the message: ${error}`); + //Get current date + let today = new Date(); + let dd = today.getDate(); + let mm = today.getMonth() + 1; //January is 0! + + let yyyy = today.getFullYear(); + if (dd < 10) { + dd = '0' + dd; + } + if (mm < 10) { + mm = '0' + mm; + } + today = dd + '/' + mm + '/' + yyyy; + //Get current hour + let time = new Date(); + let currentTime = time.getHours() + '_' + time.getMinutes() + '_' + time.getSeconds(); + //Create folder with current date + mkdirp(`./error/${today}`, function (err){ + if (err) { + console.error(err); + } + //Create txt with the current time + fs.writeFile(`./error/${today}/${currentTime}.txt`, `Error happenend on the command: ${command.id}\n${message}\nOn the message: ${error}`, function (err) { + if (err) { + console.error(err); + } + console.log(`Logged error succesfully in /error/${today}/${currentTime}.txt`); + }); + }); + } +} + + +module.exports = errorListener; \ No newline at end of file