diff --git a/src/utils/shardUtils.ts b/src/utils/shardUtils.ts index 1a67d3c..623e842 100644 --- a/src/utils/shardUtils.ts +++ b/src/utils/shardUtils.ts @@ -2,26 +2,28 @@ import { GuildQueueStatisticsMetadata } from 'discord-player'; import { Client, Guild } from 'discord.js'; import { PlayerStatistics, ShardPlayerStatistics } from '../types/utilTypes'; -async function fetchGuildsPerShard(client?: Client): Promise { +async function fetchGuildsPerShard(client?: Client): Promise[]> { if (!client || !client.shard) { throw new Error('Client is undefined or not sharded.'); } try { - return (await client.shard.fetchClientValues('guilds.cache')) as Guild[][]; + return (await client.shard.fetchClientValues('guilds.cache')) as Map[]; } catch (error) { throw new Error('Failed to fetch client values from shards.'); } } -function calculateTotalCountForShard(guildList: Guild[]) { +function calculateTotalCountForShard(guildListMap: Map) { + const guildList: Guild[] = [...guildListMap.values()]; + const totalGuildCount: number = guildList.length; const totalMemberCount: number = guildList.reduce((acc: number, guild: Guild) => acc + guild.memberCount, 0); return { totalGuildCount, totalMemberCount }; } -function calculateTotalCountForAllShards(guildListPerShard: Guild[][]) { +function calculateTotalCountForAllShards(guildListPerShard: Map[]) { return guildListPerShard.reduce( (totals, guildList) => { const { totalGuildCount, totalMemberCount } = calculateTotalCountForShard(guildList); @@ -35,7 +37,7 @@ function calculateTotalCountForAllShards(guildListPerShard: Guild[][]) { } export async function fetchTotalGuildStatistics(client?: Client) { - const guildsPerShard: Guild[][] = await fetchGuildsPerShard(client); + const guildsPerShard: Map[] = await fetchGuildsPerShard(client); const { totalGuildCount, totalMemberCount } = calculateTotalCountForAllShards(guildsPerShard); return { totalGuildCount, totalMemberCount };