diff --git a/src/commands/system/shards.js b/src/commands/system/shards.js new file mode 100644 index 0000000..6ca61b4 --- /dev/null +++ b/src/commands/system/shards.js @@ -0,0 +1,105 @@ +const logger = require('../../services/logger'); +const config = require('config'); +const embedOptions = config.get('embedOptions'); +const { notValidGuildId } = require('../../utils/validation/systemCommandValidator'); +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); + +module.exports = { + isSystemCommand: true, + isNew: false, + isBeta: false, + data: new SlashCommandBuilder() + .setName('shards') + .setDescription('Show information about the running shards.') + .setDMPermission(false) + .setNSFW(false) + .addBooleanOption((option) => + option + .setName('sort') + .setDescription('Whether or not to sort by most active voice connections.') + .setRequired(false) + ) + .addNumberOption((option) => option.setName('page').setDescription('Page number to show').setMinValue(1)), + execute: async ({ interaction, client }) => { + if (await notValidGuildId(interaction)) { + return; + } + + let shardInfoList = []; + + await client.shard + .broadcastEval((shardClient) => { + /* eslint-disable no-undef */ + let playerStats = player.generateStatistics(); + let shardInfo = { + shardId: shardClient.shard.ids[0], + guildCount: shardClient.guilds.cache.size, + guildMemberCount: shardClient.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0), + playerStatistics: { + activeVoiceConnections: playerStats.queues.length, + totalTracks: playerStats.queues.reduce((acc, queue) => acc + queue.tracksCount, 0), + totalListeners: playerStats.queues.reduce((acc, queue) => acc + queue.listeners, 0) + } + }; + + return shardInfo; + }) + .then((results) => { + shardInfoList = results; + const sortByActiveConnections = interaction.options.getBoolean('sort', false); + if (sortByActiveConnections) { + shardInfoList = shardInfoList.sort( + (a, b) => b.playerStatistics.activeVoiceConnections - a.playerStatistics.activeVoiceConnections + ); + } + logger.debug(results, `[Shard ${client.shard.ids[0]}] Fetched shardInfo from each shard.`); + }) + .catch((error) => { + logger.error(error, `[Shard ${client.shard.ids[0]}] Failed to fetch client values from shards.`); + }); + + const shardCount = shardInfoList.length; + const totalPages = Math.ceil(shardCount / 10) || 1; + const pageIndex = (interaction.options.getNumber('page') || 1) - 1; + + const currentPageShards = shardInfoList.slice(pageIndex * 10, pageIndex * 10 + 10); + + const evenShardIndexes = currentPageShards.filter((shard, index) => index % 2 === 0); + const oddShardIndexes = currentPageShards.filter((shard, index) => index % 2 !== 0); + + function shardInfoToString(shard) { + let string = ''; + string += `**Shard ${shard.shardId}** - Guilds: ${shard.guildCount.toLocaleString( + 'en-US' + )} (${shard.guildMemberCount.toLocaleString('en-US')})\n`; + string += `**┗** Connections: ${shard.playerStatistics.activeVoiceConnections.toLocaleString('en-US')}\n`; + string += `**┗** Tracks: ${shard.playerStatistics.totalTracks.toLocaleString('en-US')}\n`; + string += `**┗** Listeners: ${shard.playerStatistics.totalListeners.toLocaleString('en-US')}\n`; + return string; + } + + const evenShardIndexesString = evenShardIndexes.map(shardInfoToString).join('\n') + 'ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ'; + const oddShardIndexesString = oddShardIndexes.map(shardInfoToString).join('\n'); + + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription(`**${embedOptions.icons.server} Shard overview - ${shardCount} total shards**\n`) + .addFields( + { + name: ' ', + value: evenShardIndexesString, + inline: true + }, + { + name: ' ', + value: oddShardIndexesString, + inline: true + } + ) + .setColor(embedOptions.colors.info) + .setFooter({ text: `Shard id: ${client.shard.ids[0]}, page ${pageIndex + 1} of ${totalPages}` }) + ] + }); + } +}; diff --git a/src/commands/system/systemstatus.js b/src/commands/system/systemstatus.js index cc9037f..a431647 100644 --- a/src/commands/system/systemstatus.js +++ b/src/commands/system/systemstatus.js @@ -43,6 +43,7 @@ module.exports = { const discordPlayerVersion = dependencies['discord-player']; const extractorVersion = dependencies['@discord-player/extractor']; const mediaplexVersion = dependencies['mediaplex']; + const distubeYtdlVersion = dependencies['@distube/ytdl-core']; await client.shard .broadcastEval(() => { @@ -105,7 +106,8 @@ module.exports = { `**┗ ${opusVersion}** @discord-player/opus\n` + `**┗ ${extractorVersion}** @discord-player/extractor\n` + `**${voiceVersion}** discord-voip\n` + - `**${mediaplexVersion}** mediaplex`; + `**${mediaplexVersion}** mediaplex\n` + + `**${distubeYtdlVersion}** @distube/ytdl-core`; const discordStatusString = `**${client.ws.ping} ms** Discord API latency`;