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

Commit

Permalink
fix: refactored skip command
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Sep 13, 2023
1 parent c871a75 commit 90346e7
Showing 1 changed file with 76 additions and 82 deletions.
158 changes: 76 additions & 82 deletions src/interactions/commands/player/skip.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { GuildQueue, Track, useQueue } from 'discord-player';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
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 SkipCommand extends BaseSlashCommandInteraction {
constructor() {
Expand All @@ -29,96 +30,89 @@ class SkipCommand extends BaseSlashCommandInteraction {
checkQueueCurrentTrack
]);

const skipToTrack: number = interaction.options.getNumber('tracknumber')!;
const skipToTrackInput: number = interaction.options.getNumber('tracknumber')!;

if (skipToTrack) {
if (skipToTrack > queue.tracks.data.length) {
logger.debug('Specified track number was higher than total tracks.');

logger.debug('Responding with warning embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${this.embedOptions.icons.warning} Oops!**\nThere are only **\`${queue.tracks.data.length}\`** tracks in the queue. You cannot skip to track **\`${skipToTrack}\`**.\n\nView tracks added to the queue with **\`/queue\`**.`
)
.setColor(this.embedOptions.colors.warning)
]
});
} else {
const skippedTrack: Track = queue.currentTrack!;
logger.debug('Responding with warning embed.');

queue.node.skipTo(skipToTrack - 1);
logger.debug('Skipped to specified track number.');

logger.debug('Responding with success embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor(this.getEmbedUserAuthor(interaction))
.setDescription(
`**${
this.embedOptions.icons.skipped
} Skipped track**\n${this.getDisplayTrackDurationAndUrl(skippedTrack)}`
)
.setThumbnail(this.getTrackThumbnailUrl(skippedTrack))
.setColor(this.embedOptions.colors.success)
]
});
}
if (skipToTrackInput) {
return await this.handleSkipToTrack(logger, interaction, queue, skipToTrackInput);
} else {
if (queue.tracks.data.length === 0 && !queue.currentTrack) {
logger.debug('No tracks in queue and no current track.');

logger.debug('Responding with warning embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${this.embedOptions.icons.warning} Oops!**\nThere is nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(this.embedOptions.colors.warning)
]
});
}
return await this.handleSkipToNextTrack(logger, interaction, queue);
}
}

private async handleSkipToTrack(
logger: Logger,
interaction: ChatInputCommandInteraction,
queue: GuildQueue,
skipToTrack: number
) {
if (skipToTrack > queue.tracks.data.length) {
return await this.handleTrackNumberHigherThanTotalTracks(skipToTrack, queue, logger, interaction);
} else {
const skippedTrack: Track = queue.currentTrack!;
queue.node.skipTo(skipToTrack - 1);
logger.debug('Skipped to specified track number.');
return await this.respondWithSuccessEmbed(skippedTrack, interaction);
}
}

queue.node.skip();
logger.debug('Skipped current track.');

const loopModesFormatted: Map<number, string> = new Map([
[0, 'disabled'],
[1, 'track'],
[2, 'queue'],
[3, 'autoplay']
]);
private async handleTrackNumberHigherThanTotalTracks(
skipToTrack: number,
queue: GuildQueue,
logger: Logger,
interaction: ChatInputCommandInteraction
) {
logger.debug('Specified track number 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` +
'View tracks added to the queue with **`/queue`**.'
)
.setColor(this.embedOptions.colors.warning)
]
});
}

const loopModeUserString: string = loopModesFormatted.get(queue.repeatMode)!;
private async handleSkipToNextTrack(logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue) {
if (queue.tracks.data.length === 0 && !queue.currentTrack) {
return await this.handleNoTracksInQueueAndNoCurrentTrack(logger, interaction);
}

const getRepeatModeMessage = (repeatMode: number): string => {
const icon = repeatMode === 3 ? this.embedOptions.icons.autoplaying : this.embedOptions.icons.looping;
return `**${icon} Looping**\nLoop mode is set to **\`${loopModeUserString}\`**. You can change it with **\`/loop\`**.`;
};
const skippedTrack: Track = queue.currentTrack!;
queue.node.skip();
logger.debug('Skipped current track.');
return await this.respondWithSuccessEmbed(skippedTrack, interaction);
}

const repeatModeString: string = queue.repeatMode === 0 ? '' : getRepeatModeMessage(queue.repeatMode);
private async handleNoTracksInQueueAndNoCurrentTrack(logger: Logger, interaction: ChatInputCommandInteraction) {
logger.debug('No tracks in queue and no current track.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${this.embedOptions.icons.warning} Oops!**\n` +
'There is nothing currently playing. First add some tracks with **`/play`**!'
)
.setColor(this.embedOptions.colors.warning)
]
});
}

logger.debug('Responding with success embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor(this.getEmbedUserAuthor(interaction))
.setDescription(
`**${this.embedOptions.icons.skipped} Skipped track**\n${this.getDisplayTrackDurationAndUrl(
skippedTrack
)}` + `\n\n${repeatModeString}`
)
.setThumbnail(this.getTrackThumbnailUrl(skippedTrack))
.setColor(this.embedOptions.colors.success)
]
});
}
private async respondWithSuccessEmbed(skippedTrack: Track, interaction: ChatInputCommandInteraction) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor(this.getEmbedUserAuthor(interaction))
.setDescription(
`**${this.embedOptions.icons.skipped} Skipped track**\n` +
`${this.getDisplayTrackDurationAndUrl(skippedTrack)}`
)
.setThumbnail(this.getTrackThumbnailUrl(skippedTrack))
.setColor(this.embedOptions.colors.success)
]
});
}
}

Expand Down

0 comments on commit 90346e7

Please sign in to comment.