mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 14:38:49 +00:00
fix: refactor loop command
This commit is contained in:
1 parent
56056cd73d
commit
03c1d5d90f
1 file changed
+103
-98
@@ -1,9 +1,16 @@
|
|||||||
import { GuildQueue, QueueRepeatMode, useQueue } from 'discord-player';
|
import { GuildQueue, QueueRepeatMode, useQueue } from 'discord-player';
|
||||||
import { EmbedBuilder, SlashCommandBuilder, SlashCommandStringOption } from 'discord.js';
|
import {
|
||||||
|
ChatInputCommandInteraction,
|
||||||
|
EmbedBuilder,
|
||||||
|
Message,
|
||||||
|
SlashCommandBuilder,
|
||||||
|
SlashCommandStringOption
|
||||||
|
} from 'discord.js';
|
||||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
||||||
import { checkQueueExists } from '../../../utils/validation/queueValidator';
|
import { checkQueueExists } from '../../../utils/validation/queueValidator';
|
||||||
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
|
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
|
||||||
|
import { Logger } from 'pino';
|
||||||
|
|
||||||
class LoopCommand extends BaseSlashCommandInteraction {
|
class LoopCommand extends BaseSlashCommandInteraction {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -16,10 +23,10 @@ class LoopCommand extends BaseSlashCommandInteraction {
|
|||||||
.setDescription('Loop mode: Track, queue, autoplay or disabled.')
|
.setDescription('Loop mode: Track, queue, autoplay or disabled.')
|
||||||
.setRequired(false)
|
.setRequired(false)
|
||||||
.addChoices(
|
.addChoices(
|
||||||
{ name: 'Track', value: '1' },
|
{ name: 'Track', value: QueueRepeatMode.TRACK.toString() },
|
||||||
{ name: 'Queue', value: '2' },
|
{ name: 'Queue', value: QueueRepeatMode.QUEUE.toString() },
|
||||||
{ name: 'Autoplay', value: '3' },
|
{ name: 'Autoplay', value: QueueRepeatMode.AUTOPLAY.toString() },
|
||||||
{ name: 'Disabled', value: '0' }
|
{ name: 'Disabled', value: QueueRepeatMode.OFF.toString() }
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
super(data);
|
super(data);
|
||||||
@@ -37,120 +44,118 @@ class LoopCommand extends BaseSlashCommandInteraction {
|
|||||||
checkQueueExists
|
checkQueueExists
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// TODO: create type for loop modes formatted
|
const userInputRepeatMode: QueueRepeatMode = parseInt(interaction.options.getString('mode')!);
|
||||||
const loopModesFormatted: Map<number, string> = new Map([
|
const currentRepeatMode: QueueRepeatMode = queue.repeatMode;
|
||||||
[0, 'disabled'],
|
|
||||||
[1, 'track'],
|
|
||||||
[2, 'queue'],
|
|
||||||
[3, 'autoplay']
|
|
||||||
]);
|
|
||||||
|
|
||||||
const mode: number = parseInt(interaction.options.getString('mode')!);
|
if (!userInputRepeatMode && userInputRepeatMode !== 0) {
|
||||||
const modeUserString: string = loopModesFormatted.get(mode)!;
|
return await this.handleNoInputMode(logger, interaction, currentRepeatMode);
|
||||||
const currentMode: QueueRepeatMode = queue.repeatMode;
|
|
||||||
const currentModeUserString: string = loopModesFormatted.get(currentMode)!;
|
|
||||||
|
|
||||||
if (!mode && mode !== 0) {
|
|
||||||
logger.debug('No mode input was provided, responding with current loop mode.');
|
|
||||||
|
|
||||||
logger.debug('Responding with info embed.');
|
|
||||||
return await interaction.editReply({
|
|
||||||
embeds: [
|
|
||||||
new EmbedBuilder()
|
|
||||||
.setDescription(
|
|
||||||
`**${
|
|
||||||
currentMode === 3 ? this.embedOptions.icons.autoplay : this.embedOptions.icons.loop
|
|
||||||
} Current loop mode**\nThe looping mode is currently set to **\`${currentModeUserString}\`**.`
|
|
||||||
)
|
|
||||||
.setColor(this.embedOptions.colors.info)
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode === currentMode) {
|
if (userInputRepeatMode === currentRepeatMode) {
|
||||||
logger.debug(`Loop mode is already set to ${modeUserString}.`);
|
return await this.handleSameMode(logger, interaction, userInputRepeatMode);
|
||||||
|
|
||||||
logger.debug('Responding with warning embed.');
|
|
||||||
return await interaction.editReply({
|
|
||||||
embeds: [
|
|
||||||
new EmbedBuilder()
|
|
||||||
.setDescription(
|
|
||||||
`**${this.embedOptions.icons.warning} Oops!**\nLoop mode is already **\`${modeUserString}\`**.`
|
|
||||||
)
|
|
||||||
.setColor(this.embedOptions.colors.warning)
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
queue.setRepeatMode(mode);
|
return await this.handleChangeMode(logger, interaction, queue, currentRepeatMode, userInputRepeatMode);
|
||||||
|
}
|
||||||
|
|
||||||
// switch(queue.repeatMode) instead of multiple if statements
|
private async handleNoInputMode(
|
||||||
|
logger: Logger,
|
||||||
|
interaction: ChatInputCommandInteraction,
|
||||||
|
currentRepeatMode: QueueRepeatMode
|
||||||
|
): Promise<Message> {
|
||||||
|
logger.debug('No repeat mode was provided, responding with current repeat mode.');
|
||||||
|
|
||||||
if (queue.repeatMode !== mode) {
|
const repeatModeEmbedIcon =
|
||||||
logger.warn(
|
currentRepeatMode === 3 ? this.embedOptions.icons.autoplay : this.embedOptions.icons.loop;
|
||||||
'Failed to change loop mode. After setting queue repeat mode, the value was not the same as input.'
|
const repeatModeEmbedName = this.getRepeatModeEmbedName(currentRepeatMode);
|
||||||
);
|
|
||||||
|
|
||||||
logger.debug('Responding with error embed.');
|
logger.debug('Responding with info embed.');
|
||||||
return await interaction.editReply({
|
return await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
new EmbedBuilder()
|
||||||
.setDescription(
|
.setDescription(
|
||||||
`**${this.embedOptions.icons.error} Uh-oh... Failed to change loop mode!**\nI tried to change the loop mode to **\`${modeUserString}\`**, but something went wrong.\n\nYou can try to perform the command again.\n\n_If you think this message is incorrect or the issue persists, please submit a bug report in the **[support server](${this.botOptions.serverInviteUrl})**._`
|
`**${repeatModeEmbedIcon} Current loop mode**\nThe looping mode is currently set to **\`${repeatModeEmbedName}\`**.`
|
||||||
)
|
)
|
||||||
.setColor(this.embedOptions.colors.error)
|
.setColor(this.embedOptions.colors.info)
|
||||||
.setFooter({ text: `Execution ID: ${executionId}` })
|
]
|
||||||
]
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (queue.repeatMode === 0) {
|
private async handleSameMode(
|
||||||
logger.debug('Disabled loop mode.');
|
logger: Logger,
|
||||||
|
interaction: ChatInputCommandInteraction,
|
||||||
|
currentRepeatMode: QueueRepeatMode
|
||||||
|
): Promise<Message> {
|
||||||
|
const repeatModeEmbedName = this.getRepeatModeEmbedName(currentRepeatMode);
|
||||||
|
logger.debug(`Loop mode is already set to '${repeatModeEmbedName}'.`);
|
||||||
|
logger.debug('Responding with warning embed.');
|
||||||
|
return await interaction.editReply({
|
||||||
|
embeds: [
|
||||||
|
new EmbedBuilder()
|
||||||
|
.setDescription(
|
||||||
|
`**${this.embedOptions.icons.warning} Oops!**\nLoop mode is already **\`${repeatModeEmbedName}\`**.`
|
||||||
|
)
|
||||||
|
.setColor(this.embedOptions.colors.warning)
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Different text when disabling autoplay.
|
private async handleChangeMode(
|
||||||
logger.debug('Responding with success embed.');
|
logger: Logger,
|
||||||
return await interaction.editReply({
|
interaction: ChatInputCommandInteraction,
|
||||||
embeds: [
|
queue: GuildQueue,
|
||||||
new EmbedBuilder()
|
fromRepeatMode: QueueRepeatMode,
|
||||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
toRepeatMode: QueueRepeatMode
|
||||||
.setDescription(
|
): Promise<Message> {
|
||||||
`**${this.embedOptions.icons.success} Loop mode disabled**\nChanging loop mode from **\`${currentModeUserString}\`** to **\`${modeUserString}\`**.\n\nThe ${currentModeUserString} will no longer play on repeat!`
|
const newRepeatModeEmbedName = this.getRepeatModeEmbedName(toRepeatMode);
|
||||||
)
|
const getChangedRepeatModeEmbedReply = this.getChangedRepeatModeEmbedReply(fromRepeatMode, toRepeatMode);
|
||||||
.setColor(this.embedOptions.colors.success)
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (queue.repeatMode === 3) {
|
queue.setRepeatMode(toRepeatMode);
|
||||||
logger.debug('Enabled autoplay mode.');
|
logger.debug(`Loop mode changed to '${newRepeatModeEmbedName}'.`);
|
||||||
|
|
||||||
logger.debug('Responding with success embed.');
|
|
||||||
return await interaction.editReply({
|
|
||||||
embeds: [
|
|
||||||
new EmbedBuilder()
|
|
||||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
|
||||||
.setDescription(
|
|
||||||
`**${this.embedOptions.icons.autoplaying} Loop mode changed**\nChanging loop mode from **\`${currentModeUserString}\`** to **\`${modeUserString}\`**.\n\nWhen the queue is empty, similar tracks will start playing!`
|
|
||||||
)
|
|
||||||
.setColor(this.embedOptions.colors.success)
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug('Enabled loop mode.');
|
|
||||||
|
|
||||||
logger.debug('Responding with success embed.');
|
logger.debug('Responding with success embed.');
|
||||||
return await interaction.editReply({
|
return await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
new EmbedBuilder()
|
||||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
||||||
.setDescription(
|
.setDescription(getChangedRepeatModeEmbedReply)
|
||||||
`**${this.embedOptions.icons.looping} Loop mode changed**\nChanging loop mode from **\`${currentModeUserString}\`** to **\`${modeUserString}\`**.\n\nThe ${modeUserString} will now play on repeat!`
|
|
||||||
)
|
|
||||||
.setColor(this.embedOptions.colors.success)
|
.setColor(this.embedOptions.colors.success)
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getRepeatModeEmbedName(repeatMode: QueueRepeatMode): string {
|
||||||
|
const repeatModeEmbedString: { [key in QueueRepeatMode]: string } = {
|
||||||
|
[QueueRepeatMode.OFF]: 'disabled',
|
||||||
|
[QueueRepeatMode.TRACK]: 'track',
|
||||||
|
[QueueRepeatMode.QUEUE]: 'queue',
|
||||||
|
[QueueRepeatMode.AUTOPLAY]: 'autoplay'
|
||||||
|
};
|
||||||
|
|
||||||
|
return repeatModeEmbedString[repeatMode];
|
||||||
|
}
|
||||||
|
|
||||||
|
private getChangedRepeatModeEmbedReply(fromRepeatMode: QueueRepeatMode, toRepeatMode: QueueRepeatMode): string {
|
||||||
|
const fromRepeatModeEmbedName = this.getRepeatModeEmbedName(fromRepeatMode);
|
||||||
|
const toRepeatModeEmbedName = this.getRepeatModeEmbedName(toRepeatMode);
|
||||||
|
|
||||||
|
let repeatModeIcon = this.embedOptions.icons.looping;
|
||||||
|
let newRepeatModeMessage = `The ${toRepeatModeEmbedName} will now play on repeat!`;
|
||||||
|
|
||||||
|
if (toRepeatMode === QueueRepeatMode.OFF) {
|
||||||
|
repeatModeIcon = this.embedOptions.icons.success;
|
||||||
|
newRepeatModeMessage = `The ${fromRepeatModeEmbedName} will no longer play on repeat!`;
|
||||||
|
} else if (toRepeatMode === QueueRepeatMode.AUTOPLAY) {
|
||||||
|
repeatModeIcon = this.embedOptions.icons.autoplaying;
|
||||||
|
newRepeatModeMessage = 'When the queue is empty, similar tracks will start playing!';
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
`**${repeatModeIcon} Loop mode changed**\n` +
|
||||||
|
`Changing loop mode from **\`${fromRepeatModeEmbedName}\`** to **\`${toRepeatModeEmbedName}\`**.\n\n` +
|
||||||
|
newRepeatModeMessage
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new LoopCommand();
|
export default new LoopCommand();
|
||||||
Reference in new issue
Block a user