Join the message sender's channel if possible

This commit is contained in:
Luis Ávila 2021-09-15 23:12:48 +01:00
parent c97206bb8b
commit cd9d5248ac
No known key found for this signature in database
GPG key ID: 88BC24179A6B16E0
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 {inject, injectable} from 'inversify';
import {QueuedSong, STATUS} from '../services/player'; import {QueuedSong, STATUS} from '../services/player';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import {getMostPopularVoiceChannel} from '../utils/channels'; import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels';
import LoadingMessage from '../utils/loading-message'; import LoadingMessage from '../utils/loading-message';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg';
import Command from '.'; import Command from '.';
@ -36,8 +36,8 @@ export default class implements Command {
this.getSongs = getSongs; this.getSongs = getSongs;
} }
public async execute(msg: Message, args: string []): Promise<void> { public async execute(msg: Message, args: string[]): Promise<void> {
const [targetVoiceChannel] = getMostPopularVoiceChannel(msg.guild!); const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!);
const res = new LoadingMessage(msg.channel as TextChannel); const res = new LoadingMessage(msg.channel as TextChannel);
await res.start(); await res.start();

View file

@ -1,9 +1,9 @@
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import {Message, Guild} from 'discord.js'; import {Message, Guild, GuildMember} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import {QueuedSong} from '../services/player'; import {QueuedSong} from '../services/player';
import {getMostPopularVoiceChannel} from '../utils/channels'; import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels';
@injectable() @injectable()
export default class { export default class {
@ -24,7 +24,7 @@ export default class {
if (msg.content.toLowerCase().includes('packers')) { if (msg.content.toLowerCase().includes('packers')) {
await Promise.all([ await Promise.all([
msg.channel.send('GO PACKERS GO!!!'), 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; return true;
@ -33,7 +33,7 @@ export default class {
if (msg.content.toLowerCase().includes('bears')) { if (msg.content.toLowerCase().includes('bears')) {
await Promise.all([ await Promise.all([
msg.channel.send('F*** THE BEARS'), 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; return true;
@ -42,7 +42,7 @@ export default class {
if (msg.content.toLowerCase().includes('bitconnect')) { if (msg.content.toLowerCase().includes('bitconnect')) {
await Promise.all([ await Promise.all([
msg.channel.send('🌊 🌊 🌊 🌊'), 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; return true;
@ -51,10 +51,10 @@ export default class {
return false; 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 player = this.playerManager.get(guild.id);
const [channel, n] = getMostPopularVoiceChannel(guild); const [channel, n] = getMemberVoiceChannel(member) ?? getMostPopularVoiceChannel(guild);
if (!player.voiceConnection && n === 0) { if (!player.voiceConnection && n === 0) {
return; 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 => { export const isUserInVoice = (guild: Guild, user: User): boolean => {
let inVoice = false; let inVoice = false;
@ -20,6 +20,18 @@ export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.mem
return s; return s;
}, 0); }, 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] => { export const getMostPopularVoiceChannel = (guild: Guild): [VoiceChannel, number] => {
interface PopularResult { interface PopularResult {
n: number; n: number;