From d8ec0be2c93735519c96baa2b15c97f7502761ca Mon Sep 17 00:00:00 2001 From: MatteZ02 <47610069+MatteZ02@users.noreply.github.com> Date: Fri, 13 Mar 2020 22:31:04 +0200 Subject: [PATCH] Sharding --- bot.js | 2 ++ commands/queue.js | 2 +- commands/reload.js | 30 ------------------------------ commands/restart.js | 8 +++----- commands/status.js | 1 + events/msg.js | 1 + events/ready.js | 4 ++-- index.js | 15 +++++++++++++-- struct/client.js | 9 ++++----- struct/config/config.js | 7 +++++-- struct/config/messages.js | 3 ++- struct/funcs/exe.js | 2 +- 12 files changed, 35 insertions(+), 49 deletions(-) create mode 100644 bot.js delete mode 100644 commands/reload.js diff --git a/bot.js b/bot.js new file mode 100644 index 00000000..f0066a36 --- /dev/null +++ b/bot.js @@ -0,0 +1,2 @@ +const MusicClient = require('./struct/client.js'); +const client = new MusicClient({}); \ No newline at end of file diff --git a/commands/queue.js b/commands/queue.js index cf7754b0..bf28441f 100644 --- a/commands/queue.js +++ b/commands/queue.js @@ -21,7 +21,7 @@ module.exports = { const embed = new Discord.MessageEmbed() .setTitle(client.messages.queueTitle) .setDescription(`${message}\n${queuemessage}`) - .setFooter(`${serverQueue.songs.size - 20} ${client.messages.queueFooter}`) + .setFooter(`${serverQueue.songs.length} ${client.messages.queueFooter}`) .setColor(client.config.embedColor) /*if (serverQueue.songs.size > 20) { embed.setFooter(`${serverQueue.songs.size - 20} ${client.messages.queueFooter}`) diff --git a/commands/reload.js b/commands/reload.js deleted file mode 100644 index 3c00d31f..00000000 --- a/commands/reload.js +++ /dev/null @@ -1,30 +0,0 @@ -const fs = require('fs'); -const path = require('path') - -module.exports = { - name: 'reload', - alias: 'none', - usage: '', - description: 'Reload all files', - onlyDev: true, - permission: 'none', - category: 'util', - async execute(msg, args, client, Discord, prefix, command) { - const commandFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'commands')).filter(f => f.endsWith('.js')); - for (const file of commandFiles) { - const command = require(`./${file}`); - command.uses = 0; - client.commands.set(command.name, command); - client.commandAliases.set(command.alias, command); - } - const settingFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'commands/settings')).filter(f => f.endsWith('.js')); - for (const file of settingFiles) { - const option = require(`./settings/${file}`); - client.settingCmd.set(option.name, option); - } - /*fs.readdirSync(path.join(__dirname, 'funcs')).forEach(filename => { - this.funcs[filename.slice(0, -3)] = require(`../struct/funcs/${filename}`); - });*/ - msg.channel.send(client.messages.reloaded); - } -}; diff --git a/commands/restart.js b/commands/restart.js index abe84b6f..2d7c59e0 100644 --- a/commands/restart.js +++ b/commands/restart.js @@ -2,13 +2,11 @@ module.exports = { name: 'restart', alias: 'none', usage: '', - description: 'Restart the bot', + description: 'restart all shards', onlyDev: true, - permission: 'none', + permission: 'dev', category: 'util', async execute(msg, args, client, Discord, prefix, command) { - client.destroy(); - require('../index.js'); - msg.channel.send(client.messages.restart); + client.shard.respawnAll(client.config.shardDelay, client.config.respawnDelay, client.config.spawnTimeout); } }; diff --git a/commands/status.js b/commands/status.js index 36ecbe09..0efe38eb 100644 --- a/commands/status.js +++ b/commands/status.js @@ -16,6 +16,7 @@ module.exports = { .addField(client.messages.statusField1, client.ws.ping, true) .addField(client.messages.statusField2, latency, true) .addField(client.messages.statusField3, uptime, true) + .addField(client.messages.statusField4, client.shard.ids) .setAuthor(client.user.username, client.user.displayAvatarURL) .setColor(client.config.embedColor) m.delete(); diff --git a/events/msg.js b/events/msg.js index c4491505..b8f24be1 100644 --- a/events/msg.js +++ b/events/msg.js @@ -2,6 +2,7 @@ module.exports = { name: 'message', async execute(client, msg, Discord) { if (msg.author.bot || !msg.guild) return; + if (!client.global.db.guilds[msg.guild.id]) return; let prefix = client.global.db.guilds[msg.guild.id].prefix; if (client.config.devMode) prefix = client.config.devPrefix; const args = msg.content.slice(prefix.length).split(' '); diff --git a/events/ready.js b/events/ready.js index 04fce88e..acede04f 100644 --- a/events/ready.js +++ b/events/ready.js @@ -23,7 +23,7 @@ module.exports = { }; }); } - console.log('- DB Set -'); + console.log(`- DB Set - Shard: ${client.shard.ids} -`); client.user.setActivity(`@${client.user.username} help | 🎶`, { type: 'LISTENING' }); client.user.setStatus('online'); const dbl = new DBL(client.config.dblKey, client); @@ -36,7 +36,7 @@ module.exports = { }); dbl.postStats(client.guilds.size); } - console.log('- Activated -'); + console.log(`- Activated - Shard: ${client.shard.ids} -`); setInterval(async () => { if (client.config.saveDB && !client.config.devMode) { console.log('DB saved'); diff --git a/index.js b/index.js index f0066a36..16486b3b 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,13 @@ -const MusicClient = require('./struct/client.js'); -const client = new MusicClient({}); \ No newline at end of file +const config = require("./struct/config/config.js"); + +if (config.devMode) { + config.token = config.devToken; + config.shards = 1; +} + +const { ShardingManager } = require('discord.js'); +const manager = new ShardingManager('./bot.js', { token: config.token, respawn: true, totalShards: config.shards }); + +console.log('Launching shards...'); +manager.spawn(config.shards, config.shardDelay, config.shardTimeout); +manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`)); \ No newline at end of file diff --git a/struct/client.js b/struct/client.js index 641ccd2f..d2a0cd79 100644 --- a/struct/client.js +++ b/struct/client.js @@ -38,6 +38,9 @@ module.exports = class extends Client { const option = require(`../commands/settings/${file}`); this.settingCmd.set(option.name, option); } + if (this.config.devMode) { + this.config.token = this.config.devToken; + } admin.initializeApp({ credential: admin.credential.cert(serviceAccount), @@ -53,10 +56,6 @@ module.exports = class extends Client { this.db.FieldValue = require('firebase-admin').firestore.FieldValue; - if (this.config.devMode) { - this.config.token = this.config.devToken; - } - this.on('ready', () => { require(`${events}ready`).execute(this, Discord); }); @@ -70,7 +69,7 @@ module.exports = class extends Client { require(`${events}voiceStateUpdate`).execute(this, oldState, newState); }); this.on('error', (error) => { - client.channels.fetch(client.config.debug_channel).send('Error: ' + error); + client.channels.fetch(client.config.debug_channel).send(`Error: ${error} on shard: ${this.shard}`); }); this.login(this.config.token).catch(err => console.log('Failed to login: ' + err)); diff --git a/struct/config/config.js b/struct/config/config.js index c8b61409..f38f6a86 100644 --- a/struct/config/config.js +++ b/struct/config/config.js @@ -11,13 +11,16 @@ module.exports = { primary_test_channel: "617633098296721409", secondary_test_channel: "570531724002328577", devId: "360363051792203779", - //misc embedColor: "#b50002", invite: "https://discordapp.com/api/oauth2/authorize?client_id=607266889537945605&permissions=3427328&redirect_uri=https%3A%2F%2Fdiscordapp.com%2Foauth2%2Fauthorize%3Fclient_id%3D607266889537945605%26%3Bscope%3Dbot%26%3Bpermissions%3D0&scope=bot", //Settings - devMode: false, + devMode: true, dblApi: false, saveDB: true, + shards: 8, + shardDelay: 5000, + spawnTimeout: 30000, + respawnDelay: 1000, //db values prefix: ">", devPrefix: "-", diff --git a/struct/config/messages.js b/struct/config/messages.js index e9e78f63..f132fe3b 100644 --- a/struct/config/messages.js +++ b/struct/config/messages.js @@ -91,7 +91,7 @@ module.exports = { reloaded: "All files reloaded!", removed: "🗑️ removed `%SONG%` from the queue!", reset: "<:green_check_mark:674265384777416705> Reset __all__ guild settings!", - restart: "restarted!", + restart: "restarting all shards...", resumed: "<:resume:674685585478254603> Resumed the music!", seekingPointPositive: "<:redx:674263474704220182> The seeking point needs to be a positive number!", seekMax: "<:redx:674263474704220182> The lenght of this song is %LENGTH% seconds! You can't seek further than that!", @@ -118,6 +118,7 @@ module.exports = { statusField1: ":signal_strength: Ping", statusField2: "Latency", statusField3: ":stopwatch: Uptime", + statusField4: "Shard: ", statusTitle: "Status for Musix", stop: "<:stop:674685626108477519> Stopped the music!", validNumber: "<:redx:674263474704220182> I'm sorry, But you need to enter a valid __number__.", diff --git a/struct/funcs/exe.js b/struct/funcs/exe.js index 2f30a7e7..9dcb5247 100644 --- a/struct/funcs/exe.js +++ b/struct/funcs/exe.js @@ -11,7 +11,7 @@ module.exports = function (msg, args, client, Discord, prefix, command) { const embed = new Discord.MessageEmbed() .setTitle(`Musix ${error.toString()}`) .setDescription(error.stack.replace(/at /g, '**at **')) - .setFooter(`guild: ${msg.guild.id}, user: ${msg.member.id}, channel: ${msg.channel.id}, date: ${date}`) + .setFooter(`guild: ${msg.guild.id} (${msg.guild.name}), user: ${msg.member.id} (${msg.member.displayName}), channel: ${msg.channel.id} (${msg.channel.name}), date: ${date}, Shard: ${client.shard.ids}`) .setColor('#b50002'); client.debug_channel.send(embed); console.error(error);