This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update ready events to handle sharding
- Loading branch information
1 parent
006f2fa
commit 184b528
Showing
4 changed files
with
55 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters