From b0ecdf1a621ce50d613222ade75cc6689be20b2e Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 24 Mar 2022 14:29:01 +0100 Subject: [PATCH] fix(plugins): createApplicationCommand validation --- .../permissions/src/interactions/commands.ts | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/plugins/permissions/src/interactions/commands.ts b/plugins/permissions/src/interactions/commands.ts index 5c369e26c..aae92fa5e 100644 --- a/plugins/permissions/src/interactions/commands.ts +++ b/plugins/permissions/src/interactions/commands.ts @@ -92,6 +92,41 @@ export function createApplicationCommand(bot: BotWithCache) { // Only slash need to be lowercase options.name = options.name.toLowerCase(); + + // Slash commands require description + if ( + !options.description && + (isChatInput) + ) { + throw new Error( + "Slash commands require some form of a description be provided.", + ); + } + + if ( + options.description && + ((options.type === ApplicationCommandTypes.User) || + (options.type === ApplicationCommandTypes.Message)) + ) { + throw new Error("Context menu commands do not allow a description."); + } + + if ( + options.description && + !bot.utils.validateLength(options.description, { min: 1, max: 100 }) + ) { + throw new Error( + "Application command descriptions must be between 1 and 100 characters.", + ); + } + + if (options.options?.length) { + if (options.options.length > 25) { + throw new Error("Only 25 options are allowed to be provided."); + } + + options.options = validateApplicationCommandOptions(bot, options.options); + } } else { if (!CONTEXT_MENU_COMMANDS_NAME_REGEX.test(options.name)) { throw new Error( @@ -100,41 +135,6 @@ export function createApplicationCommand(bot: BotWithCache) { } } - // Slash commands require description - if ( - !options.description && - (isChatInput) - ) { - throw new Error( - "Slash commands require some form of a description be provided.", - ); - } - - if ( - options.description && - ((options.type === ApplicationCommandTypes.User) || - (options.type === ApplicationCommandTypes.Message)) - ) { - throw new Error("Context menu commands do not allow a description."); - } - - if ( - options.description && - !bot.utils.validateLength(options.description, { min: 1, max: 100 }) - ) { - throw new Error( - "Application command descriptions must be between 1 and 100 characters.", - ); - } - - if (options.options?.length) { - if (options.options.length > 25) { - throw new Error("Only 25 options are allowed to be provided."); - } - - options.options = validateApplicationCommandOptions(bot, options.options); - } - return await createApplicationCommandOld(options, guildId); }; }