mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-02 14:38:49 +00:00
134 lines
5.1 KiB
TypeScript
134 lines
5.1 KiB
TypeScript
import { GuildQueue, GuildQueueHistory, Track, useHistory, useQueue } from 'discord-player';
|
|
import { EmbedBuilder, MessageComponentInteraction } from 'discord.js';
|
|
import { BaseComponentInteraction } from '../../classes/interactions';
|
|
import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes';
|
|
import { checkQueueCurrentTrack, checkQueueExists } from '../../utils/validation/queueValidator';
|
|
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../utils/validation/voiceChannelValidator';
|
|
import { Logger } from 'pino';
|
|
import { TFunction } from 'i18next';
|
|
import { useServerTranslator } from '../../common/localeUtil';
|
|
import { formatSlashCommand } from '../../common/formattingUtils';
|
|
|
|
class ActionPreviousButton extends BaseComponentInteraction {
|
|
constructor() {
|
|
super('action-previous-button');
|
|
}
|
|
|
|
async execute(params: BaseComponentParams): BaseComponentReturnType {
|
|
const { executionId, interaction, referenceId } = params;
|
|
const logger = this.getLogger(this.name, executionId, interaction);
|
|
const translator = useServerTranslator(interaction);
|
|
|
|
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
|
|
const history: GuildQueueHistory = useHistory(interaction.guild!.id)!;
|
|
|
|
await this.runValidators({ interaction, queue, executionId }, [
|
|
checkInVoiceChannel,
|
|
checkSameVoiceChannel,
|
|
checkQueueExists,
|
|
checkQueueCurrentTrack
|
|
]);
|
|
|
|
if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) {
|
|
return await this.handleNoQueue(interaction, translator);
|
|
}
|
|
|
|
if (queue.currentTrack!.id !== referenceId) {
|
|
return await this.handleAlreadySkipped(interaction, translator);
|
|
}
|
|
|
|
return await this.handleBackToPreviousTrack(logger, interaction, history, translator);
|
|
}
|
|
|
|
private async handleBackToPreviousTrack(
|
|
logger: Logger,
|
|
interaction: MessageComponentInteraction,
|
|
history: GuildQueueHistory,
|
|
translator: TFunction
|
|
) {
|
|
if (history.tracks.data.length === 0) {
|
|
return await this.handleNoTracksInHistory(logger, interaction, translator);
|
|
}
|
|
|
|
await history.back();
|
|
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
|
|
const currentTrack: Track = queue.currentTrack!;
|
|
logger.debug('Recovered track from history.');
|
|
return await this.handleSuccess(interaction, currentTrack, translator);
|
|
}
|
|
private async handleNoTracksInHistory(
|
|
logger: Logger,
|
|
interaction: MessageComponentInteraction,
|
|
translator: TFunction
|
|
) {
|
|
logger.debug('No tracks in history.');
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setDescription(
|
|
translator('commands.back.trackHistoryEmpty', {
|
|
icon: this.embedOptions.icons.warning,
|
|
playCommand: formatSlashCommand('play', translator)
|
|
})
|
|
)
|
|
.setColor(this.embedOptions.colors.warning)
|
|
]
|
|
});
|
|
}
|
|
|
|
private async handleNoQueue(interaction: MessageComponentInteraction, translator: TFunction) {
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setDescription(
|
|
translator('validation.queueNoCurrentTrack', {
|
|
icon: this.embedOptions.icons.warning,
|
|
playCommand: formatSlashCommand('play', translator)
|
|
})
|
|
)
|
|
.setColor(this.embedOptions.colors.warning)
|
|
],
|
|
components: []
|
|
});
|
|
}
|
|
|
|
private async handleAlreadySkipped(interaction: MessageComponentInteraction, translator: TFunction) {
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setDescription(
|
|
translator('validation.trackNotPlayingAnymore', {
|
|
icon: this.embedOptions.icons.warning
|
|
})
|
|
)
|
|
.setColor(this.embedOptions.colors.warning)
|
|
],
|
|
components: []
|
|
});
|
|
}
|
|
|
|
private async handleSuccess(
|
|
interaction: MessageComponentInteraction,
|
|
recoveredTrack: Track,
|
|
translator: TFunction
|
|
) {
|
|
const successEmbed = new EmbedBuilder()
|
|
.setAuthor(this.getEmbedUserAuthor(interaction))
|
|
.setDescription(
|
|
translator('commands.back.trackRecovered', {
|
|
icon: this.embedOptions.icons.back,
|
|
track: this.getDisplayTrackDurationAndUrl(recoveredTrack, translator)
|
|
})
|
|
)
|
|
.setThumbnail(recoveredTrack.thumbnail)
|
|
.setColor(this.embedOptions.colors.success);
|
|
|
|
return await interaction.editReply({
|
|
embeds: [successEmbed],
|
|
components: []
|
|
});
|
|
}
|
|
}
|
|
|
|
export default new ActionPreviousButton();
|