From 4b5704896d690c2a4bd8357ac2dc58664b35c39f Mon Sep 17 00:00:00 2001 From: LenAnderson Date: Tue, 16 Jul 2024 09:26:37 -0400 Subject: [PATCH] more flexibililty for enums custom mapping from enum value class to enum option class --- .../SlashCommandAutoCompleteNameResult.js | 4 ++-- .../SlashCommandEnumAutoCompleteOption.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/public/scripts/slash-commands/SlashCommandAutoCompleteNameResult.js b/public/scripts/slash-commands/SlashCommandAutoCompleteNameResult.js index 6c00c275c..e72a20e6a 100644 --- a/public/scripts/slash-commands/SlashCommandAutoCompleteNameResult.js +++ b/public/scripts/slash-commands/SlashCommandAutoCompleteNameResult.js @@ -115,7 +115,7 @@ export class SlashCommandAutoCompleteNameResult extends AutoCompleteNameResult { const result = new AutoCompleteSecondaryNameResult( value, start + name.length, - enumList.map(it=>new SlashCommandEnumAutoCompleteOption(this.executor.command, it)), + enumList.map(it=>SlashCommandEnumAutoCompleteOption.from(this.executor.command, it)), true, ); result.isRequired = true; @@ -182,7 +182,7 @@ export class SlashCommandAutoCompleteNameResult extends AutoCompleteNameResult { const result = new AutoCompleteSecondaryNameResult( value, start, - enumList.map(it=>new SlashCommandEnumAutoCompleteOption(this.executor.command, it)), + enumList.map(it=>SlashCommandEnumAutoCompleteOption.from(this.executor.command, it)), false, ); const isCompleteValue = enumList.find(it=>it.value == value); diff --git a/public/scripts/slash-commands/SlashCommandEnumAutoCompleteOption.js b/public/scripts/slash-commands/SlashCommandEnumAutoCompleteOption.js index 0c04d9c62..01f8188cc 100644 --- a/public/scripts/slash-commands/SlashCommandEnumAutoCompleteOption.js +++ b/public/scripts/slash-commands/SlashCommandEnumAutoCompleteOption.js @@ -3,6 +3,17 @@ import { SlashCommand } from './SlashCommand.js'; import { SlashCommandEnumValue } from './SlashCommandEnumValue.js'; export class SlashCommandEnumAutoCompleteOption extends AutoCompleteOption { + /** + * @param {SlashCommand} cmd + * @param {SlashCommandEnumValue} enumValue + * @returns {SlashCommandEnumAutoCompleteOption} + */ + static from(cmd, enumValue) { + const mapped = this.valueToOptionMap.find(it=>enumValue instanceof it.value)?.option ?? this; + return new mapped(cmd, enumValue); + } + /**@type {{value:(typeof SlashCommandEnumValue), option:(typeof SlashCommandEnumAutoCompleteOption)}[]} */ + static valueToOptionMap = []; /**@type {SlashCommand}*/ cmd; /**@type {SlashCommandEnumValue}*/ enumValue;