From e7f7b7a91e9d23b03d631619876a4fa8c69ef5bf Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 13 Dec 2023 01:35:33 +0100 Subject: [PATCH 1/2] rename tracknumber to position / trackPosition --- src/interactions/commands/player/back.ts | 17 ++++-- src/interactions/commands/player/remove.ts | 69 ++++++++++++++++------ src/interactions/commands/player/skip.ts | 26 ++++---- 3 files changed, 76 insertions(+), 36 deletions(-) diff --git a/src/interactions/commands/player/back.ts b/src/interactions/commands/player/back.ts index 11ef37f..d5b04c6 100644 --- a/src/interactions/commands/player/back.ts +++ b/src/interactions/commands/player/back.ts @@ -12,7 +12,7 @@ class BackCommand extends BaseSlashCommandInteraction { .setName('back') .setDescription('Go back to previous track or specified position in history.') .addNumberOption((option) => - option.setName('tracknumber').setDescription('The position in history to go back to.').setMinValue(1) + option.setName('position').setDescription('The position in history to go back to.').setMinValue(1) ); super(data); } @@ -30,7 +30,7 @@ class BackCommand extends BaseSlashCommandInteraction { checkQueueCurrentTrack ]); - const backToTrackInput: number = interaction.options.getNumber('tracknumber')!; + const backToTrackInput: number = interaction.options.getNumber('position')!; if (backToTrackInput) { return await this.handleBackToTrackPosition(logger, interaction, history, backToTrackInput); @@ -46,22 +46,27 @@ class BackCommand extends BaseSlashCommandInteraction { backtoTrackPosition: number ) { if (backtoTrackPosition > history.tracks.data.length) { - return await this.handleTrackNumberHigherThanTotalTracks(backtoTrackPosition, history, logger, interaction); + return await this.handleTrackPositionHigherThanQueueLength( + backtoTrackPosition, + history, + logger, + interaction + ); } else { await history.back(); const recoveredTrack: Track = history.currentTrack! ?? history.tracks.data[backtoTrackPosition - 1]; - logger.debug('Went back to specified track number in history.'); + logger.debug('Went back to specified track position in history.'); return await this.respondWithSuccessEmbed(recoveredTrack, interaction); } } - private async handleTrackNumberHigherThanTotalTracks( + private async handleTrackPositionHigherThanQueueLength( backToTrackPosition: number, history: GuildQueueHistory, logger: Logger, interaction: ChatInputCommandInteraction ) { - logger.debug('Specified track number was higher than total tracks.'); + logger.debug('Specified track position was higher than total tracks.'); return await interaction.editReply({ embeds: [ new EmbedBuilder() diff --git a/src/interactions/commands/player/remove.ts b/src/interactions/commands/player/remove.ts index d44d6d9..00a2d0e 100644 --- a/src/interactions/commands/player/remove.ts +++ b/src/interactions/commands/player/remove.ts @@ -10,13 +10,41 @@ class RemoveCommand extends BaseSlashCommandInteraction { constructor() { const data = new SlashCommandBuilder() .setName('remove') - .setDescription('Remove specified track from the queue') - .addNumberOption((option) => - option - .setName('tracknumber') - .setDescription('The position in queue for track to remove.') - .setMinValue(1) - .setRequired(true) + .setDescription('Remove tracks from the queue') + .addSubcommand((subcommand) => + subcommand + .setName('track') + .setDescription('Remove a track from the queue') + .addIntegerOption((option) => + option + .setName('position') + .setDescription('The position in queue for track to remove.') + .setMinValue(1) + .setRequired(true) + ) + ) + .addSubcommand((subcommand) => + subcommand + .setName('range') + .setDescription('Remove a range of tracks from the queue') + .addIntegerOption((option) => + option + .setName('start') + .setDescription('The starting position of the range to remove') + .setRequired(true) + ) + .addIntegerOption((option) => + option + .setName('end') + .setDescription('The ending position of the range to remove') + .setRequired(true) + ) + ) + .addSubcommand((subcommand) => + subcommand.setName('queue').setDescription('Remove all tracks from the queue') + ) + .addSubcommand((subcommand) => + subcommand.setName('duplicates').setDescription('Remove all duplicate tracks from the queue') ); super(data); } @@ -33,22 +61,29 @@ class RemoveCommand extends BaseSlashCommandInteraction { checkQueueExists ]); - const removeTrackNumber: number = interaction.options.getNumber('tracknumber')!; + const subcommand = interaction.options.getSubcommand(); - if (removeTrackNumber > queue.tracks.data.length) { - return await this.handleInvalidTrackNumber(logger, interaction, removeTrackNumber, queue); + switch (subcommand) { + default: + return Promise.resolve(); } - return await this.removeTrackFromQueue(logger, interaction, queue, removeTrackNumber); + const trackPositionInput: number = interaction.options.getNumber('position')!; + + if (trackPositionInput > queue.tracks.data.length) { + return await this.handleTrackPositionHigherThanQueueLength(logger, interaction, trackPositionInput, queue); + } + + return await this.removeTrackFromQueue(logger, interaction, queue, trackPositionInput); } - private async handleInvalidTrackNumber( + private async handleTrackPositionHigherThanQueueLength( logger: Logger, interaction: ChatInputCommandInteraction, - removeTrackNumber: number, + trackPositionInput: number, queue: GuildQueue ) { - logger.debug('Specified track number is higher than total tracks.'); + logger.debug('Specified track position is higher than total tracks.'); logger.debug('Responding with warning embed.'); await interaction.editReply({ @@ -56,7 +91,7 @@ class RemoveCommand extends BaseSlashCommandInteraction { new EmbedBuilder() .setDescription( `**${this.embedOptions.icons.warning} Oops!**\n` + - `Track **\`${removeTrackNumber}\`** is not a valid track number. There are a total of **\`${queue.tracks.data.length}\`** tracks in the queue.` + + `Track **\`${trackPositionInput}\`** is not a valid track position. There are a total of **\`${queue.tracks.data.length}\`** tracks in the queue.` + '\n\nView tracks added to the queue with **`/queue`**.' ) .setColor(this.embedOptions.colors.warning) @@ -69,9 +104,9 @@ class RemoveCommand extends BaseSlashCommandInteraction { logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue, - removeTrackNumber: number + trackPositionInput: number ) { - const removedTrack: Track = queue.node.remove(removeTrackNumber - 1)!; + const removedTrack: Track = queue.node.remove(trackPositionInput - 1)!; logger.debug(`Removed track '${removedTrack.url}' from queue.`); logger.debug('Responding with success embed.'); diff --git a/src/interactions/commands/player/skip.ts b/src/interactions/commands/player/skip.ts index e95580c..f3c738a 100644 --- a/src/interactions/commands/player/skip.ts +++ b/src/interactions/commands/player/skip.ts @@ -12,7 +12,7 @@ class SkipCommand extends BaseSlashCommandInteraction { .setName('skip') .setDescription('Skip track to next or specified position in queue.') .addNumberOption((option) => - option.setName('tracknumber').setDescription('The position in queue to skip to.').setMinValue(1) + option.setName('position').setDescription('The position in queue to skip to.').setMinValue(1) ); super(data); } @@ -30,10 +30,10 @@ class SkipCommand extends BaseSlashCommandInteraction { checkQueueCurrentTrack ]); - const skipToTrackInput: number = interaction.options.getNumber('tracknumber')!; + const trackPositionInput: number = interaction.options.getNumber('position')!; - if (skipToTrackInput) { - return await this.handleSkipToTrackPosition(logger, interaction, queue, skipToTrackInput); + if (trackPositionInput) { + return await this.handleSkipToTrackPosition(logger, interaction, queue, trackPositionInput); } else { return await this.handleSkipToNextTrack(logger, interaction, queue); } @@ -43,31 +43,31 @@ class SkipCommand extends BaseSlashCommandInteraction { logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue, - skipToTrackPosition: number + trackPosition: number ) { - if (skipToTrackPosition > queue.tracks.data.length) { - return await this.handleTrackNumberHigherThanTotalTracks(skipToTrackPosition, queue, logger, interaction); + if (trackPosition > queue.tracks.data.length) { + return await this.handleTrackPositionHigherThanQueueLength(trackPosition, queue, logger, interaction); } else { const skippedTrack: Track = queue.currentTrack!; - queue.node.skipTo(skipToTrackPosition - 1); - logger.debug('Skipped to specified track number.'); + queue.node.skipTo(trackPosition - 1); + logger.debug('Skipped to specified track position.'); return await this.respondWithSuccessEmbed(skippedTrack, interaction); } } - private async handleTrackNumberHigherThanTotalTracks( - skipToTrack: number, + private async handleTrackPositionHigherThanQueueLength( + trackPosition: number, queue: GuildQueue, logger: Logger, interaction: ChatInputCommandInteraction ) { - logger.debug('Specified track number was higher than total tracks.'); + logger.debug('Specified track position was higher than total tracks.'); return await interaction.editReply({ embeds: [ new EmbedBuilder() .setDescription( `**${this.embedOptions.icons.warning} Oops!**\n` + - `There are only **\`${queue.tracks.data.length}\`** tracks in the queue. You cannot skip to track **\`${skipToTrack}\`**.\n\n` + + `There are only **\`${queue.tracks.data.length}\`** tracks in the queue. You cannot skip to track **\`${trackPosition}\`**.\n\n` + 'View tracks added to the queue with **`/queue`**.' ) .setColor(this.embedOptions.colors.warning) From 27d4c6c0220e84f4005c077a768040528ebbdffb Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 13 Dec 2023 02:11:05 +0100 Subject: [PATCH 2/2] feat: Updated /remove command with options for range, queue, user and duplicates! --- src/interactions/commands/player/remove.ts | 213 +++++++++++++++++---- 1 file changed, 179 insertions(+), 34 deletions(-) diff --git a/src/interactions/commands/player/remove.ts b/src/interactions/commands/player/remove.ts index 00a2d0e..ef1d5a4 100644 --- a/src/interactions/commands/player/remove.ts +++ b/src/interactions/commands/player/remove.ts @@ -14,7 +14,7 @@ class RemoveCommand extends BaseSlashCommandInteraction { .addSubcommand((subcommand) => subcommand .setName('track') - .setDescription('Remove a track from the queue') + .setDescription('Remove a track from the queue by position') .addIntegerOption((option) => option .setName('position') @@ -43,6 +43,14 @@ class RemoveCommand extends BaseSlashCommandInteraction { .addSubcommand((subcommand) => subcommand.setName('queue').setDescription('Remove all tracks from the queue') ) + .addSubcommand((subcommand) => + subcommand + .setName('user') + .setDescription('Remove all tracks from a specific user') + .addUserOption((option) => + option.setName('target').setDescription('User to remove tracks for').setRequired(true) + ) + ) .addSubcommand((subcommand) => subcommand.setName('duplicates').setDescription('Remove all duplicate tracks from the queue') ); @@ -64,48 +72,126 @@ class RemoveCommand extends BaseSlashCommandInteraction { const subcommand = interaction.options.getSubcommand(); switch (subcommand) { + case 'track': + return await this.handleRemovedTrack(logger, interaction, queue); + case 'range': + return await this.handleRemoveRange(logger, interaction, queue); + case 'queue': + return await this.handleRemoveQueue(logger, interaction, queue); + case 'user': + return await this.handleRemoveUserTracks(logger, interaction, queue); + case 'duplicates': + return await this.handleRemoveDuplicates(logger, interaction, queue); default: return Promise.resolve(); } + } - const trackPositionInput: number = interaction.options.getNumber('position')!; + private async handleRemoveUserTracks(logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue) { + const targetUser = interaction.options.getUser('target')!; + const removedTracks: Track[] = []; + queue.tracks.data.forEach((track) => { + if (track.requestedBy?.id === targetUser.id) { + const removedTrack = queue.node.remove(track); + if (removedTrack) { + removedTracks.push(removedTrack); + } + } + }); + + if (removedTracks.length === 0) { + return await this.handleNoTracksRemoved(logger, interaction); + } + + logger.debug(`Removed ${removedTracks.length} tracks from queue added by a user.`); + + return await this.handleResponseRemovedTracks(logger, interaction, removedTracks.length); + } + + private async handleRemoveDuplicates(logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue) { + const removedTracks: Track[] = []; + const uniqueTrackUrls = new Set(); + queue.tracks.data.forEach((track) => { + if (uniqueTrackUrls.has(track.url)) { + const removedTrack = queue.node.remove(track); + if (removedTrack) { + removedTracks.push(removedTrack); + } + } else { + uniqueTrackUrls.add(track.url); + } + }); + + if (removedTracks.length === 0) { + return await this.handleNoTracksRemoved(logger, interaction); + } + + logger.debug(`Removed ${removedTracks.length} duplicate tracks from queue.`); + + return await this.handleResponseRemovedTracks(logger, interaction, removedTracks.length); + } + + private async handleRemoveRange(logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue) { + const start: number = interaction.options.getInteger('start')!; + const end: number = interaction.options.getInteger('end')!; + + if (start > queue.tracks.data.length || end > queue.tracks.data.length) { + return await this.handleTrackPositionHigherThanQueueLength(logger, interaction, start, queue); + } else if (start > end) { + logger.debug('Start position is higher than end position.'); + + logger.debug('Responding with warning embed.'); + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${this.embedOptions.icons.warning} Oops!**\n` + + `Start position **\`${start}\`** is higher than end position **\`${end}\`**. Please specify a valid range.` + + '\n\nView tracks added to the queue with **`/queue`**.' + ) + .setColor(this.embedOptions.colors.warning) + ] + }); + return Promise.resolve(); + } + + const removedTracks: Track[] = []; + for (let i = start; i <= end; i++) { + const track = queue.node.remove(start - 1); + if (track) { + removedTracks.push(track); + } + } + + if (removedTracks.length === 0) { + return await this.handleNoTracksRemoved(logger, interaction); + } + + logger.debug(`Removed ${removedTracks.length} tracks from queue.`); + + return await this.handleResponseRemovedTracks(logger, interaction, removedTracks.length); + } + + private async handleRemoveQueue(logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue) { + const queueLength = queue.tracks.data.length; + queue.clear(); + + if (queueLength === 0) { + return await this.handleNoTracksRemoved(logger, interaction); + } + + logger.debug('Cleared the queue and removed all tracks.'); + + return await this.handleResponseRemovedTracks(logger, interaction, queueLength); + } + + private async handleRemovedTrack(logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue) { + const trackPositionInput: number = interaction.options.getInteger('position')!; if (trackPositionInput > queue.tracks.data.length) { return await this.handleTrackPositionHigherThanQueueLength(logger, interaction, trackPositionInput, queue); } - return await this.removeTrackFromQueue(logger, interaction, queue, trackPositionInput); - } - - private async handleTrackPositionHigherThanQueueLength( - logger: Logger, - interaction: ChatInputCommandInteraction, - trackPositionInput: number, - queue: GuildQueue - ) { - logger.debug('Specified track position is higher than total tracks.'); - - logger.debug('Responding with warning embed.'); - await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${this.embedOptions.icons.warning} Oops!**\n` + - `Track **\`${trackPositionInput}\`** is not a valid track position. There are a total of **\`${queue.tracks.data.length}\`** tracks in the queue.` + - '\n\nView tracks added to the queue with **`/queue`**.' - ) - .setColor(this.embedOptions.colors.warning) - ] - }); - return Promise.resolve(); - } - - private async removeTrackFromQueue( - logger: Logger, - interaction: ChatInputCommandInteraction, - queue: GuildQueue, - trackPositionInput: number - ) { const removedTrack: Track = queue.node.remove(trackPositionInput - 1)!; logger.debug(`Removed track '${removedTrack.url}' from queue.`); @@ -125,6 +211,65 @@ class RemoveCommand extends BaseSlashCommandInteraction { }); return Promise.resolve(); } + + private async handleNoTracksRemoved(logger: Logger, interaction: ChatInputCommandInteraction) { + logger.debug('Responding with warning embed.'); + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor(this.getEmbedUserAuthor(interaction)) + .setDescription( + `**${this.embedOptions.icons.warning} No tracks removed**\n` + + 'There were no tracks removed from the queue. Please check if the option you selected has tracks to remove.' + ) + .setColor(this.embedOptions.colors.warning) + ] + }); + return Promise.resolve(); + } + + private async handleResponseRemovedTracks( + logger: Logger, + interaction: ChatInputCommandInteraction, + removedAmount: number + ) { + logger.debug('Responding with success embed.'); + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor(this.getEmbedUserAuthor(interaction)) + .setDescription( + `**${this.embedOptions.icons.success} Removed tracks**\n` + + `**\`${removedAmount}\`** tracks were removed from the queue.` + ) + .setColor(this.embedOptions.colors.success) + ] + }); + return Promise.resolve(); + } + + private async handleTrackPositionHigherThanQueueLength( + logger: Logger, + interaction: ChatInputCommandInteraction, + trackPositionInput: number, + queue: GuildQueue + ) { + logger.debug('Specified track position is higher than total tracks.'); + + logger.debug('Responding with warning embed.'); + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${this.embedOptions.icons.warning} Oops!**\n` + + `Position **\`${trackPositionInput}\`** is not a valid track position. There are only a total of **\`${queue.tracks.data.length}\`** tracks in the queue.` + + '\n\nView tracks added to the queue with **`/queue`**.' + ) + .setColor(this.embedOptions.colors.warning) + ] + }); + return Promise.resolve(); + } } export default new RemoveCommand();