| 1 | import { SlashCommandClosure } from './SlashCommandClosure.js'; |
| 2 | import { commonEnumProviders } from './SlashCommandCommonEnumsProvider.js'; |
| 3 | import { SlashCommandEnumValue } from './SlashCommandEnumValue.js'; |
| 4 | import { SlashCommandExecutor } from './SlashCommandExecutor.js'; |
| 5 | import { SlashCommandScope } from './SlashCommandScope.js'; |
| 6 | |
| 7 | |
| 8 | /**@readonly*/ |
| 9 | /**@enum {string}*/ |
| 10 | export const ARGUMENT_TYPE = { |
| 11 | 'STRING': 'string', |
| 12 | 'NUMBER': 'number', |
| 13 | 'RANGE': 'range', |
| 14 | 'BOOLEAN': 'bool', |
| 15 | 'VARIABLE_NAME': 'varname', |
| 16 | 'CLOSURE': 'closure', |
| 17 | 'SUBCOMMAND': 'subcommand', |
| 18 | 'LIST': 'list', |
| 19 | 'DICTIONARY': 'dictionary', |
| 20 | }; |
| 21 | |
| 22 | export class SlashCommandArgument { |
| 23 | /** |
| 24 | * Creates an unnamed argument from a properties object. |
| 25 | * @param {Object} props |
| 26 | * @param {string} props.description description of the argument |
| 27 | * @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} [props.typeList=[ARGUMENT_TYPE.STRING]] default: ARGUMENT_TYPE.STRING - list of accepted types (from ARGUMENT_TYPE) |
| 28 | * @param {boolean} [props.isRequired=false] default: false - whether the argument is required (false = optional argument) |
| 29 | * @param {boolean} [props.acceptsMultiple=false] default: false - whether argument accepts multiple values |
| 30 | * @param {string|SlashCommandClosure} [props.defaultValue=null] default value if no value is provided |
| 31 | * @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} [props.enumList=[]] list of accepted values |
| 32 | * @param {(executor:SlashCommandExecutor, scope:SlashCommandScope)=>SlashCommandEnumValue[]} [props.enumProvider=null] function that returns auto complete options |
| 33 | * @param {boolean} [props.forceEnum=false] default: false - whether the input must match one of the enum values |
| 34 | */ |
| 35 | static fromProps(props) { |
| 36 | return new SlashCommandArgument( |
| 37 | props.description, |
| 38 | props.typeList ?? [ARGUMENT_TYPE.STRING], |
| 39 | props.isRequired ?? false, |
| 40 | props.acceptsMultiple ?? false, |
| 41 | props.defaultValue ?? null, |
| 42 | props.enumList ?? [], |
| 43 | props.enumProvider ?? null, |
| 44 | props.forceEnum ?? false, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /**@type {string}*/ description; |
| 49 | /**@type {ARGUMENT_TYPE[]}*/ typeList = []; |
| 50 | /**@type {boolean}*/ isRequired = false; |
| 51 | /**@type {boolean}*/ acceptsMultiple = false; |
| 52 | /**@type {string|SlashCommandClosure}*/ defaultValue; |
| 53 | /**@type {SlashCommandEnumValue[]}*/ enumList = []; |
| 54 | /**@type {(executor:SlashCommandExecutor, scope:SlashCommandScope)=>SlashCommandEnumValue[]}*/ enumProvider = null; |
| 55 | /**@type {boolean}*/ forceEnum = false; |
| 56 | |
| 57 | /** |
| 58 | * @param {string} description |
| 59 | * @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} types |
| 60 | * @param {string|SlashCommandClosure} defaultValue |
| 61 | * @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} enums |
| 62 | * @param {(executor:SlashCommandExecutor, scope:SlashCommandScope)=>SlashCommandEnumValue[]} enumProvider function that returns auto complete options |
| 63 | */ |
| 64 | constructor(description, types, isRequired = false, acceptsMultiple = false, defaultValue = null, enums = [], enumProvider = null, forceEnum = false) { |
| 65 | this.description = description; |
| 66 | this.typeList = types ? Array.isArray(types) ? types : [types] : []; |
| 67 | this.isRequired = isRequired ?? false; |
| 68 | this.acceptsMultiple = acceptsMultiple ?? false; |
| 69 | this.defaultValue = defaultValue; |
| 70 | this.enumList = (enums ? Array.isArray(enums) ? enums : [enums] : []).map(it => { |
| 71 | if (it instanceof SlashCommandEnumValue) return it; |
| 72 | return new SlashCommandEnumValue(it); |
| 73 | }); |
| 74 | this.enumProvider = enumProvider; |
| 75 | this.forceEnum = forceEnum; |
| 76 | |
| 77 | // If no enums were set explictly and the type is one where we know possible enum values, we set them here |
| 78 | if (!this.enumList.length && this.typeList.length === 1 && this.typeList.includes(ARGUMENT_TYPE.BOOLEAN)) this.enumList = commonEnumProviders.boolean()(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | export class SlashCommandNamedArgument extends SlashCommandArgument { |
| 83 | /** |
| 84 | * Creates an unnamed argument from a properties object. |
| 85 | * @param {Object} props |
| 86 | * @param {string} props.name the argument's name |
| 87 | * @param {string} props.description description of the argument |
| 88 | * @param {string[]} [props.aliasList=[]] list of aliases |
| 89 | * @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} [props.typeList=[ARGUMENT_TYPE.STRING]] default: ARGUMENT_TYPE.STRING - list of accepted types (from ARGUMENT_TYPE) |
| 90 | * @param {boolean} [props.isRequired=false] default: false - whether the argument is required (false = optional argument) |
| 91 | * @param {boolean} [props.acceptsMultiple=false] default: false - whether argument accepts multiple values |
| 92 | * @param {string|SlashCommandClosure} [props.defaultValue=null] default value if no value is provided |
| 93 | * @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} [props.enumList=[]] list of accepted values |
| 94 | * @param {(executor:SlashCommandExecutor, scope:SlashCommandScope)=>SlashCommandEnumValue[]} [props.enumProvider=null] function that returns auto complete options |
| 95 | * @param {boolean} [props.forceEnum=false] default: false - whether the input must match one of the enum values |
| 96 | */ |
| 97 | static fromProps(props) { |
| 98 | return new SlashCommandNamedArgument( |
| 99 | props.name, |
| 100 | props.description, |
| 101 | props.typeList ?? [ARGUMENT_TYPE.STRING], |
| 102 | props.isRequired ?? false, |
| 103 | props.acceptsMultiple ?? false, |
| 104 | props.defaultValue ?? null, |
| 105 | props.enumList ?? [], |
| 106 | props.aliasList ?? [], |
| 107 | props.enumProvider ?? null, |
| 108 | props.forceEnum ?? false, |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /**@type {string}*/ name; |
| 113 | /**@type {string[]}*/ aliasList = []; |
| 114 | |
| 115 | /** |
| 116 | * @param {string} name |
| 117 | * @param {string} description |
| 118 | * @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} types |
| 119 | * @param {boolean} [isRequired=false] |
| 120 | * @param {boolean} [acceptsMultiple=false] |
| 121 | * @param {string|SlashCommandClosure} [defaultValue=null] |
| 122 | * @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} [enums=[]] |
| 123 | * @param {string[]} [aliases=[]] |
| 124 | * @param {(executor:SlashCommandExecutor, scope:SlashCommandScope)=>SlashCommandEnumValue[]} [enumProvider=null] function that returns auto complete options |
| 125 | * @param {boolean} [forceEnum=false] |
| 126 | */ |
| 127 | constructor(name, description, types, isRequired = false, acceptsMultiple = false, defaultValue = null, enums = [], aliases = [], enumProvider = null, forceEnum = false) { |
| 128 | super(description, types, isRequired, acceptsMultiple, defaultValue, enums, enumProvider, forceEnum); |
| 129 | this.name = name; |
| 130 | this.aliasList = aliases ? Array.isArray(aliases) ? aliases : [aliases] : []; |
| 131 | } |
| 132 | } |