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

Commit

Permalink
fix: refactor remove command
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Sep 13, 2023
1 parent ff41013 commit 230ba5f
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/interactions/commands/player/remove.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 { checkQueueExists } from '../../../utils/validation/queueValidator';
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { Logger } from 'pino';

class RemoveCommand extends BaseSlashCommandInteraction {
constructor() {
Expand Down Expand Up @@ -35,26 +36,44 @@ class RemoveCommand extends BaseSlashCommandInteraction {
const removeTrackNumber: number = interaction.options.getNumber('tracknumber')!;

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

logger.debug('Responding with warning embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${this.embedOptions.icons.warning} Oops!**\nTrack **\`${removeTrackNumber}\`** is not a valid track number. There are a total of **\`${queue.tracks.data.length}\`** tracks in the queue.\n\nView tracks added to the queue with **\`/queue\`**.`
)
.setColor(this.embedOptions.colors.warning)
]
});
return await this.handleInvalidTrackNumber(logger, interaction, removeTrackNumber, queue);
}

// Remove specified track number from queue
return await this.removeTrackFromQueue(logger, interaction, queue, removeTrackNumber);
}

private async handleInvalidTrackNumber(
logger: Logger,
interaction: ChatInputCommandInteraction,
removeTrackNumber: number,
queue: GuildQueue
) {
logger.debug('Specified track number is higher than total tracks.');

logger.debug('Responding with warning embed.');
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${this.embedOptions.icons.warning} Oops!**\nTrack **\`${removeTrackNumber}\`** is not a valid track number. There are a total of **\`${queue.tracks.data.length}\`** tracks in the queue.\n\nView tracks added to the queue with **\`/queue\`**.`
)
.setColor(this.embedOptions.colors.warning)
]
});
return Promise.resolve();
}

private async removeTrackFromQueue(
logger: Logger,
interaction: ChatInputCommandInteraction,
queue: GuildQueue,
removeTrackNumber: number
) {
const removedTrack: Track = queue.node.remove(removeTrackNumber - 1)!;
logger.debug(`Removed track '${removedTrack.url}' from queue.`);

logger.debug('Responding with success embed.');
return await interaction.editReply({
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor(await this.getEmbedUserAuthor(interaction))
Expand All @@ -67,6 +86,7 @@ class RemoveCommand extends BaseSlashCommandInteraction {
.setColor(this.embedOptions.colors.success)
]
});
return Promise.resolve();
}
}

Expand Down

0 comments on commit 230ba5f

Please # to comment.