mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-03 14:38:49 +00:00
fix: refactored queue command, added more common methods
This commit is contained in:
1 parent
bf30d565d7
commit
1ebb7c3b89
6 files changed
+175
-149
No files matched your search
@@ -1,14 +1,11 @@
|
||||
import config from 'config';
|
||||
import { GuildQueue, PlayerTimestamp, Track, useQueue } from 'discord-player';
|
||||
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
||||
import { GuildQueue, Track, useQueue } from 'discord-player';
|
||||
import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
||||
import { Logger } from 'pino';
|
||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||
import { PlayerOptions } from '../../../types/configTypes';
|
||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
||||
import { checkQueueExists } from '../../../utils/validation/queueValidator';
|
||||
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
|
||||
|
||||
const playerOptions: PlayerOptions = config.get('playerOptions');
|
||||
|
||||
class QueueCommand extends BaseSlashCommandInteraction {
|
||||
constructor() {
|
||||
const data = new SlashCommandBuilder()
|
||||
@@ -32,120 +29,140 @@ class QueueCommand extends BaseSlashCommandInteraction {
|
||||
checkQueueExists
|
||||
]);
|
||||
|
||||
const pageIndex: number = (interaction.options.getNumber('page') || 1) - 1;
|
||||
let queueString: string = '';
|
||||
|
||||
const queueLength: number = queue.tracks.data.length;
|
||||
const totalPages: number = Math.ceil(queueLength / 10) || 1;
|
||||
const pageIndex: number = this.getPageIndex(interaction);
|
||||
const totalPages: number = this.getTotalPages(queue);
|
||||
|
||||
if (pageIndex > totalPages - 1) {
|
||||
logger.debug('Specified page was higher than total pages.');
|
||||
|
||||
logger.debug('Responding with warning embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.warning} Oops!**\nPage **\`${
|
||||
pageIndex + 1
|
||||
}\`** is not a valid page number.\n\nThere are only a total of **\`${totalPages}\`** pages in the queue.`
|
||||
)
|
||||
.setColor(this.embedOptions.colors.warning)
|
||||
]
|
||||
});
|
||||
return await this.handleInvalidPage(logger, interaction, pageIndex, totalPages);
|
||||
}
|
||||
|
||||
if (queue.tracks.data.length === 0) {
|
||||
logger.debug('Queue exists but with no tracks, displaying empty queue.');
|
||||
queueString = 'The queue is empty, add some tracks with **`/play`**!';
|
||||
} else {
|
||||
queueString = queue.tracks.data
|
||||
.slice(pageIndex * 10, pageIndex * 10 + 10)
|
||||
.map((track, index) => {
|
||||
return `**${pageIndex * 10 + index + 1}.** ${this.getDisplayTrackDurationAndUrl(track)}`;
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
const queueTracksListString: string = this.getQueueTracksListString(queue, pageIndex);
|
||||
|
||||
const currentTrack: Track = queue.currentTrack!;
|
||||
if (currentTrack) {
|
||||
return await this.handleCurrentTrack(logger, interaction, queue, currentTrack, queueTracksListString);
|
||||
}
|
||||
|
||||
const loopModesFormatted = new Map([
|
||||
return await this.handleNoCurrentTrack(logger, interaction, queue, queueTracksListString);
|
||||
}
|
||||
|
||||
private async handleCurrentTrack(
|
||||
logger: Logger,
|
||||
interaction: ChatInputCommandInteraction,
|
||||
queue: GuildQueue,
|
||||
currentTrack: Track,
|
||||
queueTracksListString: string
|
||||
) {
|
||||
logger.debug('Queue exists with current track, gathering information.');
|
||||
|
||||
logger.debug('Responding with info embed.');
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedQueueAuthor(interaction, queue))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.audioPlaying} Now playing**\n` +
|
||||
`${this.getFormattedTrackUrl(currentTrack)}\n` +
|
||||
`${this.getDisplayTrackRequestedBy(currentTrack)}\n` +
|
||||
`${this.getDisplayQueueProgressBar(queue)}\n\n` +
|
||||
`${this.getDisplayRepeatMode(queue.repeatMode)}` +
|
||||
`**${this.embedOptions.icons.queue} Tracks in queue**\n` +
|
||||
queueTracksListString
|
||||
)
|
||||
.setThumbnail(this.getTrackThumbnailUrl(currentTrack))
|
||||
.setFooter(this.getFooterDisplayPageInfo(interaction, queue))
|
||||
.setColor(this.embedOptions.colors.info)
|
||||
]
|
||||
});
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
private async handleNoCurrentTrack(
|
||||
logger: Logger,
|
||||
interaction: ChatInputCommandInteraction,
|
||||
queue: GuildQueue,
|
||||
queueTracksListString: string
|
||||
) {
|
||||
logger.debug('Queue exists but there is no current track.');
|
||||
|
||||
logger.debug('Responding with info embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedQueueAuthor(interaction, queue))
|
||||
.setDescription(
|
||||
`${this.getDisplayRepeatMode(queue.repeatMode)}` +
|
||||
`**${this.embedOptions.icons.queue} Tracks in queue**\n` +
|
||||
queueTracksListString
|
||||
)
|
||||
.setFooter(this.getFooterDisplayPageInfo(interaction, queue))
|
||||
.setColor(this.embedOptions.colors.info)
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
private async handleInvalidPage(
|
||||
logger: Logger,
|
||||
interaction: ChatInputCommandInteraction,
|
||||
pageIndex: number,
|
||||
totalPages: number
|
||||
) {
|
||||
logger.debug('Specified page was higher than total pages.');
|
||||
|
||||
logger.debug('Responding with warning embed.');
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.warning} Oops!**\n` +
|
||||
`Page **\`${pageIndex + 1}\`** is not a valid page number.\n\n` +
|
||||
`There are only a total of **\`${totalPages}\`** pages in the queue.`
|
||||
)
|
||||
.setColor(this.embedOptions.colors.warning)
|
||||
]
|
||||
});
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
private getDisplayRepeatMode(repeatMode: number): string {
|
||||
const repeatModesFormatted = 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.autoplay : this.embedOptions.icons.loop;
|
||||
return `**${icon} Looping**\nLoop mode is set to **\`${loopModeUserString}\`**. You can change it with **\`/loop\`**.\n\n`;
|
||||
};
|
||||
|
||||
const repeatModeString: string = queue.repeatMode === 0 ? '' : getRepeatModeMessage(queue.repeatMode);
|
||||
|
||||
if (!currentTrack) {
|
||||
logger.debug('Queue exists but there is no current track.');
|
||||
|
||||
logger.debug('Responding with info embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedQueueAuthor(interaction, queue))
|
||||
.setDescription(
|
||||
`${repeatModeString}` +
|
||||
`**${this.embedOptions.icons.queue} Tracks in queue**\n${queueString}`
|
||||
)
|
||||
.setFooter({
|
||||
text: `Page ${pageIndex + 1} of ${totalPages} (${queueLength} tracks)`
|
||||
})
|
||||
.setColor(this.embedOptions.colors.info)
|
||||
]
|
||||
});
|
||||
} else {
|
||||
logger.debug('Queue exists with current track, gathering information.');
|
||||
const timestamp: PlayerTimestamp = queue.node.getTimestamp()!;
|
||||
let bar: string = `**\`${timestamp.current.label}\`** ${queue.node.createProgressBar({
|
||||
queue: false,
|
||||
length: playerOptions.progressBar.length ?? 12,
|
||||
timecodes: playerOptions.progressBar.timecodes ?? false,
|
||||
indicator: playerOptions.progressBar.indicator ?? '🔘',
|
||||
leftChar: playerOptions.progressBar.leftChar ?? '▬',
|
||||
rightChar: playerOptions.progressBar.rightChar ?? '▬'
|
||||
})} **\`${timestamp.total.label}\`**`;
|
||||
|
||||
if (Number(currentTrack.raw.duration) === 0 || currentTrack.duration === '0:00') {
|
||||
bar = '_No duration available._';
|
||||
}
|
||||
|
||||
if (currentTrack.raw.live) {
|
||||
bar = `${this.embedOptions.icons.liveTrack} **\`LIVE\`** - Playing continuously from live source.`;
|
||||
}
|
||||
|
||||
logger.debug('Responding with info embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedQueueAuthor(interaction, queue))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.audioPlaying} Now playing**\n` +
|
||||
(currentTrack
|
||||
? `**[${currentTrack.title}](${currentTrack.raw.url ?? currentTrack.url})**`
|
||||
: 'None') +
|
||||
`\nRequested by: <@${currentTrack.requestedBy?.id}>` +
|
||||
`\n ${bar}\n\n` +
|
||||
`${repeatModeString}` +
|
||||
`**${this.embedOptions.icons.queue} Tracks in queue**\n${queueString}`
|
||||
)
|
||||
.setThumbnail(this.getTrackThumbnailUrl(currentTrack))
|
||||
.setFooter({
|
||||
text: `Page ${pageIndex + 1} of ${totalPages} (${queueLength} tracks)`
|
||||
})
|
||||
.setColor(this.embedOptions.colors.info)
|
||||
]
|
||||
});
|
||||
if (repeatMode === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const loopModeUserString: string = repeatModesFormatted.get(repeatMode)!;
|
||||
const icon = repeatMode === 3 ? this.embedOptions.icons.autoplay : this.embedOptions.icons.loop;
|
||||
return (
|
||||
`**${icon} Looping**\n` +
|
||||
`Loop mode is set to **\`${loopModeUserString}\`**. You can change it with **\`/loop\`**.\n\n`
|
||||
);
|
||||
}
|
||||
|
||||
private getPageIndex(interaction: ChatInputCommandInteraction): number {
|
||||
return (interaction.options.getNumber('page') || 1) - 1;
|
||||
}
|
||||
|
||||
private getTotalPages(queue: GuildQueue): number {
|
||||
return Math.ceil(queue.tracks.data.length / 10) || 1;
|
||||
}
|
||||
|
||||
private getQueueTracksListString(queue: GuildQueue, pageIndex: number): string {
|
||||
if (!queue || queue.tracks.data.length === 0) {
|
||||
return 'The queue is empty, add some tracks with **`/play`**!';
|
||||
}
|
||||
|
||||
return queue.tracks.data
|
||||
.slice(pageIndex * 10, pageIndex * 10 + 10)
|
||||
.map((track, index) => {
|
||||
return `**${pageIndex * 10 + index + 1}.** ${this.getDisplayTrackDurationAndUrl(track)}`;
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user