From 870a432490b515f550b4b2d038cc22d2625a6b68 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Fri, 15 Mar 2024 20:04:13 +0100 Subject: [PATCH] Refactor creation of action buttons in queue slashcommand --- src/common/utils/createActionButton.ts | 25 +++++++++ src/interactions/slashcommands/queue.ts | 72 ++++++++++++------------- 2 files changed, 61 insertions(+), 36 deletions(-) create mode 100644 src/common/utils/createActionButton.ts diff --git a/src/common/utils/createActionButton.ts b/src/common/utils/createActionButton.ts new file mode 100644 index 0000000..c0eec6d --- /dev/null +++ b/src/common/utils/createActionButton.ts @@ -0,0 +1,25 @@ +import { + APIActionRowComponent, + APIButtonComponent, + APIMessageActionRowComponent, + ButtonBuilder, + ButtonStyle +} from 'discord.js'; + +export function createNewActionButton( + customId: string, + emoji: string, + embedActionRow: APIActionRowComponent | null +): APIButtonComponent { + const actionButton: APIButtonComponent = new ButtonBuilder() + .setCustomId(customId) + .setStyle(ButtonStyle.Secondary) + .setEmoji(emoji) + .toJSON(); + + if (embedActionRow) { + embedActionRow.components.push(actionButton); + } + + return actionButton; +} diff --git a/src/interactions/slashcommands/queue.ts b/src/interactions/slashcommands/queue.ts index 552a48c..e0ea9c0 100644 --- a/src/interactions/slashcommands/queue.ts +++ b/src/interactions/slashcommands/queue.ts @@ -1,10 +1,7 @@ import { GuildQueue, Track, useQueue } from 'discord-player'; import { APIActionRowComponent, - APIButtonComponent, APIMessageActionRowComponent, - ButtonBuilder, - ButtonStyle, ChatInputCommandInteraction, ComponentType, EmbedBuilder, @@ -18,6 +15,7 @@ import { checkQueueExists } from '../../common/validation/queueValidator'; import { checkInVoiceChannel, checkSameVoiceChannel } from '../../common/validation/voiceChannelValidator'; import { formatDuration, formatRepeatModeDetailed, formatSlashCommand } from '../../common/utils/formattingUtils'; import { localizeCommand, useServerTranslator, Translator } from '../../common/utils/localeUtil'; +import { createNewActionButton } from '../../common/utils/createActionButton'; class QueueCommand extends BaseSlashCommandInteraction { constructor() { @@ -66,39 +64,33 @@ class QueueCommand extends BaseSlashCommandInteraction { return await this.handleNoCurrentTrack(logger, interaction, queue, queueTracksListString, translator); } - private async handleCurrentTrack( - logger: Logger, - interaction: ChatInputCommandInteraction, - queue: GuildQueue, + private createEmbedActionRow( currentTrack: Track, - queueTracksListString: string, + queue: GuildQueue, translator: Translator - ) { - logger.debug('Queue exists with current track, gathering information.'); + ): APIActionRowComponent { + const embedActionRow: APIActionRowComponent = { + type: ComponentType.ActionRow, + components: [] + }; - const components: APIMessageActionRowComponent[] = []; + const previousButton = createNewActionButton( + `action-previous-button_${currentTrack.id}`, + this.embedOptions.icons.previousTrack, + embedActionRow + ); - const previousButton: APIButtonComponent = new ButtonBuilder() - .setDisabled(queue.history.tracks.data.length > 0 ? false : true) - .setCustomId(`action-previous-button_${currentTrack.id}`) - .setStyle(ButtonStyle.Secondary) - .setEmoji(this.embedOptions.icons.previousTrack) - .toJSON(); - components.push(previousButton); + const playPauseButton = createNewActionButton( + `action-pauseresume-button_${currentTrack.id}`, + this.embedOptions.icons.pauseResumeTrack, + embedActionRow + ); - const playPauseButton: APIButtonComponent = new ButtonBuilder() - .setCustomId(`action-pauseresume-button_${currentTrack.id}`) - .setStyle(ButtonStyle.Secondary) - .setEmoji(this.embedOptions.icons.pauseResumeTrack) - .toJSON(); - components.push(playPauseButton); - - const skipButton: APIButtonComponent = new ButtonBuilder() - .setCustomId(`action-skip-button_${currentTrack.id}`) - .setStyle(ButtonStyle.Secondary) - .setEmoji(this.embedOptions.icons.nextTrack) - .toJSON(); - components.push(skipButton); + const skipButton = createNewActionButton( + `action-skip-button_${currentTrack.id}`, + this.embedOptions.icons.nextTrack, + embedActionRow + ); if (this.embedOptions.components.showButtonLabels) { previousButton.label = translator('musicPlayerCommon.controls.previous'); @@ -108,10 +100,18 @@ class QueueCommand extends BaseSlashCommandInteraction { skipButton.label = translator('musicPlayerCommon.controls.skip'); } - const embedActionRow: APIActionRowComponent = { - type: ComponentType.ActionRow, - components - }; + return embedActionRow; + } + + private async handleCurrentTrack( + logger: Logger, + interaction: ChatInputCommandInteraction, + queue: GuildQueue, + currentTrack: Track, + queueTracksListString: string, + translator: Translator + ) { + logger.debug('Queue exists with current track, gathering information.'); logger.debug('Responding with info embed.'); await interaction.reply({ @@ -138,7 +138,7 @@ class QueueCommand extends BaseSlashCommandInteraction { .setFooter(this.getDisplayFullFooterInfo(interaction, queue, translator)) .setColor(this.embedOptions.colors.info) ], - components: [embedActionRow] + components: [this.createEmbedActionRow(currentTrack, queue, translator)] }); return Promise.resolve(); }