From 100325b7d819243cc8785edbad69f8f02c9a55e9 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 13 Dec 2023 02:42:58 +0100 Subject: [PATCH] feat: Add new /move command to move a track to specified position --- config/default.js | 1 + src/interactions/commands/player/move.ts | 116 +++++++++++++++++++++++ src/types/configTypes.d.ts | 1 + 3 files changed, 118 insertions(+) create mode 100644 src/interactions/commands/player/move.ts diff --git a/config/default.js b/config/default.js index 91023cb..a76933d 100644 --- a/config/default.js +++ b/config/default.js @@ -100,6 +100,7 @@ module.exports.embedOptions = { back: '⏮️', pauseResumed: '⏯️', shuffled: '🔀', + moved: '🔀', volume: '🔊', volumeIsMuted: '🔇', volumeChanged: '🔊', diff --git a/src/interactions/commands/player/move.ts b/src/interactions/commands/player/move.ts new file mode 100644 index 0000000..0f95017 --- /dev/null +++ b/src/interactions/commands/player/move.ts @@ -0,0 +1,116 @@ +import { GuildQueue, Track, useQueue } from 'discord-player'; +import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from 'discord.js'; +import { BaseSlashCommandInteraction } from '../../../classes/interactions'; +import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; +import { checkQueueCurrentTrack, checkQueueExists } from '../../../utils/validation/queueValidator'; +import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { Logger } from 'pino'; + +class MoveCommand extends BaseSlashCommandInteraction { + constructor() { + const data = new SlashCommandBuilder() + .setName('move') + .setDescription('Move a track to a specified position in queue.') + .addIntegerOption((option) => + option + .setName('from') + .setDescription('The position of the track to move') + .setMinValue(1) + .setRequired(true) + ) + .addIntegerOption((option) => + option + .setName('to') + .setDescription('The position to move the track to') + .setMinValue(1) + .setRequired(true) + ); + super(data); + } + + async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { + const { executionId, interaction } = params; + const logger = this.getLogger(this.name, executionId, interaction); + + const queue: GuildQueue = useQueue(interaction.guild!.id)!; + + await this.runValidators({ interaction, queue, executionId }, [ + checkInVoiceChannel, + checkSameVoiceChannel, + checkQueueExists, + checkQueueCurrentTrack + ]); + + const fromPositionInput: number = interaction.options.getInteger('from')!; + const toPositionInput: number = interaction.options.getInteger('to')!; + return await this.handleMoveTrackToPosition(logger, interaction, queue, fromPositionInput, toPositionInput); + } + + private async handleMoveTrackToPosition( + logger: Logger, + interaction: ChatInputCommandInteraction, + queue: GuildQueue, + fromPosition: number, + toPosition: number + ) { + if (fromPosition > queue.tracks.data.length || toPosition > queue.tracks.data.length) { + logger.debug('One of the specified track positions was higher than total tracks.'); + return await this.handleTrackPositionHigherThanQueueLength( + fromPosition, + toPosition, + queue, + logger, + interaction + ); + } else { + const trackToMove: Track = queue.tracks.data[fromPosition - 1]; + queue.node.move(trackToMove, toPosition - 1); + logger.debug(`Moved track from position ${fromPosition} to position ${toPosition} in queue`); + return await this.respondWithSuccessEmbed(trackToMove, interaction, fromPosition, toPosition); + } + } + + private async handleTrackPositionHigherThanQueueLength( + fromPosition: number, + toPosition: number, + queue: GuildQueue, + logger: Logger, + interaction: ChatInputCommandInteraction + ) { + logger.debug('One of the specified track positions was higher than total tracks.'); + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${this.embedOptions.icons.warning} Oops!**\n` + + `You cannot move track in position **\`${fromPosition}\`** to position **\`${toPosition}\`**. There are only **\`${queue.tracks.data.length}\`** tracks in the queue.\n\n` + + 'View tracks added to the queue with **`/queue`**.' + ) + .setColor(this.embedOptions.colors.warning) + ] + }); + } + + private async respondWithSuccessEmbed( + movedTrack: Track, + interaction: ChatInputCommandInteraction, + fromPosition: number, + toPosition: number + ) { + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor(this.getEmbedUserAuthor(interaction)) + .setDescription( + `**${this.embedOptions.icons.moved} Moved track**\n` + + `${this.getDisplayTrackDurationAndUrl(movedTrack)}\n\n` + + `Track has been moved from position **\`${fromPosition}\`** to position **\`${toPosition}\`**.` + ) + .setThumbnail(this.getTrackThumbnailUrl(movedTrack)) + .setColor(this.embedOptions.colors.success) + ] + }); + } +} + +export default new MoveCommand(); diff --git a/src/types/configTypes.d.ts b/src/types/configTypes.d.ts index df8a3e1..f93cf9e 100644 --- a/src/types/configTypes.d.ts +++ b/src/types/configTypes.d.ts @@ -74,6 +74,7 @@ export type EmbedOptions = { back: string; pauseResumed: string; shuffled: string; + moved: string; volume: string; volumeIsMuted: string; volumeChanged: string;