From ac33ffcb47e46d1e171042146851bb2551aff033 Mon Sep 17 00:00:00 2001 From: ITOH Date: Sun, 23 May 2021 12:29:42 +0200 Subject: [PATCH] move required option to the start of array after optional --- src/util/utils.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/util/utils.ts b/src/util/utils.ts index 166f58e46..bd86f0486 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -126,6 +126,9 @@ function validateSlashOptionChoices( /** @private */ function validateSlashOptions(options: ApplicationCommandOption[]) { + let allowRequired = true; + const newOptions: ApplicationCommandOption[] = []; + for (const option of options) { eventHandlers.debug?.("loop", `Running for of loop in validateSlashOptions function.`); if (option.choices?.length) { @@ -145,7 +148,18 @@ function validateSlashOptions(options: ApplicationCommandOption[]) { if (option.choices) { validateSlashOptionChoices(option.choices, option.type); } + + if (!allowRequired && option.required) { + newOptions.unshift(option); + continue; + } + + if (allowRequired && !option.required) allowRequired = false; + + newOptions.push(option); } + + return newOptions; } export function validateSlashCommands( @@ -176,7 +190,7 @@ export function validateSlashCommands( throw new Error(Errors.TOO_MANY_SLASH_OPTIONS); } - validateSlashOptions(command.options); + command.options = validateSlashOptions(command.options); } return command;