Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
feat: Add new /move command to move a track to specified position
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Dec 13, 2023
1 parent 4b630e2 commit 100325b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module.exports.embedOptions = {
back: '⏮️',
pauseResumed: '⏯️',
shuffled: '🔀',
moved: '🔀',
volume: '🔊',
volumeIsMuted: '🔇',
volumeChanged: '🔊',
Expand Down
116 changes: 116 additions & 0 deletions src/interactions/commands/player/move.ts
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions src/types/configTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type EmbedOptions = {
back: string;
pauseResumed: string;
shuffled: string;
moved: string;
volume: string;
volumeIsMuted: string;
volumeChanged: string;
Expand Down

0 comments on commit 100325b

Please # to comment.