From 184b528f56f36df00f0e1366a531bad42ea18779 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Fri, 21 Jul 2023 12:27:49 +0200 Subject: [PATCH] feat: update ready events to handle sharding --- src/bot.js | 8 ++++++-- src/events/client/ready.js | 25 ++++++++++++++++--------- src/index.js | 13 +++++++++++-- src/utils/other/postBotStats.js | 26 ++++++++++++++++++++++---- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/bot.js b/src/bot.js index 2bce80e..9b71b27 100644 --- a/src/bot.js +++ b/src/bot.js @@ -1,3 +1,4 @@ +require('dotenv').config(); const logger = require('./services/logger'); const { registerEventListeners } = require('./utils/registerEventListeners.js'); const { registerClientCommands } = require('./utils/registerClientCommands.js'); @@ -8,8 +9,11 @@ const { createPlayer } = require('./utils/factory/createPlayer.js'); const client = await createClient(); const player = await createPlayer(client); - registerEventListeners(client, player); - registerClientCommands(client); + client.on('allShardsReady', async () => { + registerEventListeners(client, player); + registerClientCommands(client); + client.emit('ready', client); + }); client.login(process.env.DISCORD_BOT_TOKEN); })().catch((error) => { diff --git a/src/events/client/ready.js b/src/events/client/ready.js index 7d81bf3..d0aaacc 100644 --- a/src/events/client/ready.js +++ b/src/events/client/ready.js @@ -1,28 +1,35 @@ const logger = require('../../services/logger'); const { embedOptions, systemOptions, presenceStatusOptions } = require('../../config'); -const { Events, EmbedBuilder } = require('discord.js'); const { postBotStats } = require('../../utils/other/postBotStats.js'); +const { Events, EmbedBuilder } = require('discord.js'); module.exports = { name: Events.ClientReady, isDebug: false, - once: true, + once: false, execute: async (client) => { - logger.info(`Client logged in successfully as ${client.user.tag}!`); + logger.debug(`Shard ${client.shard.ids[0]}> Client 'ready' event emitted after 'allShardsReady'.`); + await client.user.setPresence(presenceStatusOptions); await client.user.setPresence(presenceStatusOptions); - // Post bot stats to bot lists in production - process.env.NODE_ENV === 'production' ? postBotStats(client) : null; + const channel = await client.channels.cache.get(systemOptions.systemMessageChannelId); - // send message to system message channel for event - if (systemOptions.systemMessageChannelId) { - await client.channels.cache.get(systemOptions.systemMessageChannelId).send({ + // Check if the channel exists in curent shard and send a message + if (channel) { + logger.info( + `Shard ${client.shard.ids[0]}> All shards ready, sending system message to channel id ${channel.id}.` + ); + channel.send({ embeds: [ new EmbedBuilder() - .setDescription(`${embedOptions.icons.success} **${client.user.tag}** is now **\`online\`**!`) + .setDescription( + `**${embedOptions.icons.success} All shards ready**\n**${client.user.tag}** is now **\`online\`**!` + ) .setColor(embedOptions.colors.success) ] }); } + + process.env.NODE_ENV === 'production' ? await postBotStats(client) : null; } }; diff --git a/src/index.js b/src/index.js index 1402da4..b3d4fb9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,26 @@ require('dotenv').config(); const logger = require('./services/logger'); -const { ShardingManager } = require('discord.js'); +const { ShardingManager, ShardEvents } = require('discord.js'); const manager = new ShardingManager('./src/bot.js', { token: process.env.DISCORD_BOT_TOKEN, - totalShards: 'auto', + totalShards: 2, shardList: 'auto', mode: 'process', respawn: true }); +const readyShards = new Set(); manager.on('shardCreate', (shard) => { logger.debug(`Launched shard ${shard.id}`); + + // When all shards are ready, emit event 'allShardsReady' to all shards + shard.on(ShardEvents.Ready, () => { + readyShards.add(shard.id); + if (readyShards.size === manager.totalShards) { + manager.broadcastEval((client) => client.emit('allShardsReady')); + } + }); }); manager.spawn(); diff --git a/src/utils/other/postBotStats.js b/src/utils/other/postBotStats.js index 1ca1775..2a5c668 100644 --- a/src/utils/other/postBotStats.js +++ b/src/utils/other/postBotStats.js @@ -1,9 +1,27 @@ const logger = require('../../services/logger'); const https = require('node:https'); -exports.postBotStats = (client) => { +exports.postBotStats = async (client) => { try { - const guildCount = client.guilds.cache.size; + if (client.shard.ids[0] !== 0) { + return logger.debug(`Shard ${client.shard.ids[0]}> Shard is not the first shard, not posting stats.`); + } + + logger.debug( + client.shard.ids, + `Shard ${client.shard.ids[0]}> Posting stats to bot lists from client with shard id 0.` + ); + + let guildCount = 0; + + await client.shard + .fetchClientValues('guilds.cache.size') + .then((results) => { + guildCount = results.reduce((acc, guildCount) => acc + guildCount, 0); + }) + .catch((error) => { + logger.error(error, `Shard ${client.shard.ids[0]}> Failed to fetch client values from shards.`); + }); /* eslint-disable camelcase */ const sites = [ @@ -51,7 +69,7 @@ exports.postBotStats = (client) => { } ]; - logger.info(`Posting stats to bot lists with guildCount ${guildCount}...`); + logger.info(`Shard ${client.shard.ids[0]}> Posting stats to bot lists with guildCount ${guildCount}...`); sites.map((site) => { let options = { protocol: 'https:', @@ -75,6 +93,6 @@ exports.postBotStats = (client) => { request.end(); }); } catch (error) { - logger.error(error, 'Failed to post stats to bot lists'); + logger.error(error, `Shard ${client.shard.ids[0]}> Failed to post stats to bot lists.`); } };