From 6eb704c5b4f7a92e254f14eab962ac8bc76a081b Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sat, 29 Jan 2022 12:06:11 -0500 Subject: [PATCH] Fix resume playback bug --- Dockerfile | 4 ++++ src/commands/play.ts | 38 +++++++++++++++++++++++++++++++++++--- src/utils/log-banner.ts | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index b2ca0c4..47a3592 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,7 +35,11 @@ COPY --from=builder /usr/app/migrations migrations RUN yarn prisma generate +ARG COMMIT_HASH=unknown + ENV DATA_DIR /data ENV NODE_ENV production +ENV BUILD_DATE $(date) +ENV COMMIT_HASH $COMMIT_HASH CMD ["yarn", "start"] diff --git a/src/commands/play.ts b/src/commands/play.ts index e8d565c..2d2fc6d 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -1,4 +1,4 @@ -import {AutocompleteInteraction, CommandInteraction} from 'discord.js'; +import {AutocompleteInteraction, CommandInteraction, GuildMember} from 'discord.js'; import {URL} from 'url'; import {SlashCommandBuilder} from '@discordjs/builders'; import {inject, injectable} from 'inversify'; @@ -10,6 +10,10 @@ import getYouTubeAndSpotifySuggestionsFor from '../utils/get-youtube-and-spotify import KeyValueCacheProvider from '../services/key-value-cache.js'; import {ONE_HOUR_IN_SECONDS} from '../utils/constants.js'; import AddQueryToQueue from '../services/add-query-to-queue.js'; +import PlayerManager from '../managers/player.js'; +import {STATUS} from '../services/player.js'; +import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; +import {getMemberVoiceChannel, getMostPopularVoiceChannel} from '../utils/channels.js'; @injectable() export default class implements Command { @@ -33,18 +37,46 @@ export default class implements Command { private readonly spotify: Spotify; private readonly cache: KeyValueCacheProvider; private readonly addQueryToQueue: AddQueryToQueue; + private readonly playerManager: PlayerManager; - constructor(@inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider, @inject(TYPES.Services.AddQueryToQueue) addQueryToQueue: AddQueryToQueue) { + constructor(@inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider, @inject(TYPES.Services.AddQueryToQueue) addQueryToQueue: AddQueryToQueue, @inject(TYPES.Managers.Player) playerManager: PlayerManager) { this.spotify = thirdParty.spotify; this.cache = cache; this.addQueryToQueue = addQueryToQueue; + this.playerManager = playerManager; } // eslint-disable-next-line complexity public async execute(interaction: CommandInteraction): Promise { + const query = interaction.options.getString('query'); + + const player = this.playerManager.get(interaction.guild!.id); + const [targetVoiceChannel] = getMemberVoiceChannel(interaction.member as GuildMember) ?? getMostPopularVoiceChannel(interaction.guild!); + + if (!query) { + if (player.status === STATUS.PLAYING) { + throw new Error('already playing, give me a song name'); + } + + // Must be resuming play + if (!player.getCurrent()) { + throw new Error('nothing to play'); + } + + await player.connect(targetVoiceChannel); + await player.play(); + + await interaction.reply({ + content: 'the stop-and-go light is now green', + embeds: [buildPlayingMessageEmbed(player)], + }); + + return; + } + await this.addQueryToQueue.addToQueue({ interaction, - query: interaction.options.getString('query')!.trim(), + query: query.trim(), addToFrontOfQueue: interaction.options.getBoolean('immediate') ?? false, shuffleAdditions: interaction.options.getBoolean('shuffle') ?? false, }); diff --git a/src/utils/log-banner.ts b/src/utils/log-banner.ts index 0ad6466..9cde988 100644 --- a/src/utils/log-banner.ts +++ b/src/utils/log-banner.ts @@ -9,6 +9,7 @@ const logBanner = () => { paypalUser: 'codetheweb', githubSponsor: 'codetheweb', madeByPrefix: 'Made with 🎶 by ', + buildDate: process.env.BUILD_DATE ? new Date(process.env.BUILD_DATE) : undefined, }).join('\n')); console.log('\n'); };