From 7eb4d4858ffa74223da19d5ca8e160d566150006 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Mon, 21 Aug 2023 11:06:36 +0200 Subject: [PATCH 1/3] fix: add distube ytdl core version to system status --- src/commands/system/systemstatus.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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`; From 173b234b0da7571934cb9e63580ae3dc8ae61662 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Mon, 21 Aug 2023 11:45:23 +0200 Subject: [PATCH 2/3] feat: added /shards system command --- src/commands/system/shards.js | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/commands/system/shards.js diff --git a/src/commands/system/shards.js b/src/commands/system/shards.js new file mode 100644 index 0000000..0b99d75 --- /dev/null +++ b/src/commands/system/shards.js @@ -0,0 +1,70 @@ +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), + 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; + 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.`); + }); + + systemStatusString = shardInfoList + .map((shardInfo) => { + string = ''; + string += `**Shard id:** \`${shardInfo.shardId}\`\n`; + string += `**Guild count:** \`${shardInfo.guildCount}\`\n`; + string += `**Guild member count:** \`${shardInfo.guildMemberCount}\`\n`; + string += `**Active voice connections:** \`${shardInfo.playerStatistics.activeVoiceConnections}\`\n`; + string += `**Total tracks:** \`${shardInfo.playerStatistics.totalTracks}\`\n`; + string += `**Total listeners:** \`${shardInfo.playerStatistics.totalListeners}\`\n`; + return string; + }) + .join('\n'); + + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription(`**${embedOptions.icons.server} Shards overview**\n` + systemStatusString) + .setColor(embedOptions.colors.info) + .setFooter({ text: `Shard id for this guild: ${client.shard.ids[0]}` }) + ] + }); + } +}; From 825db5ea5a45015d22149b9e68330faf8b22d2bd Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Mon, 21 Aug 2023 17:40:34 +0200 Subject: [PATCH 3/3] feat: Update shards command --- src/commands/system/shards.js | 65 +++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/src/commands/system/shards.js b/src/commands/system/shards.js index 0b99d75..6ca61b4 100644 --- a/src/commands/system/shards.js +++ b/src/commands/system/shards.js @@ -12,7 +12,14 @@ module.exports = { .setName('shards') .setDescription('Show information about the running shards.') .setDMPermission(false) - .setNSFW(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; @@ -39,31 +46,59 @@ module.exports = { }) .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.`); }); - systemStatusString = shardInfoList - .map((shardInfo) => { - string = ''; - string += `**Shard id:** \`${shardInfo.shardId}\`\n`; - string += `**Guild count:** \`${shardInfo.guildCount}\`\n`; - string += `**Guild member count:** \`${shardInfo.guildMemberCount}\`\n`; - string += `**Active voice connections:** \`${shardInfo.playerStatistics.activeVoiceConnections}\`\n`; - string += `**Total tracks:** \`${shardInfo.playerStatistics.totalTracks}\`\n`; - string += `**Total listeners:** \`${shardInfo.playerStatistics.totalListeners}\`\n`; - return string; - }) - .join('\n'); + 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} Shards overview**\n` + systemStatusString) + .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 for this guild: ${client.shard.ids[0]}` }) + .setFooter({ text: `Shard id: ${client.shard.ids[0]}, page ${pageIndex + 1} of ${totalPages}` }) ] }); }