From cd9d5248acae76a48930d77842cf7845fb1fbe0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81vila?= Date: Wed, 15 Sep 2021 23:12:48 +0100 Subject: [PATCH] Join the message sender's channel if possible --- src/commands/play.ts | 6 +++--- src/services/natural-language-commands.ts | 14 +++++++------- src/utils/channels.ts | 14 +++++++++++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/commands/play.ts b/src/commands/play.ts index c301fe9..a67b318 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -4,7 +4,7 @@ import {TYPES} from '../types'; import {inject, injectable} from 'inversify'; import {QueuedSong, STATUS} from '../services/player'; import PlayerManager from '../managers/player'; -import {getMostPopularVoiceChannel} from '../utils/channels'; +import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels'; import LoadingMessage from '../utils/loading-message'; import errorMsg from '../utils/error-msg'; import Command from '.'; @@ -36,8 +36,8 @@ export default class implements Command { this.getSongs = getSongs; } - public async execute(msg: Message, args: string []): Promise { - const [targetVoiceChannel] = getMostPopularVoiceChannel(msg.guild!); + public async execute(msg: Message, args: string[]): Promise { + const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!); const res = new LoadingMessage(msg.channel as TextChannel); await res.start(); diff --git a/src/services/natural-language-commands.ts b/src/services/natural-language-commands.ts index 608f574..442cbb0 100644 --- a/src/services/natural-language-commands.ts +++ b/src/services/natural-language-commands.ts @@ -1,9 +1,9 @@ import {inject, injectable} from 'inversify'; -import {Message, Guild} from 'discord.js'; +import {Message, Guild, GuildMember} from 'discord.js'; import {TYPES} from '../types'; import PlayerManager from '../managers/player'; import {QueuedSong} from '../services/player'; -import {getMostPopularVoiceChannel} from '../utils/channels'; +import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels'; @injectable() export default class { @@ -24,7 +24,7 @@ export default class { if (msg.content.toLowerCase().includes('packers')) { await Promise.all([ msg.channel.send('GO PACKERS GO!!!'), - this.playClip(msg.guild!, {title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false}, 8, 10) + this.playClip(msg.guild!, msg.member!, {title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false}, 8, 10) ]); return true; @@ -33,7 +33,7 @@ export default class { if (msg.content.toLowerCase().includes('bears')) { await Promise.all([ msg.channel.send('F*** THE BEARS'), - this.playClip(msg.guild!, {title: 'GO PACKERS!', artist: 'Charlie Berens', url: 'https://www.youtube.com/watch?v=UaqlE9Pyy_Q', length: 385, playlist: null, isLive: false}, 358, 5.5) + this.playClip(msg.guild!, msg.member!, {title: 'GO PACKERS!', artist: 'Charlie Berens', url: 'https://www.youtube.com/watch?v=UaqlE9Pyy_Q', length: 385, playlist: null, isLive: false}, 358, 5.5) ]); return true; @@ -42,7 +42,7 @@ export default class { if (msg.content.toLowerCase().includes('bitconnect')) { await Promise.all([ msg.channel.send('🌊 🌊 🌊 🌊'), - this.playClip(msg.guild!, {title: 'BITCONNEEECCT', artist: 'Carlos Matos', url: 'https://www.youtube.com/watch?v=lCcwn6bGUtU', length: 227, playlist: null, isLive: false}, 50, 13) + this.playClip(msg.guild!, msg.member!, {title: 'BITCONNEEECCT', artist: 'Carlos Matos', url: 'https://www.youtube.com/watch?v=lCcwn6bGUtU', length: 227, playlist: null, isLive: false}, 50, 13) ]); return true; @@ -51,10 +51,10 @@ export default class { return false; } - private async playClip(guild: Guild, song: QueuedSong, position: number, duration: number): Promise { + private async playClip(guild: Guild, member: GuildMember, song: QueuedSong, position: number, duration: number): Promise { const player = this.playerManager.get(guild.id); - const [channel, n] = getMostPopularVoiceChannel(guild); + const [channel, n] = getMemberVoiceChannel(member) ?? getMostPopularVoiceChannel(guild); if (!player.voiceConnection && n === 0) { return; diff --git a/src/utils/channels.ts b/src/utils/channels.ts index b27f5a6..f46e469 100644 --- a/src/utils/channels.ts +++ b/src/utils/channels.ts @@ -1,4 +1,4 @@ -import {Guild, VoiceChannel, User} from 'discord.js'; +import {Guild, VoiceChannel, User, GuildMember} from 'discord.js'; export const isUserInVoice = (guild: Guild, user: User): boolean => { let inVoice = false; @@ -20,6 +20,18 @@ export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.mem return s; }, 0); +export const getMemberVoiceChannel = (member?: GuildMember): [VoiceChannel, number] | null => { + const channel = member?.voice?.channel; + if (channel && channel.type === 'voice') { + return [ + channel, + getSizeWithoutBots(channel) + ]; + } + + return null; +}; + export const getMostPopularVoiceChannel = (guild: Guild): [VoiceChannel, number] => { interface PopularResult { n: number;