diff --git a/src/interactions/commands/player/nowplaying.ts b/src/interactions/commands/player/nowplaying.ts index 025037a..40fb354 100644 --- a/src/interactions/commands/player/nowplaying.ts +++ b/src/interactions/commands/player/nowplaying.ts @@ -8,6 +8,7 @@ import { ButtonStyle, ComponentType, EmbedBuilder, + EmbedField, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; @@ -39,44 +40,76 @@ class NowPlayingCommand extends BaseSlashCommandInteraction { checkQueueCurrentTrack ]); - const sourceStringsFormatted: Map = new Map([ - ['youtube', 'YouTube'], - ['soundcloud', 'SoundCloud'], - ['spotify', 'Spotify'], - ['apple_music', 'Apple Music'], - ['arbitrary', 'Direct source'] - ]); - - const sourceIcons: Map = new Map([ - ['youtube', this.embedOptions.icons.sourceYouTube], - ['soundcloud', this.embedOptions.icons.sourceSoundCloud], - ['spotify', this.embedOptions.icons.sourceSpotify], - ['apple_music', this.embedOptions.icons.sourceAppleMusic], - ['arbitrary', this.embedOptions.icons.sourceArbitrary] - ]); - + const tracksInQueueCount: number = queue.tracks.data.length; const currentTrack: Track = queue.currentTrack!; + const displayTrackUrl: string = this.getDisplayTrackUrl(currentTrack); + const displayTrackRequestedBy: string = this.getDisplayTrackRequestedBy(currentTrack); + const displayTrackPlayingStatus: string = this.getDisplayTrackPlayingStatus(queue); + const displayQueueRepeatMode: string = this.getDisplayQueueRepeatMode(queue); + const displayEmbedProgressBar: string = await this.getDisplayQueueProgressBar(queue); - let author: string = currentTrack.author ? currentTrack.author : 'Unavailable'; - if (author === 'cdn.discordapp.com') { - author = 'Unavailable'; - } - const plays: number = currentTrack.views !== 0 ? currentTrack.views : 0; + const customId: string = `nowplaying-skip-button_${currentTrack.id}`; + logger.debug(`Generated custom id for skip button: ${customId}`); - let displayPlays: string = plays.toLocaleString('en-US'); + const skipButton: APIButtonComponent = new ButtonBuilder() + .setCustomId(customId) + .setLabel('Skip track') + .setStyle(ButtonStyle.Secondary) + .setEmoji(this.embedOptions.icons.nextTrack) + .toJSON(); - const metadata: TrackMetadata = currentTrack.metadata as TrackMetadata; + const embedActionRow: APIActionRowComponent = { + type: ComponentType.ActionRow, + components: [skipButton] + }; - if (plays === 0 && metadata.bridge && metadata.bridge.views !== 0 && metadata.bridge.views !== undefined) { - displayPlays = metadata.bridge.views.toLocaleString('en-US'); - } else if (plays === 0) { + logger.debug('Sending info embed with action row components.'); + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor(await this.getEmbedQueueAuthor(interaction, queue)) + .setDescription( + `${displayTrackPlayingStatus}\n` + + `${displayTrackUrl}\n` + + `${displayTrackRequestedBy}\n` + + `${displayEmbedProgressBar}\n\n ` + + `${displayQueueRepeatMode}\n\n` + ) + .addFields(this.getEmbedFields(currentTrack)) + .setFooter({ + text: tracksInQueueCount ? `${tracksInQueueCount} other tracks in the queue...` : ' ' + }) + .setThumbnail(queue.currentTrack!.thumbnail) + .setColor(this.embedOptions.colors.info) + ], + components: [embedActionRow as APIActionRowComponent] + }); + } + + private getDisplayPlays(currentTrack: Track | undefined): string { + const trackMetadata = currentTrack?.metadata as TrackMetadata; + + let displayPlays: string = + (currentTrack?.views || trackMetadata?.bridge?.views || 0).toLocaleString('en-US') ?? '0'; + + if (displayPlays === '0') { displayPlays = 'Unavailable'; } - const source: string = sourceStringsFormatted.get(currentTrack.raw.source!) ?? 'Unavailable'; - const queueLength: number = queue.tracks.data.length; + return displayPlays; + } + + private getDisplayTrackAuthor(currentTrack: Track | undefined): string { + let author: string = currentTrack?.author ? currentTrack.author : 'Unavailable'; + if (author === 'cdn.discordapp.com') { + author = 'Unavailable'; + } + return author; + } + + private async getDisplayQueueProgressBar(queue: GuildQueue): Promise { const timestamp: PlayerTimestamp = queue.node.getTimestamp()!; - let bar: string = `**\`${timestamp.current.label}\`** ${queue.node.createProgressBar({ + let progressBar: string = `**\`${timestamp.current.label}\`** ${queue.node.createProgressBar({ queue: false, length: playerOptions.progressBar.length ?? 12, timecodes: playerOptions.progressBar.timecodes ?? false, @@ -85,29 +118,49 @@ class NowPlayingCommand extends BaseSlashCommandInteraction { rightChar: playerOptions.progressBar.rightChar ?? '▬' })} **\`${timestamp.total.label}\`**`; - if (Number(currentTrack.raw.duration) === 0 || currentTrack.duration === '0:00') { - bar = '_No duration available._'; + if (Number(queue.currentTrack?.raw.duration) === 0 || queue.currentTrack?.duration === '0:00') { + progressBar = '_No duration available._'; } - if (currentTrack.raw.live) { - bar = `${this.embedOptions.icons.liveTrack} **\`LIVE\`** - Playing continuously from live source.`; + if (queue.currentTrack?.raw.live) { + progressBar = `${this.embedOptions.icons.liveTrack} **\`LIVE\`** - Playing continuously from live source.`; } - const customId: string = `nowplaying-skip-button_${currentTrack.id}`; - logger.debug(`Generated custom id for skip button: ${customId}`); + return progressBar; + } - const nowPlayingButton: APIButtonComponent = new ButtonBuilder() - .setCustomId(customId) - .setLabel('Skip track') - .setStyle(ButtonStyle.Secondary) - .setEmoji(this.embedOptions.icons.nextTrack) - .toJSON(); + private getTrackSourceString(currentTrack: Track): string { + const sourceStringsFormatted: Map = new Map([ + ['youtube', 'YouTube'], + ['soundcloud', 'SoundCloud'], + ['spotify', 'Spotify'], + ['apple_music', 'Apple Music'], + ['arbitrary', 'Direct source'] + ]); - const nowPlayingActionRow: APIActionRowComponent = { - type: ComponentType.ActionRow, - components: [nowPlayingButton] - }; + return sourceStringsFormatted.get(currentTrack.raw.source!) ?? 'Unavailable'; + } + private getTrackSourceIcon(currentTrack: Track): string { + const sourceIcons: Map = new Map([ + ['youtube', this.embedOptions.icons.sourceYouTube], + ['soundcloud', this.embedOptions.icons.sourceSoundCloud], + ['spotify', this.embedOptions.icons.sourceSpotify], + ['apple_music', this.embedOptions.icons.sourceAppleMusic], + ['arbitrary', this.embedOptions.icons.sourceArbitrary] + ]); + + return sourceIcons.get(currentTrack.raw.source!) ?? ''; + } + + private getDisplayTrackSource(currentTrack: Track) { + const trackSource: string = this.getTrackSourceString(currentTrack) ?? 'Unavailable'; + const trackSourceIcon: string = this.getTrackSourceIcon(currentTrack) ?? ''; + + return `**${trackSourceIcon} [${trackSource}](${currentTrack.raw.url ?? currentTrack.url})**`; + } + + private getRepeatModeString(repeatMode: number): string { const loopModesFormatted: Map = new Map([ [0, 'disabled'], [1, 'track'], @@ -115,59 +168,61 @@ class NowPlayingCommand extends BaseSlashCommandInteraction { [3, 'autoplay'] ]); - const loopModeUserString: string = loopModesFormatted.get(queue.repeatMode)!; - - const getRepeatModeMessage = (repeatMode: number): string => { - const icon = repeatMode === 3 ? this.embedOptions.icons.autoplay : this.embedOptions.icons.loop; - return `**${icon} Looping**\nLoop mode is set to **\`${loopModeUserString}\`**. You can change it with **\`/loop\`**.\n\n`; - }; - - const repeatModeString: string = queue.repeatMode === 0 ? '' : getRepeatModeMessage(queue.repeatMode); - - logger.debug('Successfully retrieved information about the current track.'); - - logger.debug('Sending info embed with action row components.'); - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setAuthor(await this.getEmbedQueueAuthor(interaction, queue)) - .setDescription( - (queue.node.isPaused() - ? '**Currently Paused**\n' - : `**${this.embedOptions.icons.audioPlaying} Now Playing**\n`) + - `**[${currentTrack.title}](${currentTrack.raw.url ?? currentTrack.url})**` + - `\nRequested by: <@${currentTrack.requestedBy?.id}>` + - `\n ${bar}\n\n` + - `${repeatModeString}` - ) - .addFields( - { - name: '**Author**', - value: author, - inline: true - }, - { - name: '**Plays**', - value: displayPlays, - inline: true - }, - { - name: '**Track source**', - value: `**${sourceIcons.get(currentTrack.raw.source!)} [${source}](${ - currentTrack.raw.url ?? currentTrack.url - })**`, - inline: true - } - ) - .setFooter({ - text: queueLength ? `${queueLength} other tracks in the queue...` : ' ' - }) - .setThumbnail(queue.currentTrack!.thumbnail) - .setColor(this.embedOptions.colors.info) - ], - components: [nowPlayingActionRow as APIActionRowComponent] - }); + return loopModesFormatted.get(repeatMode)!; } + + private getDisplayQueueRepeatMode = (queue: GuildQueue): string => { + const repeatMode: number = queue.repeatMode; + if (repeatMode === 0) { + return ''; + } + + const loopModeUserString: string = this.getRepeatModeString(repeatMode); + const icon = repeatMode === 3 ? this.embedOptions.icons.autoplay : this.embedOptions.icons.loop; + return `**${icon} Looping**\nLoop mode is set to **\`${loopModeUserString}\`**. You can change it with **\`/loop\`**.`; + }; + + private getDisplayTrackPlayingStatus = (queue: GuildQueue): string => { + return queue.node.isPaused() + ? '**Currently Paused**' + : `**${this.embedOptions.icons.audioPlaying} Now Playing**`; + }; + + private getDisplayTrackUrl = (currentTrack: Track): string => { + const trackTitle = currentTrack.title ?? 'Title unavailable'; + const trackUrl = currentTrack.url ?? currentTrack.raw.url; + if (!trackTitle || !trackUrl) { + return '**Unavailable**'; + } + + return `**[${trackTitle}](${trackUrl})**`; + }; + + private getDisplayTrackRequestedBy = (currentTrack: Track): string => { + return currentTrack.requestedBy ? `<@${currentTrack.requestedBy.id}>` : 'Unavailable'; + }; + + private getEmbedFields = (currentTrack: Track): EmbedField[] => { + const fields: EmbedField[] = [ + { + name: '**Author**', + value: this.getDisplayTrackAuthor(currentTrack), + inline: true + }, + { + name: '**Plays**', + value: this.getDisplayPlays(currentTrack), + inline: true + }, + { + name: '**Track source**', + value: this.getDisplayTrackSource(currentTrack), + inline: true + } + ]; + + return fields; + }; } export default new NowPlayingCommand();