From 14a8833f0d607ad03f5e98ee1fa76c087e698a44 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Fri, 8 Nov 2024 10:28:10 -0800 Subject: [PATCH] update/simple discord sharding manager --- bot.js | 33 +++++++++++++++++++++++++++++++++ index.js | 33 ++++----------------------------- package.json | 2 +- 3 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 bot.js diff --git a/bot.js b/bot.js new file mode 100644 index 0000000..3856eaa --- /dev/null +++ b/bot.js @@ -0,0 +1,33 @@ +const DayzR = require('./src/DayzRBot'); +const config = require('./config/config'); +const { GatewayIntentBits } = require('discord.js'); + +const path = require("path"); +const fs = require('fs'); +const { HandleActivePlayersList } = require('./util/LogsHandler'); + +// Log all uncaught exceptions before killing process. +process.on('uncaughtException', async (error) => { + console.trace(error); + let d = new Date(); + // Asynchronously write the error message to a log file using Promises + await new Promise((resolve, reject) => { + if (HandleActivePlayersList.lastSendMessage) HandleActivePlayersList.lastSendMessage.delete().catch(error => client.sendError(channel, `HandleActivePlayersList Error: \n${error}`)); // Remove previous embed message before closing + 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 gracefully close the program + process.exit() +}); + +let client = new DayzR({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers] }, config); +client.build() diff --git a/index.js b/index.js index 3856eaa..0270ec6 100644 --- a/index.js +++ b/index.js @@ -1,33 +1,8 @@ -const DayzR = require('./src/DayzRBot'); +const { ShardingManager } = require('discord.js'); const config = require('./config/config'); -const { GatewayIntentBits } = require('discord.js'); -const path = require("path"); -const fs = require('fs'); -const { HandleActivePlayersList } = require('./util/LogsHandler'); +const manager = new ShardingManager('./bot.js', { token: config.Token }); -// Log all uncaught exceptions before killing process. -process.on('uncaughtException', async (error) => { - console.trace(error); - let d = new Date(); - // Asynchronously write the error message to a log file using Promises - await new Promise((resolve, reject) => { - if (HandleActivePlayersList.lastSendMessage) HandleActivePlayersList.lastSendMessage.delete().catch(error => client.sendError(channel, `HandleActivePlayersList Error: \n${error}`)); // Remove previous embed message before closing - 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(); - } - }); - }); +manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`)); - // Now gracefully close the program - process.exit() -}); - -let client = new DayzR({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers] }, config); -client.build() +manager.spawn(); \ No newline at end of file diff --git a/package.json b/package.json index 9fc4ffb..a0aff17 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dayzr-bot", - "version": "13.1.13", + "version": "13.3.0", "description": "A General Purpose Discord Bot for DayZ Nitrado Servers.", "main": "index.js", "nodemonConfig": {