diff options
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/play.ts | 212 | ||||
| -rw-r--r-- | src/commands/queue.ts | 60 | ||||
| -rw-r--r-- | src/commands/skip.ts | 16 | ||||
| -rw-r--r-- | src/commands/unskip.ts | 9 |
4 files changed, 137 insertions, 160 deletions
diff --git a/src/commands/play.ts b/src/commands/play.ts index 784885e..16d16dd 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -12,6 +12,7 @@ import errorMsg from '../utils/error-msg.js'; import Command from '.'; import GetSongs from '../services/get-songs.js'; import {prisma} from '../utils/db.js'; +import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; @injectable() export default class implements Command { @@ -57,143 +58,154 @@ export default class implements Command { const res = new LoadingMessage(msg.channel as TextChannel); await res.start(); - const player = this.playerManager.get(msg.guild!.id); + try { + const player = this.playerManager.get(msg.guild!.id); - const wasPlayingSong = player.getCurrent() !== null; + const wasPlayingSong = player.getCurrent() !== null; - if (args.length === 0) { - if (player.status === STATUS.PLAYING) { - await res.stop(errorMsg('already playing, give me a song name')); - return; - } + if (args.length === 0) { + if (player.status === STATUS.PLAYING) { + await res.stop(errorMsg('already playing, give me a song name')); + return; + } - // Must be resuming play - if (!wasPlayingSong) { - await res.stop(errorMsg('nothing to play')); - return; - } + // Must be resuming play + if (!wasPlayingSong) { + await res.stop(errorMsg('nothing to play')); + return; + } - await player.connect(targetVoiceChannel); - await player.play(); + await player.connect(targetVoiceChannel); + await player.play(); - await res.stop('the stop-and-go light is now green'); - return; - } + await Promise.all([ + res.stop('the stop-and-go light is now green'), + msg.channel.send({embeds: [buildPlayingMessageEmbed(player)]}), + ]); - const addToFrontOfQueue = args[args.length - 1] === 'i' || args[args.length - 1] === 'immediate'; - const shuffleAdditions = args[args.length - 1] === 's' || args[args.length - 1] === 'shuffle'; + return; + } - let newSongs: Array<Except<QueuedSong, 'addedInChannelId'>> = []; - let extraMsg = ''; + const addToFrontOfQueue = args[args.length - 1] === 'i' || args[args.length - 1] === 'immediate'; + const shuffleAdditions = args[args.length - 1] === 's' || args[args.length - 1] === 'shuffle'; - // Test if it's a complete URL - try { - const url = new URL(args[0]); + let newSongs: Array<Except<QueuedSong, 'addedInChannelId' | 'requestedBy'>> = []; + let extraMsg = ''; - const YOUTUBE_HOSTS = [ - 'www.youtube.com', - 'youtu.be', - 'youtube.com', - 'music.youtube.com', - 'www.music.youtube.com', - ]; + // Test if it's a complete URL + try { + const url = new URL(args[0]); - if (YOUTUBE_HOSTS.includes(url.host)) { + const YOUTUBE_HOSTS = [ + 'www.youtube.com', + 'youtu.be', + 'youtube.com', + 'music.youtube.com', + 'www.music.youtube.com', + ]; + + if (YOUTUBE_HOSTS.includes(url.host)) { // YouTube source - if (url.searchParams.get('list')) { + if (url.searchParams.get('list')) { // YouTube playlist - newSongs.push(...await this.getSongs.youtubePlaylist(url.searchParams.get('list')!)); - } else { - // Single video - const song = await this.getSongs.youtubeVideo(url.href); - - if (song) { - newSongs.push(song); + newSongs.push(...await this.getSongs.youtubePlaylist(url.searchParams.get('list')!)); } else { - await res.stop(errorMsg('that doesn\'t exist')); - return; + // Single video + const song = await this.getSongs.youtubeVideo(url.href); + + if (song) { + newSongs.push(song); + } else { + await res.stop(errorMsg('that doesn\'t exist')); + return; + } } - } - } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { - const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(args[0], playlistLimit); + } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { + const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(args[0], playlistLimit); - if (totalSongs > playlistLimit) { - extraMsg = `a random sample of ${playlistLimit} songs was taken`; - } + if (totalSongs > playlistLimit) { + extraMsg = `a random sample of ${playlistLimit} songs was taken`; + } - if (totalSongs > playlistLimit && nSongsNotFound !== 0) { - extraMsg += ' and '; - } + if (totalSongs > playlistLimit && nSongsNotFound !== 0) { + extraMsg += ' and '; + } - if (nSongsNotFound !== 0) { - if (nSongsNotFound === 1) { - extraMsg += '1 song was not found'; - } else { - extraMsg += `${nSongsNotFound.toString()} songs were not found`; + if (nSongsNotFound !== 0) { + if (nSongsNotFound === 1) { + extraMsg += '1 song was not found'; + } else { + extraMsg += `${nSongsNotFound.toString()} songs were not found`; + } } - } - newSongs.push(...convertedSongs); - } - } catch (_: unknown) { + newSongs.push(...convertedSongs); + } + } catch (_: unknown) { // Not a URL, must search YouTube - const query = addToFrontOfQueue ? args.slice(0, args.length - 1).join(' ') : args.join(' '); + const query = addToFrontOfQueue ? args.slice(0, args.length - 1).join(' ') : args.join(' '); - const song = await this.getSongs.youtubeVideoSearch(query); + const song = await this.getSongs.youtubeVideoSearch(query); - if (song) { - newSongs.push(song); - } else { - await res.stop(errorMsg('that doesn\'t exist')); + if (song) { + newSongs.push(song); + } else { + await res.stop(errorMsg('that doesn\'t exist')); + return; + } + } + + if (newSongs.length === 0) { + await res.stop(errorMsg('no songs found')); return; } - } - if (newSongs.length === 0) { - await res.stop(errorMsg('no songs found')); - return; - } + if (shuffleAdditions) { + newSongs = shuffle(newSongs); + } - if (shuffleAdditions) { - newSongs = shuffle(newSongs); - } + newSongs.forEach(song => { + player.add({...song, addedInChannelId: msg.channel.id, requestedBy: msg.author.id}, {immediate: addToFrontOfQueue}); + }); - newSongs.forEach(song => { - player.add({...song, addedInChannelId: msg.channel.id}, {immediate: addToFrontOfQueue}); - }); + const firstSong = newSongs[0]; - const firstSong = newSongs[0]; + let statusMsg = ''; - let statusMsg = ''; + if (player.voiceConnection === null) { + await player.connect(targetVoiceChannel); - if (player.voiceConnection === null) { - await player.connect(targetVoiceChannel); + // Resume / start playback + await player.play(); - // Resume / start playback - await player.play(); + if (wasPlayingSong) { + statusMsg = 'resuming playback'; + } - if (wasPlayingSong) { - statusMsg = 'resuming playback'; + await msg.channel.send({embeds: [buildPlayingMessageEmbed(player)]}); } - } - // Build response message - if (statusMsg !== '') { - if (extraMsg === '') { - extraMsg = statusMsg; - } else { - extraMsg = `${statusMsg}, ${extraMsg}`; + // Build response message + if (statusMsg !== '') { + if (extraMsg === '') { + extraMsg = statusMsg; + } else { + extraMsg = `${statusMsg}, ${extraMsg}`; + } } - } - if (extraMsg !== '') { - extraMsg = ` (${extraMsg})`; - } + if (extraMsg !== '') { + extraMsg = ` (${extraMsg})`; + } - if (newSongs.length === 1) { - await res.stop(`u betcha, **${firstSong.title}** added to the${addToFrontOfQueue ? ' front of the' : ''} queue${extraMsg}`); - } else { - await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`); + if (newSongs.length === 1) { + await res.stop(`u betcha, **${firstSong.title}** added to the${addToFrontOfQueue ? ' front of the' : ''} queue${extraMsg}`); + } else { + await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`); + } + } catch (error) { + await res.stop(); + throw error; } } } diff --git a/src/commands/queue.ts b/src/commands/queue.ts index 58d6377..79285b1 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -1,15 +1,9 @@ -import {Message, MessageEmbed} from 'discord.js'; -import getYouTubeID from 'get-youtube-id'; +import {Message} from 'discord.js'; import {inject, injectable} from 'inversify'; import {TYPES} from '../types.js'; import PlayerManager from '../managers/player.js'; -import {STATUS} from '../services/player.js'; import Command from '.'; -import getProgressBar from '../utils/get-progress-bar.js'; -import errorMsg from '../utils/error-msg.js'; -import {prettyTime} from '../utils/time.js'; - -const PAGE_SIZE = 10; +import {buildQueueEmbed} from '../utils/build-embed.js'; @injectable() export default class implements Command { @@ -29,54 +23,8 @@ export default class implements Command { public async execute(msg: Message, args: string []): Promise<void> { const player = this.playerManager.get(msg.guild!.id); - const currentlyPlaying = player.getCurrent(); - - if (currentlyPlaying) { - const queueSize = player.queueSize(); - const queuePage = args[0] ? parseInt(args[0], 10) : 1; - - const maxQueuePage = Math.ceil((queueSize + 1) / PAGE_SIZE); - - if (queuePage > maxQueuePage) { - await msg.channel.send(errorMsg('the queue isn\'t that big')); - return; - } - - const embed = new MessageEmbed(); - - embed.setTitle(currentlyPlaying.title); - embed.setURL(`https://www.youtube.com/watch?v=${currentlyPlaying.url.length === 11 ? currentlyPlaying.url : getYouTubeID(currentlyPlaying.url) ?? ''}`); - - let description = player.status === STATUS.PLAYING ? 'âšī¸' : 'âļī¸'; - description += ' '; - description += getProgressBar(20, player.getPosition() / currentlyPlaying.length); - description += ' '; - description += `\`[${prettyTime(player.getPosition())}/${currentlyPlaying.isLive ? 'live' : prettyTime(currentlyPlaying.length)}]\``; - description += ' đ'; - description += player.isQueueEmpty() ? '' : '\n\n**Next up:**'; - - embed.setDescription(description); - - let footer = `Source: ${currentlyPlaying.artist}`; - - if (currentlyPlaying.playlist) { - footer += ` (${currentlyPlaying.playlist.title})`; - } - - embed.setFooter({text: footer}); - - const queuePageBegin = (queuePage - 1) * PAGE_SIZE; - const queuePageEnd = queuePageBegin + PAGE_SIZE; - - player.getQueue().slice(queuePageBegin, queuePageEnd).forEach((song, i) => { - embed.addField(`${(i + 1 + queuePageBegin).toString()}/${queueSize.toString()}`, song.title, false); - }); - - embed.addField('Page', `${queuePage} out of ${maxQueuePage}`, false); + const embed = buildQueueEmbed(player, args[0] ? parseInt(args[0], 10) : 1); - await msg.channel.send({embeds: [embed]}); - } else { - await msg.channel.send('queue empty'); - } + await msg.channel.send({embeds: [embed]}); } } diff --git a/src/commands/skip.ts b/src/commands/skip.ts index 4bab307..ad9d4dc 100644 --- a/src/commands/skip.ts +++ b/src/commands/skip.ts @@ -5,6 +5,7 @@ import PlayerManager from '../managers/player.js'; import Command from '.'; import LoadingMessage from '../utils/loading-message.js'; import errorMsg from '../utils/error-msg.js'; +import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; @injectable() export default class implements Command { @@ -39,10 +40,21 @@ export default class implements Command { try { await loader.start(); await player.forward(numToSkip); - - await loader.stop('keep \'er movin\''); } catch (_: unknown) { await loader.stop(errorMsg('no song to skip to')); + return; } + + const promises = [ + loader.stop('keep \'er movin\''), + ]; + + if (player.getCurrent()) { + promises.push(msg.channel.send({ + embeds: [buildPlayingMessageEmbed(player)], + })); + } + + await Promise.all(promises); } } diff --git a/src/commands/unskip.ts b/src/commands/unskip.ts index a453448..adc44e7 100644 --- a/src/commands/unskip.ts +++ b/src/commands/unskip.ts @@ -4,6 +4,7 @@ import {inject, injectable} from 'inversify'; import PlayerManager from '../managers/player.js'; import errorMsg from '../utils/error-msg.js'; import Command from '.'; +import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; @injectable() export default class implements Command { @@ -26,10 +27,14 @@ export default class implements Command { try { await player.back(); - - await msg.channel.send('back \'er up\''); } catch (_: unknown) { await msg.channel.send(errorMsg('no song to go back to')); + return; } + + await msg.channel.send({ + content: 'back \'er up\'', + embeds: [buildPlayingMessageEmbed(player)], + }); } } |
