From d438d46c09c236b34d5cecf75662ae94e7eca9cf Mon Sep 17 00:00:00 2001 From: Hellyson Rodrigo Parteka Date: Wed, 9 Mar 2022 23:45:59 -0300 Subject: [PATCH] Create `/resume` command; make `query` required for the `/play` command (#546) --- CHANGELOG.md | 8 +++++++ src/commands/play.ts | 41 ++++++------------------------------ src/commands/resume.ts | 46 +++++++++++++++++++++++++++++++++++++++++ src/inversify.config.ts | 2 ++ 4 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 src/commands/resume.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 446b4bc..6fa99cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `/resume` command to resume playback + +### Changed +- `query` is now a required parameter from `/play` + +### Removed +- `/play` cannot resume the playback anymore since `query` is now required ## [1.2.0] - 2022-02-24 ### Added diff --git a/src/commands/play.ts b/src/commands/play.ts index 08436c2..a1dd84b 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -1,4 +1,4 @@ -import {AutocompleteInteraction, CommandInteraction, GuildMember} from 'discord.js'; +import {AutocompleteInteraction, CommandInteraction} from 'discord.js'; import {URL} from 'url'; import {SlashCommandBuilder} from '@discordjs/builders'; import {inject, injectable} from 'inversify'; @@ -10,20 +10,17 @@ 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 { public readonly slashCommand = new SlashCommandBuilder() .setName('play') - .setDescription('play a song or resume playback') + .setDescription('play a song') .addStringOption(option => option .setName('query') .setDescription('YouTube URL, Spotify URL, or search query') - .setAutocomplete(true)) + .setAutocomplete(true) + .setRequired(true)) .addBooleanOption(option => option .setName('immediate') .setDescription('add track to the front of the queue')) @@ -36,42 +33,16 @@ 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, @inject(TYPES.Managers.Player) playerManager: PlayerManager) { + constructor(@inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider, @inject(TYPES.Services.AddQueryToQueue) addQueryToQueue: AddQueryToQueue) { 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; - } + const query = interaction.options.getString('query')!; await this.addQueryToQueue.addToQueue({ interaction, diff --git a/src/commands/resume.ts b/src/commands/resume.ts new file mode 100644 index 0000000..eddc762 --- /dev/null +++ b/src/commands/resume.ts @@ -0,0 +1,46 @@ +import {SlashCommandBuilder} from '@discordjs/builders'; +import {inject, injectable} from 'inversify'; +import Command from '.'; +import {TYPES} from '../types.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'; +import {CommandInteraction, GuildMember} from 'discord.js'; + +@injectable() +export default class implements Command { + public readonly slashCommand = new SlashCommandBuilder() + .setName('resume') + .setDescription('resume playback'); + + public requiresVC = true; + + private readonly playerManager: PlayerManager; + + constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) { + this.playerManager = playerManager; + } + + // eslint-disable-next-line complexity + public async execute(interaction: CommandInteraction): Promise { + const player = this.playerManager.get(interaction.guild!.id); + const [targetVoiceChannel] = getMemberVoiceChannel(interaction.member as GuildMember) ?? getMostPopularVoiceChannel(interaction.guild!); + 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)], + }); + } +} diff --git a/src/inversify.config.ts b/src/inversify.config.ts index 7116984..daf4e68 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -23,6 +23,7 @@ import Pause from './commands/pause.js'; import Play from './commands/play.js'; import QueueCommand from './commands/queue.js'; import Remove from './commands/remove.js'; +import Resume from './commands/resume.js'; import Seek from './commands/seek.js'; import Shuffle from './commands/shuffle.js'; import Skip from './commands/skip.js'; @@ -64,6 +65,7 @@ container.bind(TYPES.Services.AddQueryToQueue).to(AddQueryToQue Play, QueueCommand, Remove, + Resume, Seek, Shuffle, Skip,