diff --git a/src/classes/interactions.ts b/src/classes/interactions.ts index 6c91908..c1c23a0 100644 --- a/src/classes/interactions.ts +++ b/src/classes/interactions.ts @@ -1,5 +1,5 @@ import config from 'config'; -import { GuildQueue } from 'discord-player'; +import { GuildQueue, Track } from 'discord-player'; import { ApplicationCommandOptionChoiceData, AutocompleteInteraction, @@ -27,6 +27,14 @@ import { import { Validator, ValidatorParams } from '../types/utilTypes'; abstract class BaseInteraction { + embedOptions: EmbedOptions; + botOptions: BotOptions; + + constructor() { + this.embedOptions = config.get('embedOptions'); + this.botOptions = config.get('botOptions'); + } + protected getLoggerBase( module: string, name: string, @@ -50,19 +58,41 @@ abstract class BaseInteraction { } } + protected getFormattedDuration(track: Track): string { + let durationFormat = + Number(track.raw.duration) === 0 || track.duration === '0:00' ? '' : `**\`${track.duration}\`**`; + + if (track.raw.live) { + durationFormat = `**${this.embedOptions.icons.liveTrack} \`LIVE\`**`; + } + + return durationFormat; + } + + protected getFormattedTrackUrl(track: Track): string { + const trackTitle = track.title ?? 'Title unavailable'; + const trackUrl = track.url ?? track.raw.url; + if (!trackTitle || !trackUrl) { + return '**Unavailable**'; + } + return `**[${trackTitle}](${trackUrl})**`; + } + + protected getDisplayTrackDurationAndUrl(track: Track): string { + const formattedDuration = this.getFormattedDuration(track); + const formattedUrl = this.getFormattedTrackUrl(track); + + return `${formattedDuration} ${formattedUrl}`; + } + abstract execute( params: BaseInteractionParams ): Promise | ApplicationCommandOptionChoiceData | void>; } abstract class BaseInteractionWithEmbedResponse extends BaseInteraction { - embedOptions: EmbedOptions; - botOptions: BotOptions; - constructor() { super(); - this.embedOptions = config.get('embedOptions'); - this.botOptions = config.get('botOptions'); } protected async getEmbedUserAuthor( @@ -121,14 +151,10 @@ export abstract class BaseSlashCommandInteraction extends BaseInteractionWithEmb } export abstract class BaseComponentInteraction extends BaseInteractionWithEmbedResponse { - embedOptions: EmbedOptions; - botOptions: BotOptions; name: string; constructor(name: string) { super(); - this.embedOptions = config.get('embedOptions'); - this.botOptions = config.get('botOptions'); this.name = name; } diff --git a/src/interactions/commands/player/nowplaying.ts b/src/interactions/commands/player/nowplaying.ts index 40fb354..3a63e6f 100644 --- a/src/interactions/commands/player/nowplaying.ts +++ b/src/interactions/commands/player/nowplaying.ts @@ -42,7 +42,7 @@ class NowPlayingCommand extends BaseSlashCommandInteraction { const tracksInQueueCount: number = queue.tracks.data.length; const currentTrack: Track = queue.currentTrack!; - const displayTrackUrl: string = this.getDisplayTrackUrl(currentTrack); + const displayTrackUrl: string = this.getFormattedTrackUrl(currentTrack); const displayTrackRequestedBy: string = this.getDisplayTrackRequestedBy(currentTrack); const displayTrackPlayingStatus: string = this.getDisplayTrackPlayingStatus(queue); const displayQueueRepeatMode: string = this.getDisplayQueueRepeatMode(queue); @@ -188,16 +188,6 @@ class NowPlayingCommand extends BaseSlashCommandInteraction { : `**${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'; }; diff --git a/src/interactions/commands/player/pause.ts b/src/interactions/commands/player/pause.ts index ff60b9c..b473791 100644 --- a/src/interactions/commands/player/pause.ts +++ b/src/interactions/commands/player/pause.ts @@ -1,5 +1,6 @@ import { GuildQueue, Track, useQueue } from 'discord-player'; import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; +import { Logger } from 'pino'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { checkQueueCurrentTrack, checkQueueExists } from '../../../utils/validation/queueValidator'; @@ -26,18 +27,7 @@ class PauseCommand extends BaseSlashCommandInteraction { const currentTrack: Track = queue.currentTrack!; - let durationFormat = - Number(currentTrack.raw.duration) === 0 || currentTrack.duration === '0:00' - ? '' - : `\`${currentTrack.duration}\``; - - if (currentTrack.raw.live) { - durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``; - } - - // change paused state to opposite of current state - queue.node.setPaused(!queue.node.isPaused()); - logger.debug(`Set paused state to ${queue.node.isPaused()}.`); + this.togglePauseState(queue, logger); logger.debug('Responding with success embed.'); return await interaction.editReply({ @@ -47,13 +37,18 @@ class PauseCommand extends BaseSlashCommandInteraction { .setDescription( `**${this.embedOptions.icons.pauseResumed} ${ queue.node.isPaused() ? 'Paused Track' : 'Resumed track' - }**\n**${durationFormat} [${currentTrack.title}](${currentTrack.raw.url ?? currentTrack.url})**` + }**\n ${this.getDisplayTrackDurationAndUrl(currentTrack)}` ) .setThumbnail(currentTrack.thumbnail) .setColor(this.embedOptions.colors.success) ] }); } + + private togglePauseState(queue: GuildQueue, logger: Logger): void { + queue.node.setPaused(!queue.node.isPaused()); + logger.debug(`Set paused state to ${queue.node.isPaused()}.`); + } } export default new PauseCommand(); diff --git a/src/interactions/commands/player/play.ts b/src/interactions/commands/player/play.ts index 62eb9ce..5b250b6 100644 --- a/src/interactions/commands/player/play.ts +++ b/src/interactions/commands/player/play.ts @@ -226,13 +226,6 @@ class PlayCommand extends BaseSlashCommandInteraction { track.thumbnail = this.embedOptions.info.fallbackThumbnailUrl; } - let durationFormat = - Number(track.raw.duration) === 0 || track.duration === '0:00' ? '' : `\`${track.duration}\``; - - if (track.raw.live) { - durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``; - } - if (searchResult.playlist && searchResult.tracks.length > 1) { logger.debug(`Playlist found and added with player.play(). Query: '${query}'`); @@ -242,9 +235,9 @@ class PlayCommand extends BaseSlashCommandInteraction { new EmbedBuilder() .setAuthor(await this.getEmbedUserAuthor(interaction)) .setDescription( - `**${this.embedOptions.icons.success} Added playlist to queue**\n**${durationFormat} [${ - track.title - }](${track.raw.url ?? track.url})**\n\nAnd **${ + `**${ + this.embedOptions.icons.success + } Added playlist to queue**\n${this.getDisplayTrackDurationAndUrl(track)}\n\nAnd **${ searchResult.tracks.length - 1 }** more tracks... **\`/queue\`** to view all.` ) @@ -263,9 +256,9 @@ class PlayCommand extends BaseSlashCommandInteraction { new EmbedBuilder() .setAuthor(await this.getEmbedUserAuthor(interaction)) .setDescription( - `**${this.embedOptions.icons.audioStartedPlaying} Started playing**\n**${durationFormat} [${ - track.title - }](${track.raw.url ?? track.url})**` + `**${ + this.embedOptions.icons.audioStartedPlaying + } Started playing**\n${this.getDisplayTrackDurationAndUrl(track)}` ) .setThumbnail(track.thumbnail) .setColor(this.embedOptions.colors.success) @@ -281,9 +274,9 @@ class PlayCommand extends BaseSlashCommandInteraction { new EmbedBuilder() .setAuthor(await this.getEmbedUserAuthor(interaction)) .setDescription( - `${this.embedOptions.icons.success} **Added to queue**\n**${durationFormat} [${track.title}](${ - track.raw.url ?? track.url - })**` + `${this.embedOptions.icons.success} **Added to queue**\n${this.getDisplayTrackDurationAndUrl( + track + )}` ) .setThumbnail(track.thumbnail) .setColor(this.embedOptions.colors.success) diff --git a/src/interactions/commands/player/queue.ts b/src/interactions/commands/player/queue.ts index 82b3568..429abb9 100644 --- a/src/interactions/commands/player/queue.ts +++ b/src/interactions/commands/player/queue.ts @@ -62,16 +62,7 @@ class QueueCommand extends BaseSlashCommandInteraction { queueString = queue.tracks.data .slice(pageIndex * 10, pageIndex * 10 + 10) .map((track, index) => { - let durationFormat = - Number(track.raw.duration) === 0 || track.duration === '0:00' ? '' : `\`${track.duration}\``; - - if (track.raw.live) { - durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``; - } - - return `**${pageIndex * 10 + index + 1}.** **${durationFormat} [${track.title}](${ - track.raw.url ?? track.url - })**`; + return `**${pageIndex * 10 + index + 1}.** ${this.getDisplayTrackDurationAndUrl(track)}`; }) .join('\n'); } diff --git a/src/interactions/commands/player/remove.ts b/src/interactions/commands/player/remove.ts index 92d87d7..1cc3080 100644 --- a/src/interactions/commands/player/remove.ts +++ b/src/interactions/commands/player/remove.ts @@ -52,14 +52,6 @@ class RemoveCommand extends BaseSlashCommandInteraction { // Remove specified track number from queue const removedTrack: Track = queue.node.remove(removeTrackNumber - 1)!; logger.debug(`Removed track '${removedTrack.url}' from queue.`); - let durationFormat = - Number(removedTrack.raw.duration) === 0 || removedTrack.duration === '0:00' - ? '' - : `\`${removedTrack.duration}\``; - - if (removedTrack.raw.live) { - durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``; - } logger.debug('Responding with success embed.'); return await interaction.editReply({ @@ -67,9 +59,9 @@ class RemoveCommand extends BaseSlashCommandInteraction { new EmbedBuilder() .setAuthor(await this.getEmbedUserAuthor(interaction)) .setDescription( - `**${this.embedOptions.icons.success} Removed track**\n**${durationFormat} [${ - removedTrack.title - }](${removedTrack.raw.url ?? removedTrack.url})**` + `**${this.embedOptions.icons.success} Removed track**\n${this.getDisplayTrackDurationAndUrl( + removedTrack + )}` ) .setThumbnail(removedTrack.thumbnail) .setColor(this.embedOptions.colors.success) diff --git a/src/interactions/commands/player/skip.ts b/src/interactions/commands/player/skip.ts index 911324e..baf3eb3 100644 --- a/src/interactions/commands/player/skip.ts +++ b/src/interactions/commands/player/skip.ts @@ -49,15 +49,6 @@ class SkipCommand extends BaseSlashCommandInteraction { const skippedTrack: Track = queue.currentTrack!; logger.debug('Responding with warning embed.'); - let durationFormat = - Number(skippedTrack.raw.duration) === 0 || skippedTrack.duration === '0:00' - ? '' - : `\`${skippedTrack.duration}\``; - - if (skippedTrack.raw.live) { - durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``; - } - queue.node.skipTo(skipToTrack - 1); logger.debug('Skipped to specified track number.'); @@ -67,9 +58,9 @@ class SkipCommand extends BaseSlashCommandInteraction { new EmbedBuilder() .setAuthor(await this.getEmbedUserAuthor(interaction)) .setDescription( - `**${this.embedOptions.icons.skipped} Skipped track**\n**${durationFormat} [${ - skippedTrack.title - }](${skippedTrack.raw.url ?? skippedTrack.url})**` + `**${ + this.embedOptions.icons.skipped + } Skipped track**\n${this.getDisplayTrackDurationAndUrl(skippedTrack)}` ) .setThumbnail(skippedTrack.thumbnail) .setColor(this.embedOptions.colors.success) @@ -94,14 +85,6 @@ class SkipCommand extends BaseSlashCommandInteraction { const skippedTrack: Track = queue.currentTrack!; - let durationFormat = - Number(skippedTrack.raw.duration) === 0 || skippedTrack.duration === '0:00' - ? '' - : `\`${skippedTrack.duration}\``; - - if (skippedTrack.raw.live) { - durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``; - } queue.node.skip(); logger.debug('Skipped current track.'); @@ -127,9 +110,9 @@ class SkipCommand extends BaseSlashCommandInteraction { new EmbedBuilder() .setAuthor(await this.getEmbedUserAuthor(interaction)) .setDescription( - `**${this.embedOptions.icons.skipped} Skipped track**\n**${durationFormat} [${ - skippedTrack.title - }](${skippedTrack.raw.url ?? skippedTrack.url})**` + `\n\n${repeatModeString}` + `**${this.embedOptions.icons.skipped} Skipped track**\n${this.getDisplayTrackDurationAndUrl( + skippedTrack + )}` + `\n\n${repeatModeString}` ) .setThumbnail(skippedTrack.thumbnail) .setColor(this.embedOptions.colors.success)