mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-02 22:16:13 +00:00
fix: refactored nowplaying skip button component + more common methods
This commit is contained in:
1 parent
a9ce285650
commit
78e8f12c99
5 files changed
+78
-93
No files matched your search
@@ -152,6 +152,26 @@ abstract class BaseInteraction {
|
||||
return progressBar;
|
||||
}
|
||||
|
||||
protected getDisplayRepeatMode(repeatMode: number): string {
|
||||
const repeatModesFormatted = new Map([
|
||||
[0, 'disabled'],
|
||||
[1, 'track'],
|
||||
[2, 'queue'],
|
||||
[3, 'autoplay']
|
||||
]);
|
||||
|
||||
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`
|
||||
);
|
||||
}
|
||||
|
||||
abstract execute(
|
||||
params: BaseInteractionParams
|
||||
): Promise<Message<boolean> | ApplicationCommandOptionChoiceData | void>;
|
||||
|
||||
@@ -124,26 +124,6 @@ class QueueCommand extends BaseSlashCommandInteraction {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
private getDisplayRepeatMode(repeatMode: number): string {
|
||||
const repeatModesFormatted = new Map([
|
||||
[0, 'disabled'],
|
||||
[1, 'track'],
|
||||
[2, 'queue'],
|
||||
[3, 'autoplay']
|
||||
]);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,8 @@ class FiltersDisableButtonComponent extends BaseComponentInteraction {
|
||||
new EmbedBuilder()
|
||||
.setAuthor(this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.success} Disabled filters**\nAll audio filters have been disabled.`
|
||||
`**${this.embedOptions.icons.success} Disabled filters**\n` +
|
||||
'All audio filters have been disabled.'
|
||||
)
|
||||
.setColor(this.embedOptions.colors.success)
|
||||
],
|
||||
|
||||
@@ -69,24 +69,26 @@ class FiltersSelectMenuComponent extends BaseComponentInteraction {
|
||||
}
|
||||
|
||||
private buildSuccessEmbed(selectMenuInteraction: StringSelectMenuInteraction) {
|
||||
const enabledFilters: string = selectMenuInteraction.values
|
||||
.map((enabledFilter: string) => {
|
||||
const filter: FFmpegFilterOption | undefined = ffmpegFilterOptions.availableFilters.find(
|
||||
(filter) => enabledFilter == filter.value
|
||||
);
|
||||
|
||||
if (!filter) {
|
||||
return enabledFilter;
|
||||
}
|
||||
|
||||
return `- **${filter.emoji} ${filter.label}**`;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
return new EmbedBuilder()
|
||||
.setAuthor(this.getEmbedUserAuthor(selectMenuInteraction))
|
||||
.setDescription(
|
||||
`**${
|
||||
this.embedOptions.icons.success
|
||||
} Filters toggled**\nNow using these filters:\n${selectMenuInteraction.values
|
||||
.map((enabledFilter: string) => {
|
||||
const filter: FFmpegFilterOption | undefined = ffmpegFilterOptions.availableFilters.find(
|
||||
(filter) => enabledFilter == filter.value
|
||||
);
|
||||
|
||||
if (!filter) {
|
||||
return enabledFilter;
|
||||
}
|
||||
|
||||
return `- **${filter.emoji} ${filter.label}**`;
|
||||
})
|
||||
.join('\n')}`
|
||||
`**${this.embedOptions.icons.success} Filters toggled**\n` +
|
||||
'Now using these filters:\n' +
|
||||
`${enabledFilters}`
|
||||
)
|
||||
.setColor(this.embedOptions.colors.success);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { GuildQueue, Track, useQueue } from 'discord-player';
|
||||
import { EmbedBuilder } from 'discord.js';
|
||||
import { EmbedBuilder, MessageComponentInteraction } from 'discord.js';
|
||||
import { BaseComponentInteraction } from '../../classes/interactions';
|
||||
import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes';
|
||||
import { checkQueueCurrentTrack, checkQueueExists } from '../../utils/validation/queueValidator';
|
||||
@@ -24,81 +24,63 @@ class NowplayingSkipButton extends BaseComponentInteraction {
|
||||
]);
|
||||
|
||||
if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) {
|
||||
logger.debug('Tried skipping track but there was no queue.');
|
||||
|
||||
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)
|
||||
],
|
||||
components: []
|
||||
});
|
||||
return await this.handleNoQueue(interaction);
|
||||
}
|
||||
|
||||
if (queue.currentTrack!.id !== referenceId) {
|
||||
logger.debug('Tried skipping track but it is not the current track and therefore already skipped/removed.');
|
||||
|
||||
logger.debug('Responding with warning embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.warning} Oops!**\nThis track has already been skipped or is no longer playing.`
|
||||
)
|
||||
.setColor(this.embedOptions.colors.warning)
|
||||
],
|
||||
components: []
|
||||
});
|
||||
return await this.handleAlreadySkipped(interaction);
|
||||
}
|
||||
|
||||
const skippedTrack: Track = queue.currentTrack!;
|
||||
let durationFormat =
|
||||
Number(skippedTrack.raw.duration) === 0 || skippedTrack.duration === '0:00'
|
||||
? ''
|
||||
: `\`${skippedTrack.duration}\``;
|
||||
|
||||
if (skippedTrack.raw.live) {
|
||||
durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``;
|
||||
}
|
||||
queue.node.skip();
|
||||
logger.debug('Skipped the 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 this.handleSuccess(interaction, skippedTrack, queue);
|
||||
}
|
||||
|
||||
private async handleNoQueue(interaction: MessageComponentInteraction) {
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor(this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.skipped} Skipped track**\n**${durationFormat} [${
|
||||
skippedTrack.title
|
||||
}](${skippedTrack.raw.url ?? skippedTrack.url})**` + `\n\n${repeatModeString}`
|
||||
`**${this.embedOptions.icons.warning} Oops!**\nThere is nothing currently playing. First add some tracks with **\`/play\`**!`
|
||||
)
|
||||
.setThumbnail(skippedTrack.thumbnail)
|
||||
.setColor(this.embedOptions.colors.success)
|
||||
.setColor(this.embedOptions.colors.warning)
|
||||
],
|
||||
components: []
|
||||
});
|
||||
}
|
||||
|
||||
private async handleAlreadySkipped(interaction: MessageComponentInteraction) {
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.warning} Oops!**\nThis track has already been skipped or is no longer playing.`
|
||||
)
|
||||
.setColor(this.embedOptions.colors.warning)
|
||||
],
|
||||
components: []
|
||||
});
|
||||
}
|
||||
|
||||
private async handleSuccess(interaction: MessageComponentInteraction, skippedTrack: Track, queue: GuildQueue) {
|
||||
const successEmbed = new EmbedBuilder()
|
||||
.setAuthor(this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.skipped} Skipped track**\n` +
|
||||
`**${this.getDisplayTrackDurationAndUrl(skippedTrack)}\n\n` +
|
||||
`${this.getDisplayRepeatMode(queue.repeatMode)}`
|
||||
)
|
||||
.setThumbnail(skippedTrack.thumbnail)
|
||||
.setColor(this.embedOptions.colors.success);
|
||||
|
||||
return await interaction.editReply({
|
||||
embeds: [successEmbed],
|
||||
components: []
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default new NowplayingSkipButton();
|
||||
Reference in new issue
Block a user