aboutsummaryrefslogtreecommitdiff
path: root/src/utils/channels.ts
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2020-03-10 11:58:09 -0500
committerMax Isom <[email protected]>2020-03-10 11:58:09 -0500
commit8eb4c8a6c06f672cb50efae5ea30215d465000af (patch)
tree938a439e254694b4ed283d71a303b442ba739104 /src/utils/channels.ts
parent652cc2e5efed6ddef593570ee90634cbc1c452bb (diff)
downloadmuse-8eb4c8a6c06f672cb50efae5ea30215d465000af.tar.xz
muse-8eb4c8a6c06f672cb50efae5ea30215d465000af.zip
Basic play functionality
Diffstat (limited to 'src/utils/channels.ts')
-rw-r--r--src/utils/channels.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/utils/channels.ts b/src/utils/channels.ts
new file mode 100644
index 0000000..d138c45
--- /dev/null
+++ b/src/utils/channels.ts
@@ -0,0 +1,38 @@
+import {Guild, VoiceChannel} from 'discord.js';
+
+export const getMostPopularVoiceChannel = (guild: Guild, min = 0): VoiceChannel => {
+ interface PopularResult {
+ n: number;
+ channel: VoiceChannel | null;
+ }
+
+ const voiceChannels: PopularResult[] = [];
+
+ for (const [_, channel] of guild.channels.cache) {
+ if (channel.type === 'voice' && channel.members.size >= min) {
+ voiceChannels.push({
+ channel: channel as VoiceChannel,
+ n: channel.members.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) {
+ return elem;
+ }
+
+ return popular;
+ }, {n: -1, channel: null});
+
+ if (popularChannel.channel) {
+ return popularChannel.channel;
+ }
+
+ throw new Error();
+};