From e67d0004b05a7d6a72a723430c183880802cc6f4 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Tue, 5 Sep 2023 14:57:08 +0200 Subject: [PATCH] fix: Update validators with new types --- src/classes/interactions.ts | 11 +++++ src/interactions/commands/player/filters.ts | 23 +++++----- src/types/utilTypes.d.ts | 42 +------------------ src/utils/validation/permissionValidator.ts | 7 ++-- src/utils/validation/queueValidator.ts | 15 ++++--- .../validation/systemCommandValidator.ts | 7 ++-- src/utils/validation/voiceChannelValidator.ts | 7 ++-- 7 files changed, 39 insertions(+), 73 deletions(-) diff --git a/src/classes/interactions.ts b/src/classes/interactions.ts index dce385e..1bf8f45 100644 --- a/src/classes/interactions.ts +++ b/src/classes/interactions.ts @@ -21,6 +21,7 @@ import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../types/interactionTypes'; +import { ValidatorParams } from '../types/utilTypes'; abstract class BaseInteraction { protected getLoggerBase( @@ -38,6 +39,16 @@ abstract class BaseInteraction { }); } + protected validators: ((args: ValidatorParams) => Promise)[] = []; + + protected async runValidators(args: ValidatorParams): Promise { + for (const validator of this.validators) { + if (await validator(args)) { + return Promise.reject(); + } + } + } + abstract execute( params: BaseInteractionParams ): Promise | ApplicationCommandOptionChoiceData | void>; diff --git a/src/interactions/commands/player/filters.ts b/src/interactions/commands/player/filters.ts index 21ba7f4..63899b2 100644 --- a/src/interactions/commands/player/filters.ts +++ b/src/interactions/commands/player/filters.ts @@ -40,8 +40,14 @@ class FiltersCommand extends BaseSlashCommandInteraction { { name: 'Disable', value: 'disable' } ) ); - super(data); + + this.validators = [ + (args) => notInVoiceChannel(args), + (args) => notInSameVoiceChannel(args), + (args) => queueDoesNotExist(args), + (args) => queueNoCurrentTrack(args) + ]; } async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { @@ -50,18 +56,9 @@ class FiltersCommand 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 }).catch(() => { + return; + }); const filterProvider: string = interaction.options.getString('type') || 'ffmpeg'; diff --git a/src/types/utilTypes.d.ts b/src/types/utilTypes.d.ts index b43fa7c..db03d0c 100644 --- a/src/types/utilTypes.d.ts +++ b/src/types/utilTypes.d.ts @@ -23,19 +23,9 @@ export type CustomEvent = { isPlayerEvent?: boolean; }; -export type NotInVoiceChannelParams = { - interaction: ChatInputCommandInteraction | MessageComponentInteraction; - executionId: string; -}; - -export type NotInSameVoiceChannelParams = { - interaction: ChatInputCommandInteraction | MessageComponentInteraction; - queue: GuildQueue; - executionId: string; -}; - -export type NotValidGuildIdParams = { +export type ValidatorParams = { interaction: ChatInputCommandInteraction | MessageComponentInteraction; + queue?: GuildQueue; executionId: string; }; @@ -44,34 +34,6 @@ export type TransformQueryParams = { executionId: string; }; -export type QueueDoesNotExistParams = { - interaction: ChatInputCommandInteraction | MessageComponentInteraction; - queue: GuildQueue; - executionId: string; -}; - -export type QueueNoCurrentTrackParams = { - interaction: ChatInputCommandInteraction | MessageComponentInteraction; - queue: GuildQueue; - executionId: string; -}; - -export type QueueIsEmptyParams = { - interaction: ChatInputCommandInteraction | MessageComponentInteraction; - queue: GuildQueue; - executionId: string; -}; - -export type CannotJoinVoiceOrTalkParams = { - interaction: ChatInputCommandInteraction | MessageComponentInteraction; - executionId: string; -}; - -export type CannotSendMessageInChannelParams = { - interaction: ChatInputCommandInteraction | MessageComponentInteraction; - executionId: string; -}; - export type GetUptimeFormattedParams = { executionId: string; }; diff --git a/src/utils/validation/permissionValidator.ts b/src/utils/validation/permissionValidator.ts index b1130f5..d5122ef 100644 --- a/src/utils/validation/permissionValidator.ts +++ b/src/utils/validation/permissionValidator.ts @@ -8,14 +8,13 @@ import { VoiceBasedChannel, VoiceChannel } from 'discord.js'; - import { Logger } from 'pino'; import loggerModule from '../../services/logger'; import { EmbedOptions } from '../../types/configTypes'; -import { CannotJoinVoiceOrTalkParams, CannotSendMessageInChannelParams } from '../../types/utilTypes'; +import { ValidatorParams } from '../../types/utilTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); -export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: CannotJoinVoiceOrTalkParams) => { +export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'cannotJoinVoiceOrTalk', @@ -49,7 +48,7 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: Cannot return false; }; -export const cannotSendMessageInChannel = async ({ interaction, executionId }: CannotSendMessageInChannelParams) => { +export const cannotSendMessageInChannel = async ({ interaction, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'cannotSendMessageInChannel', diff --git a/src/utils/validation/queueValidator.ts b/src/utils/validation/queueValidator.ts index 3392e4a..6fa87fb 100644 --- a/src/utils/validation/queueValidator.ts +++ b/src/utils/validation/queueValidator.ts @@ -1,13 +1,12 @@ import config from 'config'; import { EmbedBuilder, InteractionType } from 'discord.js'; - +import { Logger } from 'pino'; import loggerModule from '../../services/logger'; import { EmbedOptions } from '../../types/configTypes'; -import { QueueDoesNotExistParams, QueueIsEmptyParams, QueueNoCurrentTrackParams } from '../../types/utilTypes'; -import { Logger } from 'pino'; +import { ValidatorParams } from '../../types/utilTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); -export const queueDoesNotExist = async ({ interaction, queue, executionId }: QueueDoesNotExistParams) => { +export const queueDoesNotExist = async ({ interaction, queue, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'queueDoesNotExist', @@ -37,7 +36,7 @@ export const queueDoesNotExist = async ({ interaction, queue, executionId }: Que return false; }; -export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: QueueNoCurrentTrackParams) => { +export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'queueNoCurrentTrack', @@ -49,7 +48,7 @@ export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: Q const interactionIdentifier = interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId; - if (!queue.currentTrack) { + if (queue && !queue.currentTrack) { await interaction.editReply({ embeds: [ new EmbedBuilder() @@ -67,7 +66,7 @@ export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: Q return false; }; -export const queueIsEmpty = async ({ interaction, queue, executionId }: QueueIsEmptyParams) => { +export const queueIsEmpty = async ({ interaction, queue, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'queueIsEmpty', @@ -79,7 +78,7 @@ export const queueIsEmpty = async ({ interaction, queue, executionId }: QueueIsE const interactionIdentifier = interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId; - if (queue.tracks.data.length === 0) { + if (queue && queue.tracks.data.length === 0) { await interaction.editReply({ embeds: [ new EmbedBuilder() diff --git a/src/utils/validation/systemCommandValidator.ts b/src/utils/validation/systemCommandValidator.ts index 93ec8ea..0d21a54 100644 --- a/src/utils/validation/systemCommandValidator.ts +++ b/src/utils/validation/systemCommandValidator.ts @@ -1,14 +1,13 @@ import config from 'config'; import { EmbedBuilder, InteractionType } from 'discord.js'; - +import { Logger } from 'pino'; import loggerModule from '../../services/logger'; import { EmbedOptions, SystemOptions } from '../../types/configTypes'; -import { NotValidGuildIdParams } from '../../types/utilTypes'; -import { Logger } from 'pino'; +import { ValidatorParams } from '../../types/utilTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); const systemOptions: SystemOptions = config.get('systemOptions'); -export const notValidGuildId = async ({ interaction, executionId }: NotValidGuildIdParams) => { +export const notValidGuildId = async ({ interaction, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'notValidGuildId', diff --git a/src/utils/validation/voiceChannelValidator.ts b/src/utils/validation/voiceChannelValidator.ts index 8662931..c3cf358 100644 --- a/src/utils/validation/voiceChannelValidator.ts +++ b/src/utils/validation/voiceChannelValidator.ts @@ -1,13 +1,12 @@ import config from 'config'; import { EmbedBuilder, GuildMember, InteractionType } from 'discord.js'; - import { Logger } from 'pino'; import loggerModule from '../../services/logger'; import { EmbedOptions } from '../../types/configTypes'; -import { NotInSameVoiceChannelParams, NotInVoiceChannelParams } from '../../types/utilTypes'; +import { ValidatorParams } from '../../types/utilTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); -export const notInVoiceChannel = async ({ interaction, executionId }: NotInVoiceChannelParams) => { +export const notInVoiceChannel = async ({ interaction, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'validator', name: 'notInVoiceChannel', @@ -37,7 +36,7 @@ export const notInVoiceChannel = async ({ interaction, executionId }: NotInVoice return false; }; -export const notInSameVoiceChannel = async ({ interaction, queue, executionId }: NotInSameVoiceChannelParams) => { +export const notInSameVoiceChannel = async ({ interaction, queue, executionId }: ValidatorParams) => { const logger: Logger = loggerModule.child({ module: 'utilValidation', name: 'notInSameVoiceChannel',