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

Commit

Permalink
fix: refactored nowplaying skip button component + more common methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Sep 13, 2023
1 parent a9ce285 commit 78e8f12
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 92 deletions.
20 changes: 20 additions & 0 deletions src/classes/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
20 changes: 0 additions & 20 deletions src/interactions/commands/player/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/interactions/components/filters-disable-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
],
Expand Down
32 changes: 17 additions & 15 deletions src/interactions/components/filters-select-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
94 changes: 38 additions & 56 deletions src/interactions/components/nowplaying-skip-button.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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\`**.`;
};
logger.debug('Responding with success embed.');
return await this.handleSuccess(interaction, skippedTrack, queue);
}

const repeatModeString: string = queue.repeatMode === 0 ? '' : getRepeatModeMessage(queue.repeatMode);
private async handleNoQueue(interaction: MessageComponentInteraction) {
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: []
});
}

logger.debug('Responding with success embed.');
private async handleAlreadySkipped(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!**\nThis track has already been skipped or is no longer playing.`
)
.setThumbnail(skippedTrack.thumbnail)
.setColor(this.embedOptions.colors.success)
.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();

0 comments on commit 78e8f12

Please sign in to comment.