From f25467a41db64b239eb3f248f4be7dbecec0f14f Mon Sep 17 00:00:00 2001 From: Max Isom Date: Thu, 19 Mar 2020 19:16:07 -0500 Subject: [PATCH] Small bug fixes --- src/commands/play.ts | 14 +++++++++----- src/commands/skip.ts | 7 +++---- src/commands/unskip.ts | 4 ++-- src/services/get-songs.ts | 2 +- src/services/player.ts | 4 ++++ src/utils/loading-message.ts | 14 ++++++++++---- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/commands/play.ts b/src/commands/play.ts index 629a720..30f6314 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -102,7 +102,11 @@ export default class implements Command { } if (nSongsNotFound !== 0) { - extraMsg += `and ${nSongsNotFound.toString()} songs were not found`; + if (nSongsNotFound === 1) { + extraMsg += 'and 1 song was not found'; + } else { + extraMsg += `and ${nSongsNotFound.toString()} songs were not found`; + } } newSongs.push(...convertedSongs); @@ -140,12 +144,12 @@ export default class implements Command { await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`); } - if (player.voiceConnection === null) { - await player.connect(targetVoiceChannel); - } - if (queueOldSize === 0 && !wasPlayingSong) { // Only auto-play if queue was empty before and nothing was playing + if (player.voiceConnection === null) { + await player.connect(targetVoiceChannel); + } + await player.play(); } } diff --git a/src/commands/skip.ts b/src/commands/skip.ts index cc5c1c4..1eb23a0 100644 --- a/src/commands/skip.ts +++ b/src/commands/skip.ts @@ -25,19 +25,18 @@ export default class implements Command { public async execute(msg: Message, _: string []): Promise { const queue = this.queueManager.get(msg.guild!.id); + const player = this.playerManager.get(msg.guild!.id); try { queue.forward(); + player.resetPosition(); if (queue.isEmpty() && !queue.getCurrent()) { - this.playerManager.get(msg.guild!.id).disconnect(); - } else { - await this.playerManager.get(msg.guild!.id).play(); + player.disconnect(); } await msg.channel.send('keep \'er movin\''); } catch (_) { - console.log(_); await msg.channel.send('no song to skip to'); } } diff --git a/src/commands/unskip.ts b/src/commands/unskip.ts index ff4718a..a48d819 100644 --- a/src/commands/unskip.ts +++ b/src/commands/unskip.ts @@ -26,11 +26,11 @@ export default class implements Command { public async execute(msg: Message, _: string []): Promise { const queue = this.queueManager.get(msg.guild!.id); + const player = this.playerManager.get(msg.guild!.id); try { queue.back(); - - await this.playerManager.get(msg.guild!.id).play(); + player.resetPosition(); await msg.channel.send('back \'er up\''); } catch (_) { diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index 08d5e90..ed1d125 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -176,7 +176,7 @@ export default class { private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, playlist: QueuedPlaylist | null): Promise { try { - const {items} = await ytsr(`${track.name} ${track.artists[0].name} offical`, {limit: 5}); + const {items} = await ytsr(`"${track.name}" "${track.artists[0].name}" offical`, {limit: 5}); const video = items.find((item: { type: string }) => item.type === 'video'); if (!video) { diff --git a/src/services/player.ts b/src/services/player.ts index 27c6b33..0fd7227 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -140,6 +140,10 @@ export default class { this.stopTrackingPosition(); } + resetPosition(): void { + this.positionInSeconds = 0; + } + private getCachedPath(url: string): string { return path.join(this.cacheDir, hasha(url)); } diff --git a/src/utils/loading-message.ts b/src/utils/loading-message.ts index cf48d95..dd3e9b9 100644 --- a/src/utils/loading-message.ts +++ b/src/utils/loading-message.ts @@ -1,11 +1,14 @@ import {TextChannel, Message} from 'discord.js'; import delay from 'delay'; +const INITAL_DELAY = 500; +const PERIOD = 500; + export default class { + public isStopped = true; private readonly channel: TextChannel; private readonly text: string; private msg!: Message; - private isStopped = false; constructor(channel: TextChannel, text = 'cows! count \'em') { this.channel = channel; @@ -15,22 +18,25 @@ export default class { async start(): Promise { this.msg = await this.channel.send(this.text); - const period = 500; - const icons = ['🐮', '🐴', '🐄']; const reactions = []; let i = 0; let isRemoving = false; + + this.isStopped = false; + (async () => { + await delay(INITAL_DELAY); + while (!this.isStopped) { if (reactions.length === icons.length) { isRemoving = true; } // eslint-disable-next-line no-await-in-loop - await delay(period); + await delay(PERIOD); if (isRemoving) { const reactionToRemove = reactions.shift();