muse/src/utils/channels.ts

45 lines
1 KiB
TypeScript
Raw Normal View History

2020-03-10 17:58:09 +01:00
import {Guild, VoiceChannel} from 'discord.js';
2020-03-17 02:14:15 +01:00
export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.members.array().reduce((s, member) => {
if (!member.user.bot) {
s++;
}
return s;
}, 0);
export const getMostPopularVoiceChannel = (guild: Guild): [VoiceChannel, number] => {
2020-03-10 17:58:09 +01:00
interface PopularResult {
n: number;
channel: VoiceChannel | null;
}
const voiceChannels: PopularResult[] = [];
for (const [_, channel] of guild.channels.cache) {
2020-03-17 02:14:15 +01:00
if (channel.type === 'voice') {
const size = getSizeWithoutBots(channel as VoiceChannel);
2020-03-10 17:58:09 +01:00
voiceChannels.push({
channel: channel as VoiceChannel,
2020-03-17 02:14:15 +01:00
n: size
2020-03-10 17:58:09 +01:00
});
}
}
// Find most popular channel
const popularChannel = voiceChannels.reduce((popular: PopularResult, elem: PopularResult) => {
if (elem.n > popular.n) {
return elem;
}
return popular;
}, {n: -1, channel: null});
if (popularChannel.channel) {
2020-03-17 02:14:15 +01:00
return [popularChannel.channel, popularChannel.n];
2020-03-10 17:58:09 +01:00
}
throw new Error();
};