feat: update ready events to handle sharding

This commit is contained in:
Marius Begby committed 2023-07-21 12:27:49 +02:00
1 parent 006f2fab51
commit 184b528f56
4 files changed
+55 -17

No files matched your search

+22 -4
View File
@@ -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.`);
}
};