From a2950ed722d5ed1f0e3d31922d796836ec1b18da Mon Sep 17 00:00:00 2001 From: Max Isom Date: Fri, 27 Mar 2020 15:44:49 -0500 Subject: [PATCH] Add bears handler and fix natural language handler priority --- src/bot.ts | 6 +- src/services/natural-language-commands.ts | 109 +++++++++++++--------- 2 files changed, 66 insertions(+), 49 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index de59ed2..296c06b 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -48,13 +48,13 @@ export default class { return this.client.emit('guildCreate', msg.guild); } - if (await this.naturalLanguage.execute(msg)) { + const {prefix, channel} = settings; + + if (!msg.content.startsWith(prefix) && !msg.author.bot && msg.channel.id === channel && await this.naturalLanguage.execute(msg)) { // Natural language command handled message return; } - const {prefix, channel} = settings; - if (!msg.content.startsWith(prefix) || msg.author.bot || msg.channel.id !== channel) { return; } diff --git a/src/services/natural-language-commands.ts b/src/services/natural-language-commands.ts index ce89ce0..104cd4f 100644 --- a/src/services/natural-language-commands.ts +++ b/src/services/natural-language-commands.ts @@ -1,7 +1,8 @@ import {inject, injectable} from 'inversify'; -import {Message} from 'discord.js'; +import {Message, Guild} from 'discord.js'; import {TYPES} from '../types'; import PlayerManager from '../managers/player'; +import {QueuedSong} from '../services/player'; import {getMostPopularVoiceChannel} from '../utils/channels'; @injectable() @@ -20,56 +21,72 @@ export default class { return true; } - if (msg.content.includes('packers')) { - const player = this.playerManager.get(msg.guild!.id); + 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) + ]); - const [channel, n] = getMostPopularVoiceChannel(msg.guild!); + return true; + } - await msg.channel.send('GO PACKERS GO!!!'); + 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) + ]); - if (!player.voiceConnection && n === 0) { - return false; - } - - if (!player.voiceConnection) { - await player.connect(channel); - } - - const isPlaying = player.getCurrent() !== null; - let oldPosition = 0; - - player.add({title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false}, {immediate: true}); - - if (isPlaying) { - oldPosition = player.getPosition(); - - player.manualForward(); - } - - await player.seek(8); - - return new Promise((resolve, reject) => { - try { - setTimeout(async () => { - if (player.getCurrent()?.title === 'GO PACKERS!') { - player.removeCurrent(); - - if (isPlaying) { - await player.back(); - await player.seek(oldPosition); - } else { - player.disconnect(); - } - } - - resolve(true); - }, 10000); - } catch (error) { - reject(error); - } - }); + return true; } return false; } + + private async playClip(guild: Guild, song: QueuedSong, position: number, duration: number): Promise { + const player = this.playerManager.get(guild.id); + + const [channel, n] = getMostPopularVoiceChannel(guild); + + if (!player.voiceConnection && n === 0) { + return; + } + + if (!player.voiceConnection) { + await player.connect(channel); + } + + const isPlaying = player.getCurrent() !== null; + let oldPosition = 0; + + player.add(song, {immediate: true}); + + if (isPlaying) { + oldPosition = player.getPosition(); + + player.manualForward(); + } + + await player.seek(position); + + return new Promise((resolve, reject) => { + try { + setTimeout(async () => { + if (player.getCurrent()?.title === song.title) { + player.removeCurrent(); + + if (isPlaying) { + await player.back(); + await player.seek(oldPosition); + } else { + player.disconnect(); + } + } + + resolve(); + }, duration * 1000); + } catch (error) { + reject(error); + } + }); + } }