diff --git a/app.js b/app.js index 01e5eee..8463187 100644 --- a/app.js +++ b/app.js @@ -6,14 +6,23 @@ The bot will continue to run past this message, but keep in mind that it could b require("dotenv").config(); // main sharding manager -const { Master } = require("eris-sharder"); +const { Fleet } = require("eris-fleet"); +const { isMaster } = require("cluster"); +const path = require("path"); +const { inspect } = require("util"); // dbl posting const TopGG = require("@top-gg/sdk"); const dbl = process.env.NODE_ENV === "production" && process.env.DBL !== "" ? new TopGG.Api(process.env.DBL) : null; -const master = new Master(`Bot ${process.env.TOKEN}`, "/shard.js", { - name: "esmBot", - stats: true, +const Admiral = new Fleet({ + path: path.join(__dirname, "./shard.js"), + token: `Bot ${process.env.TOKEN}`, + startingStatus: { + status: "idle", + game: { + name: "Starting esmBot..." + } + }, clientOptions: { disableEvents: { CHANNEL_DELETE: true, @@ -48,13 +57,18 @@ const master = new Master(`Bot ${process.env.TOKEN}`, "/shard.js", { } }); -master.on("stats", async (stats) => { - master.broadcast(0, Object.assign(stats, { _eventName: "stat" })); - // dbl posting +if (isMaster) { + Admiral.on("log", (m) => console.log(m)); + Admiral.on("debug", (m) => console.debug(m)); + Admiral.on("warn", (m) => console.warn(m)); + Admiral.on("error", (m) => console.error(inspect(m))); + if (dbl) { - await dbl.postStats({ - serverCount: stats.guilds, - shardCount: await master.calculateShards() + Admiral.on("stats", async (m) => { + await dbl.postStats({ + serverCount: m.guilds, + shardCount: m.shardCount + }); }); } -}); \ No newline at end of file +} \ No newline at end of file diff --git a/classes/command.js b/classes/command.js index 1dcec6f..7ce48e4 100644 --- a/classes/command.js +++ b/classes/command.js @@ -1,7 +1,8 @@ class Command { - constructor(client, cluster, ipc, message, args, content, specialArgs) { + constructor(client, cluster, worker, ipc, message, args, content, specialArgs) { this.client = client; this.cluster = cluster; + this.worker = worker; this.ipc = ipc; this.message = message; this.args = args; diff --git a/classes/musicCommand.js b/classes/musicCommand.js index 0ff03ea..98ec336 100644 --- a/classes/musicCommand.js +++ b/classes/musicCommand.js @@ -2,7 +2,7 @@ const Command = require("./command.js"); const soundPlayer = require("../utils/soundplayer.js"); class MusicCommand extends Command { - constructor(client, cluster, ipc, message, args, content, specialArgs) { + constructor(client, cluster, worker, ipc, message, args, content, specialArgs) { super(client, cluster, ipc, message, args, content, specialArgs); this.connection = soundPlayer.players.get(message.channel.guild.id); } diff --git a/commands/general/info.js b/commands/general/info.js index 1320d27..8c16822 100644 --- a/commands/general/info.js +++ b/commands/general/info.js @@ -1,10 +1,10 @@ const { version } = require("../../package.json"); -const collections = require("../../utils/collections.js"); const Command = require("../../classes/command.js"); class InfoCommand extends Command { async run() { const owner = await this.ipc.fetchUser(process.env.OWNER); + const stats = await this.ipc.getStats(); return { "embed": { "color": 16711680, @@ -23,7 +23,7 @@ class InfoCommand extends Command { }, { "name": "💬 Total Servers:", - "value": collections.stats.guilds ? collections.stats.guilds : `${this.client.guilds.size} (for this cluster only)` + "value": stats.guilds ? stats.guilds : `${this.client.guilds.size} (for this cluster only)` }, { "name": "✅ Official Server:", diff --git a/commands/general/restart.js b/commands/general/restart.js index 7b767ef..d4b7e27 100644 --- a/commands/general/restart.js +++ b/commands/general/restart.js @@ -11,7 +11,8 @@ class RestartCommand extends Command { for (const command of collections.commands) { await handler.unload(command); } - this.ipc.broadcast("restart"); + this.ipc.restartAllClusters(); + //this.ipc.broadcast("restart"); } static description = "Restarts me"; diff --git a/commands/general/stats.js b/commands/general/stats.js index b55176c..7720783 100644 --- a/commands/general/stats.js +++ b/commands/general/stats.js @@ -1,5 +1,4 @@ const { version } = require("../../package.json"); -const collections = require("../../utils/collections.js"); const day = require("dayjs"); day.extend(require("dayjs/plugin/duration")); const os = require("os"); @@ -10,6 +9,7 @@ class StatsCommand extends Command { const duration = day.duration(this.client.uptime).format(" D [days], H [hrs], m [mins], s [secs]"); const uptime = day.duration(process.uptime(), "seconds").format(" D [days], H [hrs], m [mins], s [secs]"); const owner = await this.ipc.fetchUser(process.env.OWNER); + const stats = await this.ipc.getStats(); return { embed: { "author": { @@ -24,12 +24,12 @@ class StatsCommand extends Command { }, { "name": "Cluster Memory Usage", - "value": `${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} MB`, + "value": `${stats.clusters[this.cluster].ram.toFixed(2)} MB`, "inline": true }, { "name": "Total Memory Usage", - "value": collections.stats.totalRam ? `${collections.stats.totalRam.toFixed(2)} MB` : "Unknown", + "value": stats.totalRam ? `${stats.totalRam.toFixed(2)} MB` : "Unknown", "inline": true }, { @@ -66,12 +66,12 @@ class StatsCommand extends Command { }, { "name": "Servers", - "value": collections.stats.guilds ? collections.stats.guilds : `${this.client.guilds.size} (for this cluster only)`, + "value": stats.guilds ? stats.guilds : `${this.client.guilds.size} (for this cluster only)`, "inline": true }, { "name": "Users (approximation)", - "value": collections.stats.users ? collections.stats.users : `${this.client.users.size} (for this cluster only)`, + "value": stats.users ? stats.users : `${this.client.users.size} (for this cluster only)`, "inline": true } ] diff --git a/events/guildCreate.js b/events/guildCreate.js index ff4070b..b33e8e2 100644 --- a/events/guildCreate.js +++ b/events/guildCreate.js @@ -2,7 +2,7 @@ const db = require("../utils/database.js"); const logger = require("../utils/logger.js"); // run when the bot is added to a guild -module.exports = async (client, cluster, ipc, guild) => { - logger.log("info", `[GUILD JOIN] ${guild.name} (${guild.id}) added the bot.`); +module.exports = async (client, cluster, worker, ipc, guild) => { + logger.log(`[GUILD JOIN] ${guild.name} (${guild.id}) added the bot.`); await db.addGuild(guild); }; diff --git a/events/guildDelete.js b/events/guildDelete.js index 4e38a14..40a43f2 100644 --- a/events/guildDelete.js +++ b/events/guildDelete.js @@ -1,6 +1,6 @@ const logger = require("../utils/logger.js"); // run when the bot is removed from a guild -module.exports = async (client, cluster, ipc, guild) => { +module.exports = async (client, cluster, worker, ipc, guild) => { logger.log(`[GUILD LEAVE] ${guild.name} (${guild.id}) removed the bot.`); }; diff --git a/events/messageCreate.js b/events/messageCreate.js index d812240..5ff5630 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -5,7 +5,7 @@ const collections = require("../utils/collections.js"); const parseCommand = require("../utils/parseCommand.js"); // run when someone sends a message -module.exports = async (client, cluster, ipc, message) => { +module.exports = async (client, cluster, worker, ipc, message) => { // ignore dms and other bots if (message.author.bot) return; @@ -88,7 +88,7 @@ module.exports = async (client, cluster, ipc, message) => { await database.addCount(collections.aliases.has(command) ? collections.aliases.get(command) : command); const startTime = new Date(); // eslint-disable-next-line no-unused-vars - const commandClass = new cmd(client, cluster, ipc, message, parsed._, message.content.substring(prefix.length).trim().replace(command, "").trim(), (({ _, ...o }) => o)(parsed)); // we also provide the message content as a parameter for cases where we need more accuracy + const commandClass = new cmd(client, cluster, worker, ipc, message, parsed._, message.content.substring(prefix.length).trim().replace(command, "").trim(), (({ _, ...o }) => o)(parsed)); // we also provide the message content as a parameter for cases where we need more accuracy const result = await commandClass.run(); const endTime = new Date(); if ((endTime - startTime) >= 180000) reference.allowedMentions.repliedUser = true; diff --git a/events/rawWS.js b/events/rawWS.js index 65e55de..42d5001 100644 --- a/events/rawWS.js +++ b/events/rawWS.js @@ -1,7 +1,7 @@ const player = require("../utils/soundplayer.js"); // run when a raw packet is sent, used for sending data to lavalink -module.exports = async (client, cluster, ipc, packet) => { +module.exports = async (client, cluster, worker, ipc, packet) => { if (!player.manager) return; switch (packet.t) { case "VOICE_SERVER_UPDATE": diff --git a/events/voiceChannelLeave.js b/events/voiceChannelLeave.js index c477250..df8b365 100644 --- a/events/voiceChannelLeave.js +++ b/events/voiceChannelLeave.js @@ -2,7 +2,7 @@ const soundPlayer = require("../utils/soundplayer.js"); const AwaitRejoin = require("../utils/awaitrejoin.js"); const { random } = require("../utils/misc.js"); -module.exports = async (client, cluster, ipc, member, oldChannel) => { +module.exports = async (client, cluster, worker, ipc, member, oldChannel) => { const connection = soundPlayer.players.get(oldChannel.guild.id); if (connection && connection.type === "music" && oldChannel.id === connection.voiceChannel.id) { if (oldChannel.voiceMembers.filter((i) => i.id !== client.user.id).length === 0) { diff --git a/events/voiceChannelSwitch.js b/events/voiceChannelSwitch.js index 52067d4..7977894 100644 --- a/events/voiceChannelSwitch.js +++ b/events/voiceChannelSwitch.js @@ -1,5 +1,5 @@ const leaveHandler = require("./voiceChannelLeave.js"); -module.exports = async (client, cluster, ipc, member, newChannel, oldChannel) => { +module.exports = async (client, cluster, worker, ipc, member, newChannel, oldChannel) => { await leaveHandler(client, cluster, ipc, member, oldChannel); }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 070450d..122607c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,8 @@ "dayjs": "^1.10.4", "dotenv": "^9.0.2", "emoji-regex": "^9.2.2", - "eris-sharder": "github:esmBot/eris-sharder#eris-dev", + "eris": "^0.15.1", + "eris-fleet": "^0.3.7", "file-type": "^16.1.0", "jsqr": "^1.3.1", "lavacord": "^1.1.9", @@ -596,17 +597,6 @@ "node": ">=6" } }, - "node_modules/ansi-gray": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/ansi-gray/-/ansi-gray-0.1.1.tgz", - "integrity": "sha1-KWLPVOyXksSFEKPetSRDaGHvclE=", - "dependencies": { - "ansi-wrap": "0.1.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/ansi-regex": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", @@ -626,14 +616,6 @@ "node": ">=4" } }, - "node_modules/ansi-wrap": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/ansi-wrap/-/ansi-wrap-0.1.0.tgz", - "integrity": "sha1-qCJQ3bABXponyoLoLqYDu/pF768=", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/aproba": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/aproba/-/aproba-1.2.0.tgz", @@ -990,14 +972,6 @@ "simple-swizzle": "^0.2.2" } }, - "node_modules/color-support": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/color-support/-/color-support-1.1.3.tgz", - "integrity": "sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg==", - "bin": { - "color-support": "bin.js" - } - }, "node_modules/colorette": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.2.2.tgz", @@ -1005,14 +979,6 @@ "dev": true, "peer": true }, - "node_modules/colors": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", - "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==", - "engines": { - "node": ">=0.1.90" - } - }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1213,9 +1179,9 @@ } }, "node_modules/eris": { - "version": "0.15.2-dev", - "resolved": "git+ssh://git@github.com/abalabahaha/eris.git#ea9d6700c8e3fedf5d966da4babe195c8cd681da", - "license": "MIT", + "version": "0.15.1", + "resolved": "https://registry.npmjs.org/eris/-/eris-0.15.1.tgz", + "integrity": "sha512-IQ3BPW6OjgFoqjdh+irPOa1jFlkotk+WNu2GQQ7QAQfbzQEPZgn+F+hpOxfMUXPHOZMX4sPKLkVDkMHAssBYhw==", "dependencies": { "ws": "^7.2.1" }, @@ -1227,17 +1193,12 @@ "tweetnacl": "^1.0.1" } }, - "node_modules/eris-sharder": { - "version": "1.10.0", - "resolved": "git+ssh://git@github.com/esmBot/eris-sharder.git#3b7366e5d99012ca0e3350e4d81f384b48911c2d", - "license": "MIT", - "dependencies": { - "colors": "^1.1.2", - "eris": "github:abalabahaha/eris#dev", - "fancy-log": "^1.3.0" - }, - "engines": { - "node": ">=8.0.0" + "node_modules/eris-fleet": { + "version": "0.3.7", + "resolved": "https://registry.npmjs.org/eris-fleet/-/eris-fleet-0.3.7.tgz", + "integrity": "sha512-irrGAZIwTXWxIfsSoi8YIm0nHImUADKG+wSr7yk5QgJfSFzIpCEgqFr4x7hdskfcwRa4CCR2HUrjuWDc0zITvg==", + "peerDependencies": { + "eris": "^0.15.0" } }, "node_modules/erlpack": { @@ -1606,20 +1567,6 @@ "node": ">=6" } }, - "node_modules/fancy-log": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/fancy-log/-/fancy-log-1.3.3.tgz", - "integrity": "sha512-k9oEhlyc0FrVh25qYuSELjr8oxsCoc4/LEZfg2iJJrfEk/tZL9bCoJE47gqAvI2m/AUjluCS4+3I0eTx8n3AEw==", - "dependencies": { - "ansi-gray": "^0.1.1", - "color-support": "^1.1.3", - "parse-node-version": "^1.0.0", - "time-stamp": "^1.0.0" - }, - "engines": { - "node": ">= 0.10" - } - }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -2560,14 +2507,6 @@ "node": ">=6" } }, - "node_modules/parse-node-version": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/parse-node-version/-/parse-node-version-1.0.1.tgz", - "integrity": "sha512-3YHlOa/JgH6Mnpr05jP9eDG254US9ek25LyIxZlDItp2iJtwyaXQb57lBYLdT3MowkUFYEV2XXNAYIPlESvJlA==", - "engines": { - "node": ">= 0.10" - } - }, "node_modules/path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", @@ -3420,14 +3359,6 @@ "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", "dev": true }, - "node_modules/time-stamp": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/time-stamp/-/time-stamp-1.1.0.tgz", - "integrity": "sha1-dkpaEa9QVhkhsTPztE5hhofg9cM=", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", @@ -4219,14 +4150,6 @@ "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==", "dev": true }, - "ansi-gray": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/ansi-gray/-/ansi-gray-0.1.1.tgz", - "integrity": "sha1-KWLPVOyXksSFEKPetSRDaGHvclE=", - "requires": { - "ansi-wrap": "0.1.0" - } - }, "ansi-regex": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", @@ -4240,11 +4163,6 @@ "color-convert": "^1.9.0" } }, - "ansi-wrap": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/ansi-wrap/-/ansi-wrap-0.1.0.tgz", - "integrity": "sha1-qCJQ3bABXponyoLoLqYDu/pF768=" - }, "aproba": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/aproba/-/aproba-1.2.0.tgz", @@ -4520,11 +4438,6 @@ "simple-swizzle": "^0.2.2" } }, - "color-support": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/color-support/-/color-support-1.1.3.tgz", - "integrity": "sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg==" - }, "colorette": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.2.2.tgz", @@ -4532,11 +4445,6 @@ "dev": true, "peer": true }, - "colors": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", - "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==" - }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -4690,22 +4598,20 @@ } }, "eris": { - "version": "git+ssh://git@github.com/abalabahaha/eris.git#ea9d6700c8e3fedf5d966da4babe195c8cd681da", - "from": "eris@github:abalabahaha/eris#dev", + "version": "0.15.1", + "resolved": "https://registry.npmjs.org/eris/-/eris-0.15.1.tgz", + "integrity": "sha512-IQ3BPW6OjgFoqjdh+irPOa1jFlkotk+WNu2GQQ7QAQfbzQEPZgn+F+hpOxfMUXPHOZMX4sPKLkVDkMHAssBYhw==", "requires": { "opusscript": "^0.0.8", "tweetnacl": "^1.0.1", "ws": "^7.2.1" } }, - "eris-sharder": { - "version": "git+ssh://git@github.com/esmBot/eris-sharder.git#3b7366e5d99012ca0e3350e4d81f384b48911c2d", - "from": "eris-sharder@github:esmBot/eris-sharder#eris-dev", - "requires": { - "colors": "^1.1.2", - "eris": "github:abalabahaha/eris#dev", - "fancy-log": "^1.3.0" - } + "eris-fleet": { + "version": "0.3.7", + "resolved": "https://registry.npmjs.org/eris-fleet/-/eris-fleet-0.3.7.tgz", + "integrity": "sha512-irrGAZIwTXWxIfsSoi8YIm0nHImUADKG+wSr7yk5QgJfSFzIpCEgqFr4x7hdskfcwRa4CCR2HUrjuWDc0zITvg==", + "requires": {} }, "erlpack": { "version": "git+ssh://git@github.com/abalabahaha/erlpack.git#5d0064f9e106841e1eead711a6451f99b0d289fd", @@ -4975,17 +4881,6 @@ "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", "integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==" }, - "fancy-log": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/fancy-log/-/fancy-log-1.3.3.tgz", - "integrity": "sha512-k9oEhlyc0FrVh25qYuSELjr8oxsCoc4/LEZfg2iJJrfEk/tZL9bCoJE47gqAvI2m/AUjluCS4+3I0eTx8n3AEw==", - "requires": { - "ansi-gray": "^0.1.1", - "color-support": "^1.1.3", - "parse-node-version": "^1.0.0", - "time-stamp": "^1.0.0" - } - }, "fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -5701,11 +5596,6 @@ "callsites": "^3.0.0" } }, - "parse-node-version": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/parse-node-version/-/parse-node-version-1.0.1.tgz", - "integrity": "sha512-3YHlOa/JgH6Mnpr05jP9eDG254US9ek25LyIxZlDItp2iJtwyaXQb57lBYLdT3MowkUFYEV2XXNAYIPlESvJlA==" - }, "path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", @@ -6366,11 +6256,6 @@ "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", "dev": true }, - "time-stamp": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/time-stamp/-/time-stamp-1.1.0.tgz", - "integrity": "sha1-dkpaEa9QVhkhsTPztE5hhofg9cM=" - }, "to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", diff --git a/package.json b/package.json index a58e34a..64b9baa 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "dayjs": "^1.10.4", "dotenv": "^9.0.2", "emoji-regex": "^9.2.2", - "eris-sharder": "github:esmBot/eris-sharder#eris-dev", + "eris": "^0.15.1", + "eris-fleet": "^0.3.7", "file-type": "^16.1.0", "jsqr": "^1.3.1", "lavacord": "^1.1.9", diff --git a/shard.js b/shard.js index ab7a0ff..92ddb8a 100644 --- a/shard.js +++ b/shard.js @@ -1,5 +1,5 @@ // shard base -const { Base } = require("eris-sharder"); +const { BaseClusterWorker } = require("eris-fleet"); // path stuff const { readdir } = require("fs").promises; // fancy loggings @@ -23,9 +23,11 @@ const helpGenerator = process.env.OUTPUT !== "" ? require("./utils/help.js") : null; const http = require("http"); -class Shard extends Base { +class Shard extends BaseClusterWorker { constructor(bot) { super(bot); + + this.init(); } async init() { @@ -48,7 +50,7 @@ class Shard extends Base { logger.log("log", `Loading event from ${file}...`); const eventName = file.split(".")[0]; const event = require(`./events/${file}`); - this.bot.on(eventName, event.bind(null, this.bot, this.clusterID, this.ipc)); + this.bot.on(eventName, event.bind(null, this.bot, this.clusterID, this.workerID, this.ipc)); } // connect to image api if enabled @@ -106,20 +108,40 @@ connected_workers ${image.connections.size} }); } - // handle process stop - process.on("SIGINT", () => { - logger.log("warn", "SIGINT detected, shutting down..."); - this.bot.editStatus("dnd", { - name: "Restarting/shutting down..." - }); - for (const command in collections.commands) { - handler.unload(command); - } - this.bot.disconnect(); - require("./utils/database.js").stop(); - process.exit(0); + this.ipc.register("reload", async (message) => { + const result = await handler.unload(message.cmd); + if (result) return this.ipc.broadcast("reloadFail", { result: result }); + const result2 = await handler.load(collections.paths.get(message.cmd)); + if (result2) return this.ipc.broadcast("reloadFail", { result: result2 }); + return this.ipc.broadcast("reloadSuccess"); }); - return; + + this.bot.privateChannels.limit = 0; + + this.ipc.register("soundreload", async () => { + const soundStatus = await sound.checkStatus(); + if (!soundStatus) { + const length = await sound.connect(this.bot); + return this.ipc.broadcast("soundReloadSuccess", { length }); + } else { + return this.ipc.broadcast("soundReloadFail"); + } + }); + + // connect to lavalink + if (!sound.status && !sound.connected) sound.connect(this.bot); + + database.setup(); + + // set activity (a.k.a. the gamer code) + (async function activityChanger() { + this.bot.editStatus("dnd", { + name: `${misc.random(messages)} | @${this.bot.user.username} help`, + }); + setTimeout(activityChanger.bind(this), 900000); + }).bind(this)(); + + logger.log("info", `Started worker ${this.workerID}.`); } async* getFiles(dir) { @@ -133,54 +155,16 @@ connected_workers ${image.connections.size} } } - async launch() { - await this.init(); - - this.ipc.register("stat", (message) => { - collections.stats = message; + shutdown(done) { + logger.log("warn", "SIGINT detected, shutting down..."); + this.bot.editStatus("dnd", { + name: "Restarting/shutting down..." }); - - this.ipc.register("restart", async () => { - this.bot.editStatus("dnd", { - name: "esmBot is restarting, please stand by." - }); - process.exit(1); - }); - - this.ipc.register("reload", async (message) => { - const result = await handler.unload(message.cmd); - if (result) return this.ipc.broadcast("reloadFail", { result: result }); - const result2 = await handler.load(collections.paths.get(message.cmd)); - if (result2) return this.ipc.broadcast("reloadFail", { result: result2 }); - return this.ipc.broadcast("reloadSuccess"); - }); - - this.ipc.register("soundreload", async () => { - const soundStatus = await sound.checkStatus(); - if (!soundStatus) { - const length = await sound.connect(this.bot); - return this.ipc.broadcast("soundReloadSuccess", { length }); - } else { - return this.ipc.broadcast("soundReloadFail"); - } - }); - - // connect to lavalink - if (!sound.status && !sound.connected) await sound.connect(this.bot); - - this.bot.privateChannels.limit = 0; - - await database.setup(); - - // set activity (a.k.a. the gamer code) - (async function activityChanger() { - this.bot.editStatus("dnd", { - name: `${misc.random(messages)} | @${this.bot.user.username} help`, - }); - setTimeout(activityChanger.bind(this), 900000); - }).bind(this)(); - - logger.log("info", `Started cluster ${this.clusterID}.`); + for (const command in collections.commands) { + handler.unload(command); + } + require("./utils/database.js").stop(); + done(); } } diff --git a/utils/collections.js b/utils/collections.js index 1eeeff2..177321a 100644 --- a/utils/collections.js +++ b/utils/collections.js @@ -27,6 +27,4 @@ class Cache extends Map { } exports.prefixCache = new Cache(); -exports.disabledCache = new Cache(); - -exports.stats = {}; \ No newline at end of file +exports.disabledCache = new Cache(); \ No newline at end of file diff --git a/utils/logger.js b/utils/logger.js index 1cdd451..6e5c799 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -1,4 +1,4 @@ -exports.log = (type, content) => content ? process.send({ name: type, msg: content }) : process.send({ name: "info", msg: type }); +exports.log = (type, content) => content ? process.send({ op: type, msg: content }) : process.send({ op: "info", msg: type }); exports.error = (...args) => this.log("error", ...args);