mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 22:15:29 +00:00
fix: refactored guilds command
This commit is contained in:
1 parent
fca1a90489
commit
56b25f9b97
1 file changed
+48
-35
@@ -1,5 +1,7 @@
|
|||||||
import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js';
|
import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js';
|
||||||
|
import { Logger } from 'pino';
|
||||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||||
|
import { ExtendedClient } from '../../../types/clientTypes';
|
||||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
||||||
import { checkValidGuildId } from '../../../utils/validation/systemCommandValidator';
|
import { checkValidGuildId } from '../../../utils/validation/systemCommandValidator';
|
||||||
|
|
||||||
@@ -18,45 +20,15 @@ class GuildsCommand extends BaseSlashCommandInteraction {
|
|||||||
|
|
||||||
await this.runValidators({ interaction, executionId }, [checkValidGuildId]);
|
await this.runValidators({ interaction, executionId }, [checkValidGuildId]);
|
||||||
|
|
||||||
let shardGuilds: Guild[] = [];
|
const shardGuilds = await this.fetchShardGuilds(client!, logger);
|
||||||
let totalGuildCount: number = 0;
|
const totalGuildCount = shardGuilds.length;
|
||||||
|
|
||||||
logger.debug('Fetching guilds from all shards.');
|
|
||||||
await client!
|
|
||||||
.shard!.broadcastEval((c) => {
|
|
||||||
return c.guilds.cache.map((guild) => {
|
|
||||||
return guild;
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.then((guilds) => {
|
|
||||||
shardGuilds = guilds.flat(1) as Guild[];
|
|
||||||
});
|
|
||||||
|
|
||||||
totalGuildCount = shardGuilds.length;
|
|
||||||
|
|
||||||
logger.debug(`Successfully fetched ${totalGuildCount} guilds.`);
|
logger.debug(`Successfully fetched ${totalGuildCount} guilds.`);
|
||||||
|
|
||||||
const guildListFormatted: string = shardGuilds
|
const guildListFormatted = this.formatGuildListAsString(shardGuilds);
|
||||||
.map((guild) => {
|
const totalMemberCount = this.calculateTotalMemberCount(shardGuilds);
|
||||||
return {
|
|
||||||
name: guild.name,
|
|
||||||
memberCount: guild.memberCount
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.sort((a, b) => b.memberCount - a.memberCount)
|
|
||||||
.slice(0, 25)
|
|
||||||
.map((guild, index) => `${index + 1}. \`${guild.name} (#${guild.memberCount})\``)
|
|
||||||
.join('\n');
|
|
||||||
|
|
||||||
const totalMemberCount: number = shardGuilds.reduce((a, b) => a + b.memberCount, 0);
|
let embedDescription = this.buildEmbedDescription(totalGuildCount, guildListFormatted, totalMemberCount);
|
||||||
|
|
||||||
let embedDescription =
|
|
||||||
`**${this.embedOptions.icons.bot} ${
|
|
||||||
totalGuildCount < 25 ? `Top ${totalGuildCount} guilds` : 'Top 25 guilds'
|
|
||||||
} by member count (${totalGuildCount} total)**\n${guildListFormatted}` +
|
|
||||||
`\n\n**Total members:** **\`${totalMemberCount}\`**`;
|
|
||||||
|
|
||||||
logger.debug('Transformed guild into into embed description.');
|
|
||||||
|
|
||||||
if (embedDescription.length >= 4000) {
|
if (embedDescription.length >= 4000) {
|
||||||
logger.debug('Embed description is too long, truncating.');
|
logger.debug('Embed description is too long, truncating.');
|
||||||
@@ -68,6 +40,47 @@ class GuildsCommand extends BaseSlashCommandInteraction {
|
|||||||
embeds: [new EmbedBuilder().setDescription(embedDescription).setColor(this.embedOptions.colors.info)]
|
embeds: [new EmbedBuilder().setDescription(embedDescription).setColor(this.embedOptions.colors.info)]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async fetchShardGuilds(client: ExtendedClient, logger: Logger): Promise<Guild[]> {
|
||||||
|
logger.debug('Fetching guilds from all shards.');
|
||||||
|
const guilds = await client!.shard!.broadcastEval((c) => c.guilds.cache.map((guild) => guild));
|
||||||
|
return guilds.flat(1) as Guild[];
|
||||||
|
}
|
||||||
|
|
||||||
|
private formatGuildListAsString(shardGuilds: Guild[]): string {
|
||||||
|
const guilds = this.mapGuilds(shardGuilds);
|
||||||
|
const topGuilds = this.getTopGuilds(guilds);
|
||||||
|
return this.guildsToString(topGuilds);
|
||||||
|
}
|
||||||
|
|
||||||
|
private mapGuilds(shardGuilds: Guild[]) {
|
||||||
|
return shardGuilds.map((guild) => ({ name: guild.name, memberCount: guild.memberCount }));
|
||||||
|
}
|
||||||
|
|
||||||
|
private getTopGuilds(guilds: { name: string; memberCount: number }[]) {
|
||||||
|
return guilds.sort((a, b) => b.memberCount - a.memberCount).slice(0, 25);
|
||||||
|
}
|
||||||
|
|
||||||
|
private guildsToString(guilds: { name: string; memberCount: number }[]) {
|
||||||
|
return guilds.map((guild, index) => `${index + 1}. \`${guild.name} (#${guild.memberCount})\``).join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
private calculateTotalMemberCount(shardGuilds: Guild[]): number {
|
||||||
|
return shardGuilds.reduce((a, b) => a + b.memberCount, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private buildEmbedDescription(
|
||||||
|
totalGuildCount: number,
|
||||||
|
guildListFormattedString: string,
|
||||||
|
totalMemberCount: number
|
||||||
|
): string {
|
||||||
|
const topGuildsString = totalGuildCount < 25 ? `Top ${totalGuildCount} guilds` : 'Top 25 guilds';
|
||||||
|
return (
|
||||||
|
`**${this.embedOptions.icons.bot} ${topGuildsString} by member count (${totalGuildCount} total)**\n` +
|
||||||
|
`${guildListFormattedString}\n\n` +
|
||||||
|
`**Total members:** **\`${totalMemberCount}\`**`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new GuildsCommand();
|
export default new GuildsCommand();
|
||||||
Reference in new issue
Block a user