diff --git a/index.js b/index.js index ae25354..fc66487 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const chalk = require('chalk'); const fs = require('node:fs'); const config = require('./configs/config.json'); const path = require('node:path'); +const winston = require('winston'); // Discord Client Constructor const client = new Discord.Client({ @@ -67,24 +68,52 @@ async function registerSlashCommands() { } } +// Create a Winston logger +const logger = winston.createLogger({ + level: 'error', + format: winston.format.combine( + winston.format.timestamp(), + winston.format.json() + ), + transports: [ + new winston.transports.File({ filename: 'error.log' }), + new winston.transports.Console({ + format: winston.format.combine( + winston.format.colorize(), + winston.format.simple() + ) + }) + ], +}); + // Call the async function to register slash commands registerSlashCommands(); // Anti Crash process.on('unhandledRejection', (reason, p) => { + logger.error('[antiCrash] :: Unhandled Rejection/Catch', { reason, p }); console.log(chalk.bold.redBright('[antiCrash] :: Unhandled Rejection/Catch')); console.log(reason?.stack, p); }); process.on("uncaughtException", (err, origin) => { - console.log(chalk.bold.redBright('[antiCrash] :: ncaught Exception/Catch')); + logger.error('[antiCrash] :: Uncaught Exception/Catch', { err, origin }); + console.log(chalk.bold.redBright('[antiCrash] :: Uncaught Exception/Catch')); console.log(err?.stack, origin); }); process.on('uncaughtExceptionMonitor', (err, origin) => { + logger.error('[antiCrash] :: Uncaught Exception/Catch (MONITOR)', { err, origin }); console.log(chalk.bold.redBright('[antiCrash] :: Uncaught Exception/Catch (MONITOR)')); console.log(err?.stack, origin); }); +// Optional: Add more specific error handling +process.on('warning', (warning) => { + logger.warn('[antiCrash] :: Warning detected', { warning }); + console.log(chalk.bold.yellowBright('[antiCrash] :: Warning detected')); + console.log(warning?.stack); +}); + // Discord Client login client.login(config.Token);