63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { OptionType } from "../interfaces/CommandOption";
|
|
import { ICommand, CommandDefinition } from "../interfaces/Command";
|
|
import SubcommandOption from "./SubcommandOption";
|
|
import SubcommandGroupOption from "./SubcommandGroupOption";
|
|
import CommandOption from "./CommandOption";
|
|
import { ArgsResult } from "../Parser";
|
|
|
|
abstract class Command implements ICommand {
|
|
|
|
name: string;
|
|
|
|
aliases: string[];
|
|
|
|
options: CommandOption[];
|
|
|
|
constructor(def: CommandDefinition) {
|
|
|
|
this.name = def.name;
|
|
|
|
this.aliases = def.aliases || [];
|
|
|
|
this.options = [];
|
|
for (const opt of def.options || []) {
|
|
if (opt instanceof CommandOption) this.options.push(opt);
|
|
else this.options.push(new CommandOption(opt));
|
|
}
|
|
|
|
}
|
|
|
|
abstract execute(message: unknown, args: ArgsResult): Promise<unknown>;
|
|
|
|
get subcommands(): SubcommandOption[] {
|
|
return this._subcommands(this.options);
|
|
}
|
|
|
|
get subcommandGroups(): SubcommandGroupOption[] {
|
|
return this.options.filter((opt) => opt.type === OptionType.SUB_COMMAND_GROUP);
|
|
}
|
|
|
|
subcommand(name: string): SubcommandOption | null {
|
|
name = name.toLowerCase();
|
|
return this.subcommands.find((cmd) => cmd.name === name) || null;
|
|
}
|
|
|
|
subcommandGroup(name: string): SubcommandGroupOption | null {
|
|
name = name.toLowerCase();
|
|
return this.subcommandGroups.find((opt) => opt.name === name) || null;
|
|
}
|
|
|
|
private _subcommands(options: CommandOption[]): SubcommandOption[] {
|
|
const subcommands: SubcommandOption[] = [];
|
|
for (const opt of options) {
|
|
if (opt.type === OptionType.SUB_COMMAND) subcommands.push(opt);
|
|
else if(opt.type === OptionType.SUB_COMMAND_GROUP) subcommands.push(...this._subcommands(opt.options));
|
|
}
|
|
|
|
return subcommands;
|
|
}
|
|
|
|
}
|
|
|
|
export { Command };
|
|
export default Command; |