diff --git a/src/classes/interactions.ts b/src/classes/interactions.ts index 1bf8f45..1490522 100644 --- a/src/classes/interactions.ts +++ b/src/classes/interactions.ts @@ -3,6 +3,8 @@ import { ApplicationCommandOptionChoiceData, AutocompleteInteraction, ChatInputCommandInteraction, + EmbedAuthorOptions, + GuildMember, Interaction, Message, MessageComponentInteraction, @@ -21,7 +23,7 @@ import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../types/interactionTypes'; -import { ValidatorParams } from '../types/utilTypes'; +import { Validator, ValidatorParams } from '../types/utilTypes'; abstract class BaseInteraction { protected getLoggerBase( @@ -39,13 +41,11 @@ abstract class BaseInteraction { }); } - protected validators: ((args: ValidatorParams) => Promise)[] = []; + protected validators: Validator[] = []; - protected async runValidators(args: ValidatorParams): Promise { - for (const validator of this.validators) { - if (await validator(args)) { - return Promise.reject(); - } + protected async runValidators(args: ValidatorParams, validators?: Validator[]): Promise { + for (const validator of validators ? validators : this.validators) { + await validator(args); } } @@ -54,13 +54,38 @@ abstract class BaseInteraction { ): Promise | ApplicationCommandOptionChoiceData | void>; } -export abstract class BaseSlashCommandInteraction extends BaseInteraction { +abstract class BaseInteractionWithEmbedResponse extends BaseInteraction { + embedOptions: EmbedOptions; + botOptions: BotOptions; + + constructor() { + super(); + this.embedOptions = config.get('embedOptions'); + this.botOptions = config.get('botOptions'); + } + + protected async getEmbedAuthor( + interaction: MessageComponentInteraction | ChatInputCommandInteraction + ): Promise { + let authorName: string = ''; + if (interaction.member instanceof GuildMember) { + authorName = interaction.member.nickname || interaction.user.username; + } else { + authorName = interaction.user.username; + } + + return { + name: authorName, + iconURL: interaction.user.avatarURL() || this.embedOptions.info.fallbackIconUrl + }; + } +} + +export abstract class BaseSlashCommandInteraction extends BaseInteractionWithEmbedResponse { data: Omit | SlashCommandSubcommandsOnlyBuilder; isSystemCommand: boolean; isNew: boolean; isBeta: boolean; - embedOptions: EmbedOptions; - botOptions: BotOptions; name: string; constructor( @@ -74,8 +99,6 @@ export abstract class BaseSlashCommandInteraction extends BaseInteraction { this.isSystemCommand = isSystemCommand; this.isNew = isNew; this.isBeta = isBeta; - this.embedOptions = config.get('embedOptions'); - this.botOptions = config.get('botOptions'); this.name = data.name; } @@ -86,22 +109,7 @@ export abstract class BaseSlashCommandInteraction extends BaseInteraction { abstract execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType; } -export abstract class BaseAutocompleteInteraction extends BaseInteraction { - name: string; - - constructor(name: string) { - super(); - this.name = name; - } - - protected getLogger(name: string, executionId: string, interaction: AutocompleteInteraction): Logger { - return super.getLoggerBase('autocompleteInteraction', name, executionId, interaction); - } - - abstract execute(params: BaseAutocompleteParams): BaseAutocompleteReturnType; -} - -export abstract class BaseComponentInteraction extends BaseInteraction { +export abstract class BaseComponentInteraction extends BaseInteractionWithEmbedResponse { embedOptions: EmbedOptions; botOptions: BotOptions; name: string; @@ -120,7 +128,29 @@ export abstract class BaseComponentInteraction extends BaseInteraction { abstract execute(params: BaseComponentParams): BaseComponentReturnType; } +export abstract class BaseAutocompleteInteraction extends BaseInteraction { + name: string; + + constructor(name: string) { + super(); + this.name = name; + } + + protected getLogger(name: string, executionId: string, interaction: AutocompleteInteraction): Logger { + return super.getLoggerBase('autocompleteInteraction', name, executionId, interaction); + } + + abstract execute(params: BaseAutocompleteParams): BaseAutocompleteReturnType; +} + export class CustomError extends Error { type?: string; code?: string; } + +export class InteractionValidationError extends Error { + constructor(message?: string) { + super(message); + this.name = 'InteractionValidationError'; + } +} diff --git a/src/handlers/interactionCommandHandler.ts b/src/handlers/interactionCommandHandler.ts index 57eb6bf..06f88a0 100644 --- a/src/handlers/interactionCommandHandler.ts +++ b/src/handlers/interactionCommandHandler.ts @@ -3,7 +3,7 @@ import { Logger } from 'pino'; import loggerModule from '../services/logger'; import { ExtendedClient } from '../types/clientTypes'; import { BaseSlashCommandInteraction } from '../classes/interactions'; -import { cannotSendMessageInChannel } from '../utils/validation/permissionValidator'; +import { checkChannelPermissionViewable } from '../utils/validation/permissionValidator'; export const handleCommand = async ( interaction: ChatInputCommandInteraction, @@ -20,9 +20,7 @@ export const handleCommand = async ( await interaction.deferReply(); logger.debug('Interaction deferred.'); - if (await cannotSendMessageInChannel({ interaction, executionId })) { - return; - } + await checkChannelPermissionViewable({ interaction, executionId }); const slashCommand: BaseSlashCommandInteraction = client.slashCommandInteractions!.get( interactionIdentifier diff --git a/src/handlers/interactionComponentHandler.ts b/src/handlers/interactionComponentHandler.ts index 142f690..f95d1dc 100644 --- a/src/handlers/interactionComponentHandler.ts +++ b/src/handlers/interactionComponentHandler.ts @@ -3,7 +3,7 @@ import { Logger } from 'pino'; import loggerModule from '../services/logger'; import { ExtendedClient } from '../types/clientTypes'; import { BaseComponentInteraction } from '../classes/interactions'; -import { cannotSendMessageInChannel } from '../utils/validation/permissionValidator'; +import { checkChannelPermissionViewable } from '../utils/validation/permissionValidator'; export const handleComponent = async ( interaction: MessageComponentInteraction, @@ -25,9 +25,7 @@ export const handleComponent = async ( logger.debug(`Parsed componentId '${componentId}' from identifier '${interactionIdentifier}'.`); - if (await cannotSendMessageInChannel({ interaction, executionId })) { - return; - } + await checkChannelPermissionViewable({ interaction, executionId }); const component: BaseComponentInteraction = client.componentInteractions!.get( componentId diff --git a/src/handlers/interactionErrorHandler.ts b/src/handlers/interactionErrorHandler.ts index d5199ca..e718981 100644 --- a/src/handlers/interactionErrorHandler.ts +++ b/src/handlers/interactionErrorHandler.ts @@ -10,7 +10,7 @@ import { import { Logger } from 'pino'; import loggerModule from '../services/logger'; import { BotOptions, EmbedOptions } from '../types/configTypes'; -import { CustomError } from '../classes/interactions'; +import { CustomError, InteractionValidationError } from '../classes/interactions'; const embedOptions: EmbedOptions = config.get('embedOptions'); const botOptions: BotOptions = config.get('botOptions'); @@ -28,6 +28,13 @@ export const handleError = async ( executionId: executionId }); + if (error instanceof InteractionValidationError) { + logger.debug( + `Interaction validation error '${error.message}' while handling interaction '${interactionIdentifier}'.` + ); + return; + } + // TODO: Extract replies for embeds const errorReply: InteractionReplyOptions = { embeds: [ diff --git a/src/interactions/commands/player/filters.ts b/src/interactions/commands/player/filters.ts index 63899b2..8f7d81e 100644 --- a/src/interactions/commands/player/filters.ts +++ b/src/interactions/commands/player/filters.ts @@ -18,8 +18,8 @@ import { Logger } from 'pino'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; import { FFmpegFilterOption, FFmpegFilterOptions } from '../../../types/configTypes'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists, checkQueueCurrentTrack } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions'); @@ -34,19 +34,19 @@ class FiltersCommand extends BaseSlashCommandInteraction { .setDescription('Audio filtering type to use.') .setRequired(false) .addChoices( - { name: 'FFmpeg', value: 'ffmpeg' }, - { name: 'Biquad', value: 'biquad' }, - { name: 'Equalizer', value: 'equalizer' }, - { name: 'Disable', value: 'disable' } + { name: 'FFmpeg', value: 'FFmpeg' }, + { name: 'Biquad', value: 'Biquad' }, + { name: 'Equalizer', value: 'Equalizer' }, + { name: 'Disable', value: 'Disable' } ) ); super(data); this.validators = [ - (args) => notInVoiceChannel(args), - (args) => notInSameVoiceChannel(args), - (args) => queueDoesNotExist(args), - (args) => queueNoCurrentTrack(args) + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args), + (args) => checkQueueCurrentTrack(args) ]; } @@ -56,11 +56,9 @@ class FiltersCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - await this.runValidators({ interaction, queue, executionId }).catch(() => { - return; - }); + await this.runValidators({ interaction, queue, executionId }); - const filterProvider: string = interaction.options.getString('type') || 'ffmpeg'; + const filterProvider: string = interaction.options.getString('type') || 'FFmpeg'; switch (filterProvider) { case 'ffmpeg': diff --git a/src/interactions/commands/player/leave.ts b/src/interactions/commands/player/leave.ts index 983ded7..1c6e07e 100644 --- a/src/interactions/commands/player/leave.ts +++ b/src/interactions/commands/player/leave.ts @@ -1,9 +1,9 @@ import { GuildQueue, useQueue } from 'discord-player'; -import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; +import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; +import { checkQueueExists } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class LeaveCommand extends BaseSlashCommandInteraction { constructor() { @@ -11,6 +11,12 @@ class LeaveCommand extends BaseSlashCommandInteraction { .setName('leave') .setDescription('Clear the queue and remove bot from voice channel.'); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -19,39 +25,18 @@ class LeaveCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }) - ]; + await this.runValidators({ interaction, queue, executionId }); - for (const validator of validators) { - if (await validator()) { - return; - } - } + logger.info('Validators passed, clearing queue.'); - if (!queue.deleted) { - queue.delete(); - logger.debug('Deleted the queue.'); - } - - let authorName: string; - - if (interaction.member instanceof GuildMember) { - authorName = interaction.member.nickname || interaction.user.username; - } else { - authorName = interaction.user.username; - } + logger.debug('Deleting queue.'); + await this.deleteQueue(queue); logger.debug('Responding with success embed.'); return await interaction.editReply({ embeds: [ new EmbedBuilder() - .setAuthor({ - name: authorName, - iconURL: interaction.user.avatarURL() || this.embedOptions.info.fallbackIconUrl - }) + .setAuthor(await this.getEmbedAuthor(interaction)) .setDescription( `**${this.embedOptions.icons.success} Leaving channel**\nCleared the track queue and left voice channel.\n\nTo play more music, use the **\`/play\`** command!` ) @@ -59,6 +44,12 @@ class LeaveCommand extends BaseSlashCommandInteraction { ] }); } + + protected async deleteQueue(queue: GuildQueue): Promise { + if (!queue.deleted) { + queue.delete(); + } + } } export default new LeaveCommand(); diff --git a/src/interactions/commands/player/loop.ts b/src/interactions/commands/player/loop.ts index ed1096a..ed73695 100644 --- a/src/interactions/commands/player/loop.ts +++ b/src/interactions/commands/player/loop.ts @@ -2,8 +2,8 @@ import { GuildQueue, QueueRepeatMode, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder, SlashCommandStringOption } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class LoopCommand extends BaseSlashCommandInteraction { constructor() { @@ -23,6 +23,12 @@ class LoopCommand extends BaseSlashCommandInteraction { ) ); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -31,17 +37,7 @@ class LoopCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); // TODO: create type for loop modes formatted const loopModesFormatted: Map = new Map([ diff --git a/src/interactions/commands/player/lyrics.ts b/src/interactions/commands/player/lyrics.ts index 793ebb3..61d3aa3 100644 --- a/src/interactions/commands/player/lyrics.ts +++ b/src/interactions/commands/player/lyrics.ts @@ -3,8 +3,8 @@ import { GuildQueue, Player, QueryType, useMainPlayer, useQueue } from 'discord- import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists, checkQueueCurrentTrack } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class LyricsCommand extends BaseSlashCommandInteraction { constructor() { @@ -21,29 +21,26 @@ class LyricsCommand extends BaseSlashCommandInteraction { .setAutocomplete(true) ); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args), + (args) => checkQueueCurrentTrack(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { const { executionId, interaction } = params; const logger = this.getLogger(this.name, executionId, interaction); - const query: string = interaction.options.getString('query')!; const queue: GuildQueue = useQueue(interaction.guild!.id)!; + + const query: string = interaction.options.getString('query')!; let geniusSearchQuery: string = ''; if (!query) { - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }), - () => queueNoCurrentTrack({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); geniusSearchQuery = queue.currentTrack!.title.slice(0, 50); diff --git a/src/interactions/commands/player/nowplaying.ts b/src/interactions/commands/player/nowplaying.ts index 7db0007..9e806fc 100644 --- a/src/interactions/commands/player/nowplaying.ts +++ b/src/interactions/commands/player/nowplaying.ts @@ -13,8 +13,8 @@ import { import { PlayerOptions } from '../../../types/configTypes'; import { BaseSlashCommandParams, BaseSlashCommandReturnType, TrackMetadata } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists, checkQueueCurrentTrack } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const playerOptions: PlayerOptions = config.get('playerOptions'); @@ -24,6 +24,13 @@ class NowPlayingCommand extends BaseSlashCommandInteraction { .setName('nowplaying') .setDescription('Show information about the current track.'); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args), + (args) => checkQueueCurrentTrack(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -32,19 +39,7 @@ class NowPlayingCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - // TODO: define TS type for validators - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }), - () => queueNoCurrentTrack({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); const sourceStringsFormatted: Map = new Map([ ['youtube', 'YouTube'], diff --git a/src/interactions/commands/player/pause.ts b/src/interactions/commands/player/pause.ts index fa04939..f0bef15 100644 --- a/src/interactions/commands/player/pause.ts +++ b/src/interactions/commands/player/pause.ts @@ -3,13 +3,20 @@ import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists, checkQueueCurrentTrack } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class PauseCommand extends BaseSlashCommandInteraction { constructor() { const data = new SlashCommandBuilder().setName('pause').setDescription('Toggle pause for the current track.'); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args), + (args) => checkQueueCurrentTrack(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -18,18 +25,7 @@ class PauseCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }), - () => queueNoCurrentTrack({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); const currentTrack: Track = queue.currentTrack!; diff --git a/src/interactions/commands/player/play.ts b/src/interactions/commands/player/play.ts index b2d1850..9c347fa 100644 --- a/src/interactions/commands/player/play.ts +++ b/src/interactions/commands/player/play.ts @@ -4,9 +4,9 @@ import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandInteraction, CustomError } from '../../../classes/interactions'; import { PlayerOptions } from '../../../types/configTypes'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; -import { cannotJoinVoiceOrTalk } from '../../../utils/validation/permissionValidator'; +import { checkVoicePermissionJoinAndTalk } from '../../../utils/validation/permissionValidator'; import { transformQuery } from '../../../utils/validation/searchQueryValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const playerOptions: PlayerOptions = config.get('playerOptions'); @@ -25,26 +25,21 @@ class PlayCommand extends BaseSlashCommandInteraction { .setAutocomplete(true) ); super(data); + + this.validators = [(args) => checkInVoiceChannel(args)]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { const { executionId, interaction } = params; const logger = this.getLogger(this.name, executionId, interaction); + await this.runValidators({ interaction, executionId }); + let queue: GuildQueue = useQueue(interaction.guild!.id)!; - if (queue && (await notInSameVoiceChannel({ interaction, queue, executionId }))) { - return; - } - - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => cannotJoinVoiceOrTalk({ interaction, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } + if (queue) { + await this.runValidators({ interaction, queue, executionId }, [checkSameVoiceChannel]); + } else { + await this.runValidators({ interaction, executionId }, [checkVoicePermissionJoinAndTalk]); } const player = useMainPlayer()!; diff --git a/src/interactions/commands/player/queue.ts b/src/interactions/commands/player/queue.ts index ced622e..33eeee7 100644 --- a/src/interactions/commands/player/queue.ts +++ b/src/interactions/commands/player/queue.ts @@ -4,8 +4,8 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { PlayerOptions } from '../../../types/configTypes'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const playerOptions: PlayerOptions = config.get('playerOptions'); @@ -18,6 +18,12 @@ class QueueCommand extends BaseSlashCommandInteraction { option.setName('page').setDescription('Page number to display for the queue').setMinValue(1) ); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -26,17 +32,7 @@ class QueueCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); const pageIndex: number = (interaction.options.getNumber('page') || 1) - 1; let queueString: string = ''; diff --git a/src/interactions/commands/player/remove.ts b/src/interactions/commands/player/remove.ts index 8547853..d3b361f 100644 --- a/src/interactions/commands/player/remove.ts +++ b/src/interactions/commands/player/remove.ts @@ -2,8 +2,8 @@ import { GuildQueue, Track, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class RemoveCommand extends BaseSlashCommandInteraction { constructor() { @@ -18,6 +18,12 @@ class RemoveCommand extends BaseSlashCommandInteraction { .setRequired(true) ); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -26,17 +32,7 @@ class RemoveCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); const removeTrackNumber: number = interaction.options.getNumber('tracknumber')!; diff --git a/src/interactions/commands/player/seek.ts b/src/interactions/commands/player/seek.ts index 9de0ef9..8d8853e 100644 --- a/src/interactions/commands/player/seek.ts +++ b/src/interactions/commands/player/seek.ts @@ -2,8 +2,8 @@ import { GuildQueue, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists, checkQueueCurrentTrack } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class SeekCommand extends BaseSlashCommandInteraction { constructor() { @@ -14,6 +14,13 @@ class SeekCommand extends BaseSlashCommandInteraction { option.setName('duration').setDescription('Duration in format 00:00:00 (HH:mm:ss).').setRequired(true) ); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args), + (args) => checkQueueCurrentTrack(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -22,18 +29,7 @@ class SeekCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }), - () => queueNoCurrentTrack({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); const durationInput: string = interaction.options.getString('duration')!; diff --git a/src/interactions/commands/player/shuffle.ts b/src/interactions/commands/player/shuffle.ts index 50c69ca..46537da 100644 --- a/src/interactions/commands/player/shuffle.ts +++ b/src/interactions/commands/player/shuffle.ts @@ -2,8 +2,8 @@ import { GuildQueue, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist, queueIsEmpty } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists, checkQueueEmpty } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class ShuffleCommand extends BaseSlashCommandInteraction { constructor() { @@ -11,6 +11,13 @@ class ShuffleCommand extends BaseSlashCommandInteraction { .setName('shuffle') .setDescription('Randomly shuffle all tracks in the queue.'); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args), + (args) => checkQueueEmpty(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -19,18 +26,7 @@ class ShuffleCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }), - () => queueIsEmpty({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); queue.tracks.shuffle(); logger.debug('Shuffled queue tracks.'); diff --git a/src/interactions/commands/player/skip.ts b/src/interactions/commands/player/skip.ts index c5c8b43..3328682 100644 --- a/src/interactions/commands/player/skip.ts +++ b/src/interactions/commands/player/skip.ts @@ -2,8 +2,8 @@ import { GuildQueue, Track, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists, checkQueueCurrentTrack } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class SkipCommand extends BaseSlashCommandInteraction { constructor() { @@ -14,6 +14,13 @@ class SkipCommand extends BaseSlashCommandInteraction { option.setName('tracknumber').setDescription('The position in queue to skip to.').setMinValue(1) ); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args), + (args) => checkQueueCurrentTrack(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -22,18 +29,7 @@ class SkipCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }), - () => queueNoCurrentTrack({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); const skipToTrack: number = interaction.options.getNumber('tracknumber')!; diff --git a/src/interactions/commands/player/stop.ts b/src/interactions/commands/player/stop.ts index f2c55e1..bba3690 100644 --- a/src/interactions/commands/player/stop.ts +++ b/src/interactions/commands/player/stop.ts @@ -2,8 +2,8 @@ import { GuildQueue, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class StopCommand extends BaseSlashCommandInteraction { constructor() { @@ -11,6 +11,12 @@ class StopCommand extends BaseSlashCommandInteraction { .setName('stop') .setDescription('Clear the queue and stop playing audio.'); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -19,17 +25,7 @@ class StopCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); if (!queue.deleted) { queue.setRepeatMode(0); diff --git a/src/interactions/commands/player/volume.ts b/src/interactions/commands/player/volume.ts index ea954b9..763a425 100644 --- a/src/interactions/commands/player/volume.ts +++ b/src/interactions/commands/player/volume.ts @@ -2,8 +2,8 @@ import { GuildQueue, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; +import { checkQueueExists } from '../../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; class VolumeCommand extends BaseSlashCommandInteraction { constructor() { @@ -18,6 +18,12 @@ class VolumeCommand extends BaseSlashCommandInteraction { .setMaxValue(100) ); super(data); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -26,17 +32,7 @@ class VolumeCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); const volume: number = interaction.options.getNumber('percentage')!; diff --git a/src/interactions/commands/system/guilds.ts b/src/interactions/commands/system/guilds.ts index 02563b8..d38f5ee 100644 --- a/src/interactions/commands/system/guilds.ts +++ b/src/interactions/commands/system/guilds.ts @@ -1,7 +1,7 @@ import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { notValidGuildId } from '../../../utils/validation/systemCommandValidator'; +import { checkValidGuildId } from '../../../utils/validation/systemCommandValidator'; class GuildsCommand extends BaseSlashCommandInteraction { constructor() { @@ -10,15 +10,15 @@ class GuildsCommand extends BaseSlashCommandInteraction { .setDescription('Show the top 25 guilds by member count.'); const isSystemCommand: boolean = true; super(data, isSystemCommand); + + this.validators = [(args) => checkValidGuildId(args)]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { const { executionId, interaction, client } = params; const logger = this.getLogger(this.name, executionId, interaction); - if (await notValidGuildId({ interaction, executionId })) { - return; - } + await this.runValidators({ interaction, executionId }); let shardGuilds: Guild[] = []; let totalGuildCount: number = 0; diff --git a/src/interactions/commands/system/reload.ts b/src/interactions/commands/system/reload.ts index 155cc23..f863ea7 100644 --- a/src/interactions/commands/system/reload.ts +++ b/src/interactions/commands/system/reload.ts @@ -2,7 +2,7 @@ import { ApplicationCommandOption, ApplicationCommandOptionData, EmbedBuilder, S import { ExtendedClient } from '../../../types/clientTypes'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { notValidGuildId } from '../../../utils/validation/systemCommandValidator'; +import { checkValidGuildId } from '../../../utils/validation/systemCommandValidator'; class ReloadCommand extends BaseSlashCommandInteraction { constructor() { @@ -11,15 +11,15 @@ class ReloadCommand extends BaseSlashCommandInteraction { .setDescription('Reload slash command, autocomplete and component interactions across shards.'); const isSystemCommand: boolean = true; super(data, isSystemCommand); + + this.validators = [(args) => checkValidGuildId(args)]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { const { executionId, interaction, client } = params; const logger = this.getLogger(this.name, executionId, interaction); - if (await notValidGuildId({ interaction, executionId })) { - return; - } + await this.runValidators({ interaction, executionId }); try { logger.debug('Reloading interaction module across all shards.'); diff --git a/src/interactions/commands/system/shards.ts b/src/interactions/commands/system/shards.ts index 72df71d..8f34cab 100644 --- a/src/interactions/commands/system/shards.ts +++ b/src/interactions/commands/system/shards.ts @@ -2,7 +2,7 @@ import { EmbedBuilder, EmbedField, SlashCommandBuilder } from 'discord.js'; import { ExtendedClient } from '../../../types/clientTypes'; import { BaseSlashCommandParams, BaseSlashCommandReturnType, ShardInfo } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { notValidGuildId } from '../../../utils/validation/systemCommandValidator'; +import { checkValidGuildId } from '../../../utils/validation/systemCommandValidator'; class ShardsCommand extends BaseSlashCommandInteraction { constructor() { @@ -29,15 +29,15 @@ class ShardsCommand extends BaseSlashCommandInteraction { ); const isSystemCommand: boolean = true; super(data, isSystemCommand); + + this.validators = [(args) => checkValidGuildId(args)]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { const { executionId, interaction, client } = params; const logger = this.getLogger(this.name, executionId, interaction); - if (await notValidGuildId({ interaction, executionId })) { - return; - } + await this.runValidators({ interaction, executionId }); let shardInfoList: ShardInfo[] = []; diff --git a/src/interactions/commands/system/systemstatus.ts b/src/interactions/commands/system/systemstatus.ts index b5cacb5..11270a1 100644 --- a/src/interactions/commands/system/systemstatus.ts +++ b/src/interactions/commands/system/systemstatus.ts @@ -5,7 +5,7 @@ import { dependencies, version } from '../../../../package.json'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; import { getUptimeFormatted } from '../../../utils/system/getUptimeFormatted'; -import { notValidGuildId } from '../../../utils/validation/systemCommandValidator'; +import { checkValidGuildId } from '../../../utils/validation/systemCommandValidator'; class SystemStatusCommand extends BaseSlashCommandInteraction { constructor() { @@ -14,15 +14,15 @@ class SystemStatusCommand extends BaseSlashCommandInteraction { .setDescription('Show operational status of the bot with additional technical information.'); const isSystemCommand: boolean = true; super(data, isSystemCommand); + + this.validators = [(args) => checkValidGuildId(args)]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { const { executionId, interaction, client } = params; const logger = this.getLogger(this.name, executionId, interaction); - if (await notValidGuildId({ interaction, executionId })) { - return; - } + await this.runValidators({ interaction, executionId }); // from normal /status command const uptimeString: string = await getUptimeFormatted({ executionId }); diff --git a/src/interactions/components/filters-disable-button.ts b/src/interactions/components/filters-disable-button.ts index e1bad53..57bf766 100644 --- a/src/interactions/components/filters-disable-button.ts +++ b/src/interactions/components/filters-disable-button.ts @@ -2,12 +2,18 @@ import { GuildQueue, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember } from 'discord.js'; import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes'; import { BaseComponentInteraction } from '../../classes/interactions'; -import { queueDoesNotExist } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import { checkQueueExists } from '../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; class FiltersDisableButtonComponent extends BaseComponentInteraction { constructor() { super('filters-disable-button'); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args) + ]; } async execute(params: BaseComponentParams): BaseComponentReturnType { @@ -16,17 +22,7 @@ class FiltersDisableButtonComponent extends BaseComponentInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); // Reset filters before enabling provided filters if (queue.filters.ffmpeg.filters.length > 0) { diff --git a/src/interactions/components/filters-select-menu.ts b/src/interactions/components/filters-select-menu.ts index 526c084..9108f0b 100644 --- a/src/interactions/components/filters-select-menu.ts +++ b/src/interactions/components/filters-select-menu.ts @@ -4,13 +4,19 @@ import { EmbedBuilder, GuildMember, StringSelectMenuInteraction } from 'discord. import { FFmpegFilterOption, FFmpegFilterOptions } from '../../types/configTypes'; import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes'; import { BaseComponentInteraction } from '../../classes/interactions'; -import { queueDoesNotExist } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import { checkQueueExists } from '../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions'); class FiltersSelectMenuComponent extends BaseComponentInteraction { constructor() { super('filters-select-menu'); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args) + ]; } async execute(params: BaseComponentParams): BaseComponentReturnType { @@ -22,17 +28,7 @@ class FiltersSelectMenuComponent extends BaseComponentInteraction { const selectMenuInteraction: StringSelectMenuInteraction = interaction as StringSelectMenuInteraction; const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); queue.filters.ffmpeg.setInputArgs([ '-threads', diff --git a/src/interactions/components/nowplaying-skip-button.ts b/src/interactions/components/nowplaying-skip-button.ts index 8c28090..1c6d536 100644 --- a/src/interactions/components/nowplaying-skip-button.ts +++ b/src/interactions/components/nowplaying-skip-button.ts @@ -2,12 +2,19 @@ import { GuildQueue, Track, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember } from 'discord.js'; import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes'; import { BaseComponentInteraction } from '../../classes/interactions'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import { checkQueueExists, checkQueueCurrentTrack } from '../../utils/validation/queueValidator'; +import { checkSameVoiceChannel, checkInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; class NowplayingSkipButton extends BaseComponentInteraction { constructor() { super('nowplaying-skip-button'); + + this.validators = [ + (args) => checkInVoiceChannel(args), + (args) => checkSameVoiceChannel(args), + (args) => checkQueueExists(args), + (args) => checkQueueCurrentTrack(args) + ]; } async execute(params: BaseComponentParams): BaseComponentReturnType { @@ -16,18 +23,7 @@ class NowplayingSkipButton extends BaseComponentInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; - const validators = [ - () => notInVoiceChannel({ interaction, executionId }), - () => notInSameVoiceChannel({ interaction, queue, executionId }), - () => queueDoesNotExist({ interaction, queue, executionId }), - () => queueNoCurrentTrack({ interaction, queue, executionId }) - ]; - - for (const validator of validators) { - if (await validator()) { - return; - } - } + await this.runValidators({ interaction, queue, executionId }); if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) { logger.debug('Tried skipping track but there was no queue.'); diff --git a/src/types/interactionTypes.d.ts b/src/types/interactionTypes.d.ts index d6d74ef..6cda73e 100644 --- a/src/types/interactionTypes.d.ts +++ b/src/types/interactionTypes.d.ts @@ -7,22 +7,26 @@ import { } from 'discord.js'; import { ExtendedClient } from './clientTypes'; +export type PlayerStatistics = { + activeVoiceConnections: number; + totalTracks: number; + totalListeners: number; +}; + export type ShardInfo = { shardId: number; memUsage: number; guildCount: number; guildMemberCount: number; - playerStatistics: { - activeVoiceConnections: number; - totalTracks: number; - totalListeners: number; - }; + playerStatistics: PlayerStatistics; +}; + +export type Bridge = { + views: number; }; export type TrackMetadata = { - bridge: { - views: number; - }; + bridge: Bridge; }; type BaseInteractionParams = { @@ -48,3 +52,10 @@ export type BaseSlashCommandReturnType = Promise | void>; export type BaseAutocompleteReturnType = Promise; export type BaseComponentReturnType = Promise | void>; + +export enum FilterType { + FFmpeg = 'ffmpeg', + Biquad = 'biquad', + Equalizer = 'equalizer', + Disable = 'disable' +} diff --git a/src/types/utilTypes.d.ts b/src/types/utilTypes.d.ts index db03d0c..7428eff0 100644 --- a/src/types/utilTypes.d.ts +++ b/src/types/utilTypes.d.ts @@ -23,6 +23,8 @@ export type CustomEvent = { isPlayerEvent?: boolean; }; +export type Validator = (args: ValidatorParams) => Promise; + export type ValidatorParams = { interaction: ChatInputCommandInteraction | MessageComponentInteraction; queue?: GuildQueue; diff --git a/src/utils/validation/permissionValidator.ts b/src/utils/validation/permissionValidator.ts index d5122ef..6e7a6d4 100644 --- a/src/utils/validation/permissionValidator.ts +++ b/src/utils/validation/permissionValidator.ts @@ -12,9 +12,10 @@ import { Logger } from 'pino'; import loggerModule from '../../services/logger'; import { EmbedOptions } from '../../types/configTypes'; import { ValidatorParams } from '../../types/utilTypes'; +import { InteractionValidationError } from '../../classes/interactions'; const embedOptions: EmbedOptions = config.get('embedOptions'); -export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: ValidatorParams) => { +export const checkVoicePermissionJoinAndTalk = async ({ interaction, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'cannotJoinVoiceOrTalk', @@ -33,7 +34,7 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: Valida embeds: [ new EmbedBuilder() .setDescription( - `**${embedOptions.icons.warning} Oops!**\nI do not have permission to play audio in the voice channel you are in.\n\nPlease make sure I have the **\`Connect\`** and **\`Speak\`** permissions in this voice channel.` + `**${embedOptions.icons.warning} Oops!**\nI do not have permission to play audio in the voice channel <#${channel.id}>.\n\nPlease make sure I have the **\`Connect\`** and **\`Speak\`** permissions in this voice channel.` ) .setColor(embedOptions.colors.warning) ] @@ -42,13 +43,13 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: Valida logger.debug( `User tried to use command '${interactionIdentifier}' but the bot had no permission to join/speak in the voice channel.` ); - return true; + throw new InteractionValidationError('Bot cannot join or speak in voice channel.'); } - return false; + return; }; -export const cannotSendMessageInChannel = async ({ interaction, executionId }: ValidatorParams) => { +export const checkChannelPermissionViewable = async ({ interaction, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'cannotSendMessageInChannel', @@ -81,7 +82,7 @@ export const cannotSendMessageInChannel = async ({ interaction, executionId }: V embeds: [ new EmbedBuilder() .setDescription( - `**${embedOptions.icons.warning} Oops!**\nI do not have permission to send message replies in the channel you are in.\n\nPlease make sure I have the **\`View Channel\`** permission in this text channel.` + `**${embedOptions.icons.warning} Oops!**\nI do not have permission to send message replies in the channel <#${channel.id}>.\n\nPlease make sure I have the **\`View Channel\`** permission in this text channel.` ) .setColor(embedOptions.colors.warning) ] @@ -106,8 +107,8 @@ export const cannotSendMessageInChannel = async ({ interaction, executionId }: V } } - return true; + throw new InteractionValidationError('Bot cannot send message in channel.'); } - return false; + return; }; diff --git a/src/utils/validation/queueValidator.ts b/src/utils/validation/queueValidator.ts index 6fa87fb..fd9a680 100644 --- a/src/utils/validation/queueValidator.ts +++ b/src/utils/validation/queueValidator.ts @@ -4,9 +4,10 @@ import { Logger } from 'pino'; import loggerModule from '../../services/logger'; import { EmbedOptions } from '../../types/configTypes'; import { ValidatorParams } from '../../types/utilTypes'; +import { InteractionValidationError } from '../../classes/interactions'; const embedOptions: EmbedOptions = config.get('embedOptions'); -export const queueDoesNotExist = async ({ interaction, queue, executionId }: ValidatorParams) => { +export const checkQueueExists = async ({ interaction, queue, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'queueDoesNotExist', @@ -30,13 +31,13 @@ export const queueDoesNotExist = async ({ interaction, queue, executionId }: Val }); logger.debug(`User tried to use command '${interactionIdentifier}' but there was no queue.`); - return true; + throw new InteractionValidationError('Queue does not exist.'); } - return false; + return; }; -export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: ValidatorParams) => { +export const checkQueueCurrentTrack = async ({ interaction, queue, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'queueNoCurrentTrack', @@ -60,13 +61,13 @@ export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: V }); logger.debug(`User tried to use command '${interactionIdentifier}' but there was no current track.`); - return true; + throw new InteractionValidationError('Queue has no current track.'); } - return false; + return; }; -export const queueIsEmpty = async ({ interaction, queue, executionId }: ValidatorParams) => { +export const checkQueueEmpty = async ({ interaction, queue, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'queueIsEmpty', @@ -90,8 +91,8 @@ export const queueIsEmpty = async ({ interaction, queue, executionId }: Validato }); logger.debug(`User tried to use command '${interactionIdentifier}' but there was no tracks in the queue.`); - return true; + throw new InteractionValidationError('Queue is empty.'); } - return false; + return; }; diff --git a/src/utils/validation/systemCommandValidator.ts b/src/utils/validation/systemCommandValidator.ts index 0d21a54..a8a1dd6 100644 --- a/src/utils/validation/systemCommandValidator.ts +++ b/src/utils/validation/systemCommandValidator.ts @@ -4,10 +4,11 @@ import { Logger } from 'pino'; import loggerModule from '../../services/logger'; import { EmbedOptions, SystemOptions } from '../../types/configTypes'; import { ValidatorParams } from '../../types/utilTypes'; +import { InteractionValidationError } from '../../classes/interactions'; const embedOptions: EmbedOptions = config.get('embedOptions'); const systemOptions: SystemOptions = config.get('systemOptions'); -export const notValidGuildId = async ({ interaction, executionId }: ValidatorParams) => { +export const checkValidGuildId = async ({ interaction, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'notValidGuildId', @@ -33,8 +34,8 @@ export const notValidGuildId = async ({ interaction, executionId }: ValidatorPar logger.debug( `User tried to use command '${interactionIdentifier}' but system command cannot be executed in the specified guild.` ); - return true; + throw new InteractionValidationError('System command cannot be executed in the specified guild.'); } - return false; + return; }; diff --git a/src/utils/validation/voiceChannelValidator.ts b/src/utils/validation/voiceChannelValidator.ts index c3cf358..7759341 100644 --- a/src/utils/validation/voiceChannelValidator.ts +++ b/src/utils/validation/voiceChannelValidator.ts @@ -4,9 +4,10 @@ import { Logger } from 'pino'; import loggerModule from '../../services/logger'; import { EmbedOptions } from '../../types/configTypes'; import { ValidatorParams } from '../../types/utilTypes'; +import { InteractionValidationError } from '../../classes/interactions'; const embedOptions: EmbedOptions = config.get('embedOptions'); -export const notInVoiceChannel = async ({ interaction, executionId }: ValidatorParams) => { +export const checkInVoiceChannel = async ({ interaction, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'validator', name: 'notInVoiceChannel', @@ -30,13 +31,13 @@ export const notInVoiceChannel = async ({ interaction, executionId }: ValidatorP }); logger.debug(`User tried to use command '${interactionIdentifier}' but was not in a voice channel.`); - return true; + throw new InteractionValidationError('User not in voice channel.'); } - return false; + return; }; -export const notInSameVoiceChannel = async ({ interaction, queue, executionId }: ValidatorParams) => { +export const checkSameVoiceChannel = async ({ interaction, queue, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'notInSameVoiceChannel', @@ -50,7 +51,7 @@ export const notInSameVoiceChannel = async ({ interaction, queue, executionId }: if (!queue || !queue.dispatcher) { // If there is no queue or bot is not in voice channel, then there is no need to check if user is in same voice channel. - return false; + return; } if ( @@ -68,8 +69,8 @@ export const notInSameVoiceChannel = async ({ interaction, queue, executionId }: }); logger.debug(`User tried to use command '${interactionIdentifier}' but was not in the same voice channel.`); - return true; + throw new InteractionValidationError('User not in same voice channel.'); } - return false; + return; };