From 6ca7ce3e4be92e9240208c76c6bdf1418193ccef Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 13 Dec 2023 02:16:50 +0100 Subject: [PATCH 1/3] fix: change addNumberOption to addIntegerOption --- src/interactions/commands/player/back.ts | 2 +- src/interactions/commands/player/history.ts | 2 +- src/interactions/commands/player/loop.ts | 6 +++--- src/interactions/commands/player/queue.ts | 2 +- src/interactions/commands/player/remove.ts | 2 ++ src/interactions/commands/player/skip.ts | 2 +- src/interactions/commands/player/volume.ts | 2 +- src/interactions/commands/system/shards.ts | 2 +- 8 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/interactions/commands/player/back.ts b/src/interactions/commands/player/back.ts index d5b04c6..39135a8 100644 --- a/src/interactions/commands/player/back.ts +++ b/src/interactions/commands/player/back.ts @@ -11,7 +11,7 @@ class BackCommand extends BaseSlashCommandInteraction { const data = new SlashCommandBuilder() .setName('back') .setDescription('Go back to previous track or specified position in history.') - .addNumberOption((option) => + .addIntegerOption((option) => option.setName('position').setDescription('The position in history to go back to.').setMinValue(1) ); super(data); diff --git a/src/interactions/commands/player/history.ts b/src/interactions/commands/player/history.ts index 8e71d2e..9da4003 100644 --- a/src/interactions/commands/player/history.ts +++ b/src/interactions/commands/player/history.ts @@ -22,7 +22,7 @@ class HistoryCommand extends BaseSlashCommandInteraction { const data = new SlashCommandBuilder() .setName('history') .setDescription('Show history of tracks that have been played.') - .addNumberOption((option) => + .addIntegerOption((option) => option.setName('page').setDescription('Page number to display for the history').setMinValue(1) ); super(data); diff --git a/src/interactions/commands/player/loop.ts b/src/interactions/commands/player/loop.ts index 8aee213..8c86b20 100644 --- a/src/interactions/commands/player/loop.ts +++ b/src/interactions/commands/player/loop.ts @@ -4,7 +4,7 @@ import { EmbedBuilder, Message, SlashCommandBuilder, - SlashCommandNumberOption + SlashCommandIntegerOption } from 'discord.js'; import { Logger } from 'pino'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; @@ -17,8 +17,8 @@ class LoopCommand extends BaseSlashCommandInteraction { const data = new SlashCommandBuilder() .setName('loop') .setDescription('Toggle looping a track, the whole queue or autoplay.') - .addNumberOption(() => - new SlashCommandNumberOption() + .addIntegerOption(() => + new SlashCommandIntegerOption() .setName('mode') .setDescription('Loop mode: Track, queue, autoplay or disabled.') .setRequired(false) diff --git a/src/interactions/commands/player/queue.ts b/src/interactions/commands/player/queue.ts index 8cd1790..bd752ff 100644 --- a/src/interactions/commands/player/queue.ts +++ b/src/interactions/commands/player/queue.ts @@ -23,7 +23,7 @@ class QueueCommand extends BaseSlashCommandInteraction { const data = new SlashCommandBuilder() .setName('queue') .setDescription('Show tracks that have been added to the queue.') - .addNumberOption((option) => + .addIntegerOption((option) => option.setName('page').setDescription('Page number to display for the queue').setMinValue(1) ); super(data); diff --git a/src/interactions/commands/player/remove.ts b/src/interactions/commands/player/remove.ts index ef1d5a4..7e454b7 100644 --- a/src/interactions/commands/player/remove.ts +++ b/src/interactions/commands/player/remove.ts @@ -31,12 +31,14 @@ class RemoveCommand extends BaseSlashCommandInteraction { option .setName('start') .setDescription('The starting position of the range to remove') + .setMinValue(1) .setRequired(true) ) .addIntegerOption((option) => option .setName('end') .setDescription('The ending position of the range to remove') + .setMinValue(1) .setRequired(true) ) ) diff --git a/src/interactions/commands/player/skip.ts b/src/interactions/commands/player/skip.ts index f3c738a..978242a 100644 --- a/src/interactions/commands/player/skip.ts +++ b/src/interactions/commands/player/skip.ts @@ -11,7 +11,7 @@ class SkipCommand extends BaseSlashCommandInteraction { const data = new SlashCommandBuilder() .setName('skip') .setDescription('Skip track to next or specified position in queue.') - .addNumberOption((option) => + .addIntegerOption((option) => option.setName('position').setDescription('The position in queue to skip to.').setMinValue(1) ); super(data); diff --git a/src/interactions/commands/player/volume.ts b/src/interactions/commands/player/volume.ts index 55c4b43..c87ebec 100644 --- a/src/interactions/commands/player/volume.ts +++ b/src/interactions/commands/player/volume.ts @@ -11,7 +11,7 @@ class VolumeCommand extends BaseSlashCommandInteraction { const data = new SlashCommandBuilder() .setName('volume') .setDescription('Show or change the playback volume for tracks.') - .addNumberOption((option) => + .addIntegerOption((option) => option .setName('percentage') .setDescription('Volume percentage: From 1% to 100%.') diff --git a/src/interactions/commands/system/shards.ts b/src/interactions/commands/system/shards.ts index ece9a4e..642118d 100644 --- a/src/interactions/commands/system/shards.ts +++ b/src/interactions/commands/system/shards.ts @@ -31,7 +31,7 @@ class ShardsCommand extends BaseSlashCommandInteraction { { name: 'Members', value: 'members' } ) ) - .addNumberOption((option) => + .addIntegerOption((option) => option.setName('page').setDescription('Page number to display for the shards').setMinValue(1) ); const isSystemCommand: boolean = true; From 4b630e2512282a481b50678714f7dd580d3f4be5 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 13 Dec 2023 02:37:13 +0100 Subject: [PATCH 2/3] fix: solve bug using getNumber after changing to addIntegerOption --- src/classes/interactions.ts | 2 +- src/interactions/commands/player/back.ts | 2 +- src/interactions/commands/player/history.ts | 2 +- src/interactions/commands/player/loop.ts | 2 +- src/interactions/commands/player/queue.ts | 2 +- src/interactions/commands/player/skip.ts | 2 +- src/interactions/commands/player/volume.ts | 2 +- src/interactions/commands/system/shards.ts | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/classes/interactions.ts b/src/classes/interactions.ts index 7930857..710652f 100644 --- a/src/classes/interactions.ts +++ b/src/classes/interactions.ts @@ -122,7 +122,7 @@ abstract class BaseInteraction { return { text: 'Page 1 of 1 (0 tracks)' }; } - const pageIndex: number = (interaction.options.getNumber('page') || 1) - 1; + const pageIndex: number = (interaction.options.getInteger('page') || 1) - 1; const totalPages: number = Math.ceil(queue.tracks.data.length / 10) || 1; return { text: `Page ${pageIndex + 1} of ${totalPages} (${queue.tracks.data.length} tracks)` diff --git a/src/interactions/commands/player/back.ts b/src/interactions/commands/player/back.ts index 39135a8..4eec50a 100644 --- a/src/interactions/commands/player/back.ts +++ b/src/interactions/commands/player/back.ts @@ -30,7 +30,7 @@ class BackCommand extends BaseSlashCommandInteraction { checkQueueCurrentTrack ]); - const backToTrackInput: number = interaction.options.getNumber('position')!; + const backToTrackInput: number = interaction.options.getInteger('position')!; if (backToTrackInput) { return await this.handleBackToTrackPosition(logger, interaction, history, backToTrackInput); diff --git a/src/interactions/commands/player/history.ts b/src/interactions/commands/player/history.ts index 9da4003..d049917 100644 --- a/src/interactions/commands/player/history.ts +++ b/src/interactions/commands/player/history.ts @@ -182,7 +182,7 @@ class HistoryCommand extends BaseSlashCommandInteraction { } private getPageIndex(interaction: ChatInputCommandInteraction): number { - return (interaction.options.getNumber('page') || 1) - 1; + return (interaction.options.getInteger('page') || 1) - 1; } private getTotalPages(history: GuildQueueHistory): number { diff --git a/src/interactions/commands/player/loop.ts b/src/interactions/commands/player/loop.ts index 8c86b20..5ce0b14 100644 --- a/src/interactions/commands/player/loop.ts +++ b/src/interactions/commands/player/loop.ts @@ -44,7 +44,7 @@ class LoopCommand extends BaseSlashCommandInteraction { checkQueueExists ]); - const userInputRepeatMode: QueueRepeatMode = interaction.options.getNumber('mode')!; + const userInputRepeatMode: QueueRepeatMode = interaction.options.getInteger('mode')!; const currentRepeatMode: QueueRepeatMode = queue.repeatMode; if (!userInputRepeatMode && userInputRepeatMode !== 0) { diff --git a/src/interactions/commands/player/queue.ts b/src/interactions/commands/player/queue.ts index bd752ff..97e8595 100644 --- a/src/interactions/commands/player/queue.ts +++ b/src/interactions/commands/player/queue.ts @@ -173,7 +173,7 @@ class QueueCommand extends BaseSlashCommandInteraction { } private getPageIndex(interaction: ChatInputCommandInteraction): number { - return (interaction.options.getNumber('page') || 1) - 1; + return (interaction.options.getInteger('page') || 1) - 1; } private getTotalPages(queue: GuildQueue): number { diff --git a/src/interactions/commands/player/skip.ts b/src/interactions/commands/player/skip.ts index 978242a..11adf5d 100644 --- a/src/interactions/commands/player/skip.ts +++ b/src/interactions/commands/player/skip.ts @@ -30,7 +30,7 @@ class SkipCommand extends BaseSlashCommandInteraction { checkQueueCurrentTrack ]); - const trackPositionInput: number = interaction.options.getNumber('position')!; + const trackPositionInput: number = interaction.options.getInteger('position')!; if (trackPositionInput) { return await this.handleSkipToTrackPosition(logger, interaction, queue, trackPositionInput); diff --git a/src/interactions/commands/player/volume.ts b/src/interactions/commands/player/volume.ts index c87ebec..e1df671 100644 --- a/src/interactions/commands/player/volume.ts +++ b/src/interactions/commands/player/volume.ts @@ -33,7 +33,7 @@ class VolumeCommand extends BaseSlashCommandInteraction { checkQueueExists ]); - const volume: number = interaction.options.getNumber('percentage')!; + const volume: number = interaction.options.getInteger('percentage')!; if (!volume && volume !== 0) { return await this.handleShowCurrentVolume(queue, logger, interaction); diff --git a/src/interactions/commands/system/shards.ts b/src/interactions/commands/system/shards.ts index 642118d..5bbd180 100644 --- a/src/interactions/commands/system/shards.ts +++ b/src/interactions/commands/system/shards.ts @@ -189,7 +189,7 @@ class ShardsCommand extends BaseSlashCommandInteraction { } private getPageIndex(interaction: ChatInputCommandInteraction): number { - return (interaction.options.getNumber('page') || 1) - 1; + return (interaction.options.getInteger('page') || 1) - 1; } private getTotalPages(shardCount: number): number { From 100325b7d819243cc8785edbad69f8f02c9a55e9 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 13 Dec 2023 02:42:58 +0100 Subject: [PATCH 3/3] 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;