aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2020-03-16 20:14:15 -0500
committerMax Isom <[email protected]>2020-03-16 20:14:15 -0500
commitac21b5657ba47ef9081c80424d5f8ae39b149f35 (patch)
tree1ba121343bb3ae33b6345afc217e2c29b92c23a9 /src/utils
parent32cb3ca4ae6a419f64e413ba5c8c543593a927b1 (diff)
downloadmuse-ac21b5657ba47ef9081c80424d5f8ae39b149f35.tar.xz
muse-ac21b5657ba47ef9081c80424d5f8ae39b149f35.zip
Add auto disconnect
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/channels.ts22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/utils/channels.ts b/src/utils/channels.ts
index d138c45..bd56dd4 100644
--- a/src/utils/channels.ts
+++ b/src/utils/channels.ts
@@ -1,6 +1,14 @@
import {Guild, VoiceChannel} from 'discord.js';
-export const getMostPopularVoiceChannel = (guild: Guild, min = 0): VoiceChannel => {
+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] => {
interface PopularResult {
n: number;
channel: VoiceChannel | null;
@@ -9,18 +17,16 @@ export const getMostPopularVoiceChannel = (guild: Guild, min = 0): VoiceChannel
const voiceChannels: PopularResult[] = [];
for (const [_, channel] of guild.channels.cache) {
- if (channel.type === 'voice' && channel.members.size >= min) {
+ if (channel.type === 'voice') {
+ const size = getSizeWithoutBots(channel as VoiceChannel);
+
voiceChannels.push({
channel: channel as VoiceChannel,
- n: channel.members.size
+ n: size
});
}
}
- if (voiceChannels.length === 0) {
- throw new Error('No voice channels meet minimum size');
- }
-
// Find most popular channel
const popularChannel = voiceChannels.reduce((popular: PopularResult, elem: PopularResult) => {
if (elem.n > popular.n) {
@@ -31,7 +37,7 @@ export const getMostPopularVoiceChannel = (guild: Guild, min = 0): VoiceChannel
}, {n: -1, channel: null});
if (popularChannel.channel) {
- return popularChannel.channel;
+ return [popularChannel.channel, popularChannel.n];
}
throw new Error();