2022-07-28 21:06:11 +02:00
|
|
|
import ICommandOption, { Choice, CommandOptionDefinition, DependsOnMode, OptionType, ParseResult } from "../interfaces/CommandOption";
|
2022-07-29 13:16:43 +02:00
|
|
|
import IResolver from "../interfaces/Resolver";
|
2022-07-28 21:06:11 +02:00
|
|
|
|
|
|
|
class CommandOption implements ICommandOption {
|
|
|
|
|
2022-07-29 13:16:43 +02:00
|
|
|
[key: string]: unknown;
|
|
|
|
|
2022-07-28 21:06:11 +02:00
|
|
|
name: string;
|
|
|
|
|
|
|
|
aliases: string[];
|
|
|
|
|
2022-07-29 13:16:43 +02:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
options: CommandOption[];
|
2022-07-28 21:06:11 +02:00
|
|
|
|
|
|
|
type: OptionType;
|
|
|
|
|
|
|
|
minimum?: number;
|
|
|
|
|
|
|
|
maximum?: number;
|
|
|
|
|
|
|
|
flag: boolean;
|
|
|
|
|
|
|
|
valueOptional: boolean;
|
|
|
|
|
|
|
|
defaultValue: boolean | number | string | null;
|
|
|
|
|
|
|
|
valueAsAlias: boolean;
|
|
|
|
|
|
|
|
choices: Choice[];
|
|
|
|
|
|
|
|
dependsOn: string[];
|
|
|
|
|
|
|
|
dependsOnMode: DependsOnMode;
|
|
|
|
|
|
|
|
required: boolean;
|
|
|
|
|
|
|
|
rawValue?: string[];
|
|
|
|
|
|
|
|
value?: unknown;
|
|
|
|
|
|
|
|
aliased = false;
|
|
|
|
|
2022-07-29 13:16:43 +02:00
|
|
|
private resolver?: IResolver<unknown, unknown, unknown, unknown>|undefined = undefined;
|
|
|
|
|
2022-07-28 21:06:11 +02:00
|
|
|
constructor(def: CommandOptionDefinition|ICommandOption) {
|
|
|
|
|
|
|
|
this.name = def.name;
|
|
|
|
this.aliases = def.aliases || [];
|
|
|
|
|
|
|
|
this.options = def.options || [];
|
|
|
|
this.choices = def.choices || [];
|
|
|
|
|
|
|
|
this.type = def.type || OptionType.STRING;
|
|
|
|
this.flag = def.flag || false;
|
|
|
|
|
|
|
|
this.valueOptional = def.valueOptional || false;
|
|
|
|
this.defaultValue = def.defaultValue ?? null;
|
|
|
|
this.valueAsAlias = def.valueAsAlias || false;
|
|
|
|
|
|
|
|
this.dependsOn = def.dependsOn || [];
|
|
|
|
this.dependsOnMode = def.dependsOnMode || 'AND';
|
|
|
|
|
|
|
|
this.required = def.required || false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:16:43 +02:00
|
|
|
clone(rawValue?: string[], resolver?: IResolver<unknown, unknown, unknown, unknown>): CommandOption {
|
2022-07-28 21:06:11 +02:00
|
|
|
const opt = new CommandOption(this);
|
|
|
|
opt.rawValue = rawValue;
|
2022-07-29 13:16:43 +02:00
|
|
|
opt.resolver = resolver || undefined;
|
2022-07-28 21:06:11 +02:00
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:16:43 +02:00
|
|
|
async parse(): Promise<ParseResult> {
|
|
|
|
if(!this[OptionType[this.type]]) throw new Error(`Missing parsing function for ${this.type}`);
|
|
|
|
const result = await this[OptionType[this.type]];
|
|
|
|
return result as ParseResult;
|
2022-07-28 21:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get plural(): boolean {
|
|
|
|
return this.type.toString().endsWith('S');
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:16:43 +02:00
|
|
|
protected async MEMBER() {
|
|
|
|
if (!this.resolver) throw new Error('Missing resolver');
|
|
|
|
if(!this.rawValue) throw new Error('Missing raw value');
|
|
|
|
const member = await this.resolver?.resolveMember(this.rawValue[0]);
|
|
|
|
if (!member) return { error: true };
|
|
|
|
return { value: member, removed: this.rawValue };
|
|
|
|
}
|
|
|
|
|
2022-07-28 21:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { CommandOption };
|
|
|
|
export default CommandOption;
|