more flexibililty for enums

custom mapping from enum value class to enum option class
This commit is contained in:
LenAnderson 2024-07-16 09:26:37 -04:00
parent b51bf8e38e
commit 4b5704896d
2 changed files with 13 additions and 2 deletions

View File

@ -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);

View File

@ -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;