From 402cd69336c9ea2faa62a409dd028314482a8630 Mon Sep 17 00:00:00 2001 From: tacheometry <39647014+tacheometry@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:47:49 +0200 Subject: [PATCH] Queue command buttons --- src/interactions/commands/player/queue.ts | 47 +++++++- .../components/nowplaying-back-button.ts | 111 ++++++++++++++++++ .../components/nowplaying-playpause-button.ts | 92 +++++++++++++++ 3 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 src/interactions/components/nowplaying-back-button.ts create mode 100644 src/interactions/components/nowplaying-playpause-button.ts diff --git a/src/interactions/commands/player/queue.ts b/src/interactions/commands/player/queue.ts index 247bb33..fa506fd 100644 --- a/src/interactions/commands/player/queue.ts +++ b/src/interactions/commands/player/queue.ts @@ -1,5 +1,16 @@ import { GuildQueue, Track, useQueue } from 'discord-player'; -import { ChatInputCommandInteraction, EmbedBuilder, EmbedFooterData, SlashCommandBuilder } from 'discord.js'; +import { + APIActionRowComponent, + APIButtonComponent, + APIMessageActionRowComponent, + ButtonBuilder, + ButtonStyle, + ChatInputCommandInteraction, + ComponentType, + EmbedBuilder, + EmbedFooterData, + SlashCommandBuilder +} from 'discord.js'; import { Logger } from 'pino'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; @@ -56,6 +67,37 @@ class QueueCommand extends BaseSlashCommandInteraction { ) { logger.debug('Queue exists with current track, gathering information.'); + const playPauseButton: APIButtonComponent = new ButtonBuilder() + .setCustomId(`nowplaying-playpause-button_${currentTrack.id}`) + // .setLabel('Pause/Resume track') + .setStyle(ButtonStyle.Secondary) + .setEmoji(this.embedOptions.icons.pauseResumeTrack) + .toJSON(); + const components: APIMessageActionRowComponent[] = [playPauseButton]; + if (queue.history.tracks.data.length > 0) { + const backButton: APIButtonComponent = new ButtonBuilder() + .setCustomId(`nowplaying-back-button_${currentTrack.id}`) + // .setLabel('Previous track') + .setStyle(ButtonStyle.Secondary) + .setEmoji(this.embedOptions.icons.previousTrack) + .toJSON(); + components.unshift(backButton); + } + if (queue.tracks.data.length > 0) { + const skipButton: APIButtonComponent = new ButtonBuilder() + .setCustomId(`nowplaying-skip-button_${currentTrack.id}`) + // .setLabel('Skip track') + .setStyle(ButtonStyle.Secondary) + .setEmoji(this.embedOptions.icons.nextTrack) + .toJSON(); + components.push(skipButton); + } + + const embedActionRow: APIActionRowComponent = { + type: ComponentType.ActionRow, + components + }; + logger.debug('Responding with info embed.'); await interaction.editReply({ embeds: [ @@ -73,7 +115,8 @@ class QueueCommand extends BaseSlashCommandInteraction { .setThumbnail(this.getTrackThumbnailUrl(currentTrack)) .setFooter(this.getDisplayFullFooterInfo(interaction, queue)) .setColor(this.embedOptions.colors.info) - ] + ], + components: [embedActionRow] }); return Promise.resolve(); } diff --git a/src/interactions/components/nowplaying-back-button.ts b/src/interactions/components/nowplaying-back-button.ts new file mode 100644 index 0000000..94744fe --- /dev/null +++ b/src/interactions/components/nowplaying-back-button.ts @@ -0,0 +1,111 @@ +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'; + +class NowplayingBackButton extends BaseComponentInteraction { + constructor() { + super('nowplaying-back-button'); + } + + async execute(params: BaseComponentParams): BaseComponentReturnType { + const { executionId, interaction, referenceId } = params; + const logger = this.getLogger(this.name, executionId, 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); + } + + if (queue.currentTrack!.id !== referenceId) { + return await this.handleAlreadySkipped(interaction); + } + + return await this.handleBackToPreviousTrack(logger, interaction, history); + } + + private async handleBackToPreviousTrack( + logger: Logger, + interaction: MessageComponentInteraction, + history: GuildQueueHistory + ) { + if (history.tracks.data.length === 0) { + return await this.handleNoTracksInHistory(logger, interaction); + } + + 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); + } + private async handleNoTracksInHistory(logger: Logger, interaction: MessageComponentInteraction) { + logger.debug('No tracks in history.'); + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${this.embedOptions.icons.warning} Oops!**\n` + + 'The history is empty, add some tracks with **`/play`**!' + ) + .setColor(this.embedOptions.colors.warning) + ] + }); + } + + 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: [] + }); + } + + 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, recoveredTrack: Track) { + const successEmbed = new EmbedBuilder() + .setAuthor(this.getEmbedUserAuthor(interaction)) + .setDescription( + `**${this.embedOptions.icons.back} Recovered track**\n` + + `${this.getDisplayTrackDurationAndUrl(recoveredTrack)}` + ) + .setThumbnail(recoveredTrack.thumbnail) + .setColor(this.embedOptions.colors.success); + + return await interaction.editReply({ + embeds: [successEmbed], + components: [] + }); + } +} + +export default new NowplayingBackButton(); diff --git a/src/interactions/components/nowplaying-playpause-button.ts b/src/interactions/components/nowplaying-playpause-button.ts new file mode 100644 index 0000000..346295b --- /dev/null +++ b/src/interactions/components/nowplaying-playpause-button.ts @@ -0,0 +1,92 @@ +import { GuildQueue, Track, 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'; + +class NowplayingPlayPauseButton extends BaseComponentInteraction { + constructor() { + super('nowplaying-playpause-button'); + } + + async execute(params: BaseComponentParams): BaseComponentReturnType { + const { executionId, interaction, referenceId } = params; + const logger = this.getLogger(this.name, executionId, interaction); + + const queue: GuildQueue = useQueue(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); + } + + if (queue.currentTrack!.id !== referenceId) { + return await this.handleAlreadySkipped(interaction); + } + + const currentTrack: Track = queue.currentTrack!; + if (queue.node.isPaused()) { + queue.node.resume(); + logger.debug('Resumed the track.'); + } else { + queue.node.pause(); + logger.debug('Paused the track.'); + } + + logger.debug('Responding with success embed.'); + return await this.handleSuccess(interaction, currentTrack, queue); + } + + 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: [] + }); + } + + private async handleAlreadySkipped(interaction: MessageComponentInteraction) { + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${this.embedOptions.icons.warning} Oops!**\nThis track has been skipped or is no longer playing.` + ) + .setColor(this.embedOptions.colors.warning) + ], + components: [] + }); + } + + private async handleSuccess(interaction: MessageComponentInteraction, track: Track, queue: GuildQueue) { + const successEmbed = new EmbedBuilder() + .setAuthor(this.getEmbedUserAuthor(interaction)) + .setDescription( + `**${this.embedOptions.icons.pauseResumed} ${ + queue.node.isPaused() ? 'Paused Track' : 'Resumed track' + }**\n ${this.getDisplayTrackDurationAndUrl(queue.currentTrack!)}\n\n` + + `${this.getDisplayRepeatMode(queue.repeatMode, 'success')}` + ) + .setThumbnail(track.thumbnail) + .setColor(this.embedOptions.colors.success); + + return await interaction.editReply({ + embeds: [successEmbed], + components: [] + }); + } +} + +export default new NowplayingPlayPauseButton();