From 825db5ea5a45015d22149b9e68330faf8b22d2bd Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Mon, 21 Aug 2023 17:40:34 +0200 Subject: [PATCH] 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}` }) ] }); }