mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-03 14:38:49 +00:00
fix: refactored skip command
This commit is contained in:
1 parent
c871a75822
commit
90346e7469
1 file changed
+82
-88
@@ -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() {
|
||||
@@ -29,97 +30,90 @@ 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)
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
const skippedTrack: Track = queue.currentTrack!;
|
||||
|
||||
queue.node.skip();
|
||||
logger.debug('Skipped current track.');
|
||||
|
||||
const loopModesFormatted: Map<number, string> = new Map([
|
||||
[0, 'disabled'],
|
||||
[1, 'track'],
|
||||
[2, 'queue'],
|
||||
[3, 'autoplay']
|
||||
]);
|
||||
|
||||
const loopModeUserString: string = loopModesFormatted.get(queue.repeatMode)!;
|
||||
|
||||
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 repeatModeString: string = queue.repeatMode === 0 ? '' : getRepeatModeMessage(queue.repeatMode);
|
||||
|
||||
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)
|
||||
]
|
||||
});
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
private async handleSkipToNextTrack(logger: Logger, interaction: ChatInputCommandInteraction, queue: GuildQueue) {
|
||||
if (queue.tracks.data.length === 0 && !queue.currentTrack) {
|
||||
return await this.handleNoTracksInQueueAndNoCurrentTrack(logger, interaction);
|
||||
}
|
||||
|
||||
const skippedTrack: Track = queue.currentTrack!;
|
||||
queue.node.skip();
|
||||
logger.debug('Skipped current track.');
|
||||
return await this.respondWithSuccessEmbed(skippedTrack, interaction);
|
||||
}
|
||||
|
||||
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)
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
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)
|
||||
]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default new SkipCommand();
|
||||
Reference in new issue
Block a user