feat: Update shards command

This commit is contained in:
Marius Begby committed 2023-08-21 17:40:34 +02:00
1 parent 173b234b0d
commit 825db5ea5a
1 file changed
+49 -14
+49 -14
View File
@@ -12,7 +12,14 @@ module.exports = {
.setName('shards') .setName('shards')
.setDescription('Show information about the running shards.') .setDescription('Show information about the running shards.')
.setDMPermission(false) .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 }) => { execute: async ({ interaction, client }) => {
if (await notValidGuildId(interaction)) { if (await notValidGuildId(interaction)) {
return; return;
@@ -39,31 +46,59 @@ module.exports = {
}) })
.then((results) => { .then((results) => {
shardInfoList = 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.`); logger.debug(results, `[Shard ${client.shard.ids[0]}] Fetched shardInfo from each shard.`);
}) })
.catch((error) => { .catch((error) => {
logger.error(error, `[Shard ${client.shard.ids[0]}] Failed to fetch client values from shards.`); logger.error(error, `[Shard ${client.shard.ids[0]}] Failed to fetch client values from shards.`);
}); });
systemStatusString = shardInfoList const shardCount = shardInfoList.length;
.map((shardInfo) => { const totalPages = Math.ceil(shardCount / 10) || 1;
string = ''; const pageIndex = (interaction.options.getNumber('page') || 1) - 1;
string += `**Shard id:** \`${shardInfo.shardId}\`\n`;
string += `**Guild count:** \`${shardInfo.guildCount}\`\n`; const currentPageShards = shardInfoList.slice(pageIndex * 10, pageIndex * 10 + 10);
string += `**Guild member count:** \`${shardInfo.guildMemberCount}\`\n`;
string += `**Active voice connections:** \`${shardInfo.playerStatistics.activeVoiceConnections}\`\n`; const evenShardIndexes = currentPageShards.filter((shard, index) => index % 2 === 0);
string += `**Total tracks:** \`${shardInfo.playerStatistics.totalTracks}\`\n`; const oddShardIndexes = currentPageShards.filter((shard, index) => index % 2 !== 0);
string += `**Total listeners:** \`${shardInfo.playerStatistics.totalListeners}\`\n`;
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; return string;
}) }
.join('\n');
const evenShardIndexesString = evenShardIndexes.map(shardInfoToString).join('\n') + 'ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ';
const oddShardIndexesString = oddShardIndexes.map(shardInfoToString).join('\n');
return await interaction.editReply({ return await interaction.editReply({
embeds: [ embeds: [
new EmbedBuilder() 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) .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}` })
] ]
}); });
} }