From de34ce952910b1a33988a6a64f959dfaaa4430c3 Mon Sep 17 00:00:00 2001 From: IrPgFKS0 <2511445@gmail.com> Date: Wed, 6 Sep 2023 14:44:42 -0700 Subject: [PATCH] Improved logging to file on uncaught exceptions --- index.js | 24 ++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 818349f..ade34fc 100644 --- a/index.js +++ b/index.js @@ -2,5 +2,29 @@ const DayzR = require('./structures/DayzRBot'); const config = require('./config/config'); const { GatewayIntentBits } = require('discord.js'); +const path = require("path"); +const fs = require('fs'); + let client = new DayzR({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers] }, config); client.build() + +// Log all uncaught exceptions before killing process. +process.on('uncaughtException', async (error) => { + let d = new Date(); + // Asynchronously write the error message to a log file using Promises + await new Promise((resolve, reject) => { + fs.appendFile(path.join(__dirname, "./logs/Logs.log"), + `{"level":"error","message":"${d.getHours()}:${d.getMinutes()} - ${d.getMonth()+1}:${d.getDate()}:${d.getFullYear()} | uncaughtException: ${error.stack}"}`, (logErr) => { + if (logErr) { + console.error('Error writing uncaughtException to log file:', logErr); + reject(logErr); + process.exit() + } else { + resolve(); + } + }); + }); + + // Now, you can re-throw the error + process.exit() +}); diff --git a/package.json b/package.json index f6c6390..74ed96a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dayzr-bot", - "version": "9.8.3", + "version": "9.8.4", "description": "A General Purpose Discord Bot for DayZ Nitrado Servers.", "main": "index.js", "nodemonConfig": {