Merge pull request #330 from luisfavila/join-user-voice-channel

This commit is contained in:
Max Isom 2021-09-16 09:36:33 -04:00 committed by GitHub
commit 81bbdb971d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View file

@ -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<void> {
const [targetVoiceChannel] = getMostPopularVoiceChannel(msg.guild!);
public async execute(msg: Message, args: string[]): Promise<void> {
const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!);
const res = new LoadingMessage(msg.channel as TextChannel);
await res.start();

View file

@ -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<void> {
private async playClip(guild: Guild, member: GuildMember, song: QueuedSong, position: number, duration: number): Promise<void> {
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;

View file

@ -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;