diff --git a/src/interactions/commands/player/seek.ts b/src/interactions/commands/player/seek.ts index a9b078c..466b05e 100644 --- a/src/interactions/commands/player/seek.ts +++ b/src/interactions/commands/player/seek.ts @@ -1,9 +1,10 @@ import { GuildQueue, useQueue } from 'discord-player'; -import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; +import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { checkQueueCurrentTrack, checkQueueExists } from '../../../utils/validation/queueValidator'; import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { Logger } from 'pino'; class SeekCommand extends BaseSlashCommandInteraction { constructor() { @@ -29,128 +30,146 @@ class SeekCommand extends BaseSlashCommandInteraction { checkQueueCurrentTrack ]); - const durationInput: string = interaction.options.getString('duration')!; + const durationInputSplit: string[] = interaction.options.getString('duration')!.split(':'); + const formattedDurationString: string = this.parseDurationArray(durationInputSplit); - let durationArray: string[] = durationInput!.split(':'); - - switch (durationArray.length) { - case 1: - durationArray.unshift('00', '00'); - break; - case 2: - durationArray.unshift('00'); - break; - default: - break; - } - - durationArray = durationArray.map((value) => { - return value.padStart(2, '0'); - }); - - const durationString: string = durationArray.join(':'); - - if (durationArray.length === 0 || durationArray.length > 3) { - logger.debug('Invalid duration format input.'); - - logger.debug('Responding with warning embed.'); - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${this.embedOptions.icons.warning} Oops!**\nYou entered an invalid duration format, **\`${durationString}\`**.\n\nPlease use the format **\`HH:mm:ss\`**, **\`mm:ss\`** or **\`ss\`**, where **\`HH\`** is hours, **\`mm\`** is minutes and **\`ss\`** is seconds.\n\n**Examples:**\n` + - '- **`/seek`** **`1:24:12`** - Seek to 1 hour, 24 minutes and 12 seconds.\n' + - '- **`/seek`** **`3:27`** - Seek to 3 minutes and 27 seconds.\n' + - '- **`/seek`** **`42`** - Seek to 42 seconds.' - ) - .setColor(this.embedOptions.colors.warning) - ] - }); - } - - const validElements: boolean = durationArray.every((value) => { - return value.length === 2; - }); - - if (!validElements) { - logger.debug('Invalid duration after parsing duration input.'); - - logger.debug('Responding with warning embed.'); - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${this.embedOptions.icons.warning} Oops!**\nYou entered an invalid duration format, **\`${durationString}\`**.\n\nPlease use the format **\`HH:mm:ss\`**, **\`mm:ss\`** or **\`ss\`**, where **\`HH\`** is hours, **\`mm\`** is minutes and **\`ss\`** is seconds.\n\n**Examples:**\n` + - '- **`/seek`** **`1:24:12`** - Seek to 1 hour, 24 minutes and 12 seconds.\n' + - '- **`/seek`** **`3:27`** - Seek to 3 minutes and 27 seconds.\n' + - '- **`/seek`** **`42`** - Seek to 42 seconds.' - ) - .setColor(this.embedOptions.colors.warning) - ] - }); - } - - // Now array should only be 3 elements long, all with 2 characters, e.g. ['00', '00', '00']. - // Regex can now validate if this is a valid duration format. E.g. check if the first element is 0-23, second element is 0-59 and third element is 0-59. - const regex: RegExp = new RegExp('([0-1][0-9]|2[0-3]):?[0-5][0-9]:?[0-5][0-9]'); - const isValidDuration: boolean = regex.test(durationString); - - if (!isValidDuration) { - logger.debug('Invalid duration after regex checks.'); - - logger.debug('Responding with warning embed.'); - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${this.embedOptions.icons.warning} Oops!**\nYou entered an invalid duration format, **\`${durationString}\`**.\n\nPlease use the format **\`HH:mm:ss\`**, **\`mm:ss\`** or **\`ss\`**, where **\`HH\`** is hours, **\`mm\`** is minutes and **\`ss\`** is seconds.\n\n**Examples:**\n` + - '- **`/seek`** **`1:24:12`** - Seek to 1 hour, 24 minutes and 12 seconds.\n' + - '- **`/seek`** **`3:27`** - Seek to 3 minutes and 27 seconds.\n' + - '- **`/seek`** **`42`** - Seek to 42 seconds.' - ) - .setColor(this.embedOptions.colors.warning) - ] - }); + if (!this.validateDurationFormat(durationInputSplit, formattedDurationString)) { + return await this.handleInvalidDurationFormat(logger, interaction, formattedDurationString); } const currentTrackMaxDurationInMs: number = queue.currentTrack!.durationMS; - const durationInMilliseconds: number = - Number(durationArray[0]) * 3600000 + Number(durationArray[1]) * 60000 + Number(durationArray[2]) * 1000; + const inputDurationInMilliseconds: number = this.getDurationInputInMilliseconds(durationInputSplit); - if (durationInMilliseconds > currentTrackMaxDurationInMs - 1000) { - logger.debug('Duration specified is longer than the track duration.'); - - logger.debug('Responding with warning embed.'); - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${ - this.embedOptions.icons.warning - } Oops!**\nYou entered **\`${durationString}\`**, which is a duration that is longer than the duration for the current track.\n\nPlease try a duration that is less than the duration of the track (**\`${ - queue.currentTrack!.duration - }\`**).` - ) - .setColor(this.embedOptions.colors.warning) - ] - }); + if (inputDurationInMilliseconds > currentTrackMaxDurationInMs - 1000) { + return await this.handleDurationLongerThanTrack(logger, interaction, formattedDurationString, queue); } - queue.node.seek(durationInMilliseconds); - logger.debug(`Seeked to '${durationString}' in current track.`); + return await this.seekToDurationInCurrentTrack( + logger, + interaction, + queue, + inputDurationInMilliseconds, + formattedDurationString + ); + } - logger.debug('Responding with success embed.'); - return await interaction.editReply({ + private async handleInvalidDurationFormat( + logger: Logger, + interaction: ChatInputCommandInteraction, + formattedDurationString: string + ) { + logger.debug('Invalid duration format input.'); + + logger.debug('Responding with warning embed.'); + await interaction.editReply({ embeds: [ new EmbedBuilder() - .setAuthor(this.getEmbedUserAuthor(interaction)) .setDescription( - `**${this.embedOptions.icons.success} Seeking to duration**\nSeeking to **\`${durationString}\`** in current track.` + `**${this.embedOptions.icons.warning} Oops!**\n` + + `You entered an invalid duration format, **\`${formattedDurationString}\`**.\n` + + 'Please use the format **`HH:mm:ss`**, **`mm:ss`** or **`ss`**.\n\n' + + '**Examples:**\n' + + '- **`/seek`** **`1:24:12`** - Seek to 1 hour, 24 minutes and 12 seconds.\n' + + '- **`/seek`** **`3:27`** - Seek to 3 minutes and 27 seconds.\n' + + '- **`/seek`** **`42`** - Seek to 42 seconds.' + ) + .setColor(this.embedOptions.colors.warning) + ] + }); + return Promise.resolve(); + } + + private async seekToDurationInCurrentTrack( + logger: Logger, + interaction: ChatInputCommandInteraction, + queue: GuildQueue, + durationInMilliseconds: number, + formattedDurationString: string + ) { + queue.node.seek(durationInMilliseconds); + logger.debug(`Seeked to '${formattedDurationString}' in current track.`); + + logger.debug('Responding with success embed.'); + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor(await this.getEmbedUserAuthor(interaction)) + .setDescription( + `**${this.embedOptions.icons.success} Seeking to duration**\n` + + `Seeking to **\`${formattedDurationString}\`** in current track.` ) .setThumbnail(this.getTrackThumbnailUrl(queue.currentTrack!)) .setColor(this.embedOptions.colors.success) ] }); + return Promise.resolve(); + } + + private async handleDurationLongerThanTrack( + logger: Logger, + interaction: ChatInputCommandInteraction, + formattedDurationString: string, + queue: GuildQueue + ) { + logger.debug('Duration specified is longer than the track duration.'); + + logger.debug('Responding with warning embed.'); + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${this.embedOptions.icons.warning} Oops!**\n` + + `You entered **\`${formattedDurationString}\`**, which is a duration that is longer than the duration for the current track.\n\n` + + `Please try a duration that is less than the duration of the track (**\`${ + queue.currentTrack!.duration + }\`**).` + ) + .setColor(this.embedOptions.colors.warning) + ] + }); + return Promise.resolve(); + } + + private parseDurationArray(durationInputSplit: string[]): string { + switch (durationInputSplit.length) { + case 1: + durationInputSplit.unshift('00', '00'); + break; + case 2: + durationInputSplit.unshift('00'); + break; + default: + break; + } + + durationInputSplit = durationInputSplit.map((value) => { + return value.padStart(2, '0'); + }); + + return durationInputSplit.join(':'); + } + + private validateDurationFormat(durationInputSplit: string[], formattedDurationString: string): boolean { + if (durationInputSplit.length === 0 || durationInputSplit.length > 3) { + return false; + } + + if (!durationInputSplit.every((value) => value.length === 2)) { + return false; + } + + const regex: RegExp = new RegExp('([0-1][0-9]|2[0-3]):?[0-5][0-9]:?[0-5][0-9]'); + const isValidDuration: boolean = regex.test(formattedDurationString); + return isValidDuration; + } + + private getDurationInputInMilliseconds(durationInputSplit: string[]): number { + const durationInMilliseconds = + Number(durationInputSplit[0]) * 3600000 + + Number(durationInputSplit[1]) * 60000 + + Number(durationInputSplit[2]) * 1000; + + return durationInMilliseconds; } }