Refactor creation of action buttons in queue slashcommand

This commit is contained in:
Marius Begby committed 2024-03-15 20:04:13 +01:00
1 parent b36244054d
commit 870a432490
2 files changed
+61 -36

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
import {
APIActionRowComponent,
APIButtonComponent,
APIMessageActionRowComponent,
ButtonBuilder,
ButtonStyle
} from 'discord.js';
export function createNewActionButton(
customId: string,
emoji: string,
embedActionRow: APIActionRowComponent<APIMessageActionRowComponent> | null
): APIButtonComponent {
const actionButton: APIButtonComponent = new ButtonBuilder()
.setCustomId(customId)
.setStyle(ButtonStyle.Secondary)
.setEmoji(emoji)
.toJSON();
if (embedActionRow) {
embedActionRow.components.push(actionButton);
}
return actionButton;
}
+36 -36
View File
@@ -1,10 +1,7 @@
import { GuildQueue, Track, useQueue } from 'discord-player'; import { GuildQueue, Track, useQueue } from 'discord-player';
import { import {
APIActionRowComponent, APIActionRowComponent,
APIButtonComponent,
APIMessageActionRowComponent, APIMessageActionRowComponent,
ButtonBuilder,
ButtonStyle,
ChatInputCommandInteraction, ChatInputCommandInteraction,
ComponentType, ComponentType,
EmbedBuilder, EmbedBuilder,
@@ -18,6 +15,7 @@ import { checkQueueExists } from '../../common/validation/queueValidator';
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../common/validation/voiceChannelValidator'; import { checkInVoiceChannel, checkSameVoiceChannel } from '../../common/validation/voiceChannelValidator';
import { formatDuration, formatRepeatModeDetailed, formatSlashCommand } from '../../common/utils/formattingUtils'; import { formatDuration, formatRepeatModeDetailed, formatSlashCommand } from '../../common/utils/formattingUtils';
import { localizeCommand, useServerTranslator, Translator } from '../../common/utils/localeUtil'; import { localizeCommand, useServerTranslator, Translator } from '../../common/utils/localeUtil';
import { createNewActionButton } from '../../common/utils/createActionButton';
class QueueCommand extends BaseSlashCommandInteraction { class QueueCommand extends BaseSlashCommandInteraction {
constructor() { constructor() {
@@ -66,39 +64,33 @@ class QueueCommand extends BaseSlashCommandInteraction {
return await this.handleNoCurrentTrack(logger, interaction, queue, queueTracksListString, translator); return await this.handleNoCurrentTrack(logger, interaction, queue, queueTracksListString, translator);
} }
private async handleCurrentTrack( private createEmbedActionRow(
logger: Logger,
interaction: ChatInputCommandInteraction,
queue: GuildQueue,
currentTrack: Track, currentTrack: Track,
queueTracksListString: string, queue: GuildQueue,
translator: Translator translator: Translator
) { ): APIActionRowComponent<APIMessageActionRowComponent> {
logger.debug('Queue exists with current track, gathering information.'); const embedActionRow: APIActionRowComponent<APIMessageActionRowComponent> = {
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() const playPauseButton = createNewActionButton(
.setDisabled(queue.history.tracks.data.length > 0 ? false : true) `action-pauseresume-button_${currentTrack.id}`,
.setCustomId(`action-previous-button_${currentTrack.id}`) this.embedOptions.icons.pauseResumeTrack,
.setStyle(ButtonStyle.Secondary) embedActionRow
.setEmoji(this.embedOptions.icons.previousTrack) );
.toJSON();
components.push(previousButton);
const playPauseButton: APIButtonComponent = new ButtonBuilder() const skipButton = createNewActionButton(
.setCustomId(`action-pauseresume-button_${currentTrack.id}`) `action-skip-button_${currentTrack.id}`,
.setStyle(ButtonStyle.Secondary) this.embedOptions.icons.nextTrack,
.setEmoji(this.embedOptions.icons.pauseResumeTrack) embedActionRow
.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);
if (this.embedOptions.components.showButtonLabels) { if (this.embedOptions.components.showButtonLabels) {
previousButton.label = translator('musicPlayerCommon.controls.previous'); previousButton.label = translator('musicPlayerCommon.controls.previous');
@@ -108,10 +100,18 @@ class QueueCommand extends BaseSlashCommandInteraction {
skipButton.label = translator('musicPlayerCommon.controls.skip'); skipButton.label = translator('musicPlayerCommon.controls.skip');
} }
const embedActionRow: APIActionRowComponent<APIMessageActionRowComponent> = { return embedActionRow;
type: ComponentType.ActionRow, }
components
}; 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.'); logger.debug('Responding with info embed.');
await interaction.reply({ await interaction.reply({
@@ -138,7 +138,7 @@ class QueueCommand extends BaseSlashCommandInteraction {
.setFooter(this.getDisplayFullFooterInfo(interaction, queue, translator)) .setFooter(this.getDisplayFullFooterInfo(interaction, queue, translator))
.setColor(this.embedOptions.colors.info) .setColor(this.embedOptions.colors.info)
], ],
components: [embedActionRow] components: [this.createEmbedActionRow(currentTrack, queue, translator)]
}); });
return Promise.resolve(); return Promise.resolve();
} }