2022-07-29 13:16:43 +02:00
|
|
|
import { OptionType } from "../interfaces/CommandOption";
|
2022-07-28 21:06:11 +02:00
|
|
|
import { ICommand, CommandDefinition } from "../interfaces/Command";
|
|
|
|
import SubcommandOption from "./SubcommandOption";
|
|
|
|
import SubcommandGroupOption from "./SubcommandGroupOption";
|
2022-07-29 13:16:43 +02:00
|
|
|
import CommandOption from "./CommandOption";
|
2022-07-28 21:06:11 +02:00
|
|
|
|
|
|
|
class Command implements ICommand {
|
|
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
aliases: string[];
|
|
|
|
|
2022-07-29 13:16:43 +02:00
|
|
|
options: CommandOption[];
|
2022-07-28 21:06:11 +02:00
|
|
|
|
|
|
|
constructor(def: CommandDefinition) {
|
|
|
|
|
|
|
|
this.name = def.name;
|
|
|
|
|
|
|
|
this.aliases = def.aliases || [];
|
|
|
|
|
|
|
|
this.options = def.options || [];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:16:43 +02:00
|
|
|
private _subcommands(options: CommandOption[]): SubcommandOption[] {
|
2022-07-28 21:06:11 +02:00
|
|
|
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;
|