@kotori-bot/core
Version:
Kotori Core
154 lines (153 loc) • 5.47 kB
TypeScript
import { UserAccess, type CommandAction, type CommandArgType, type CommandArgTypeSign, type CommandConfig, type ArgsOrigin, type OptsOrigin } from '../types';
import { CommandError } from '../utils/error';
/** Command argument */
interface CommandArg {
/** Argument displayname */
name: string;
/** Argument type */
type: CommandArgTypeSign;
/** Argument is wether optional */
optional: boolean;
/** Argument default value, if exists so `optional` is true */
default?: CommandArgType;
/** Argument is wether rest arguments */
rest: boolean;
}
/** Command option */
interface CommandOption {
/** Option short name, composed a uppercase letter */
name: string;
/** Option type */
type: CommandArgTypeSign;
/** Option full name (real name), composed some lowercase letters and connect by `-` between word and word */
realname: string;
/** Option description */
description?: string;
}
/**
* Command meta data.
*
* @template Args - Command arguments
* @template Opts - Command options
*/
interface CommandData<Args = ArgsOrigin, Opts = OptsOrigin, Scope = 'all'> {
/** Command root content */
root: string;
/** Command alias (need bring command prefix) */
alias: string[];
/** Command shortcut (needn't bring command prefix) */
shortcut: string[];
/** Command is wether hide at the menu */
hide: boolean;
/** Command arguments */
args: CommandArg[];
/** Command options */
options: CommandOption[];
/** Command require message scope (session type) */
scope: CommandConfig['scope'];
/** Command require user access level */
access: UserAccess;
/** Command description */
description?: string;
/** Command help message, it has more details than `description` */
help?: string;
/** Command action */
action?: CommandAction<Args, Opts, Scope>;
}
type GetSignType<T extends string> = T extends `${string}number${string}` ? number : T extends `${string}boolean${string}` ? boolean : string;
type GetArgCtn<Template extends string> = Template extends `${string}:${infer Suffix}` ? Suffix extends `${infer T}=${string}` ? GetSignType<T> : GetSignType<Suffix> : Template extends `${infer T}=${string}` ? GetSignType<T> : string;
type ParseArgs<Template extends string> = string extends Template ? ArgsOrigin : Template extends `${string} ${`<${infer Ctn}>`}${infer Rest}` ? Ctn extends `...${infer Ctn2}` ? [...GetSignType<Ctn2>[]] : [GetArgCtn<Ctn>, ...ParseArgs<Rest>] : Template extends `${string} [${infer Ctn}]${infer Rest}` ? Ctn extends `${infer Ctn2}=${string}` ? Ctn2 extends `...${infer Ctn3}` ? [...GetSignType<Ctn3>[]] : [GetArgCtn<Ctn2>, ...ParseArgs<Rest>] : Ctn extends `...${infer Ctn2}` ? [...GetSignType<Ctn2>[]] : [GetArgCtn<Ctn>?, ...ParseArgs<Rest>] : [];
type ParseOpts<Template extends string> = string extends Template ? {} : Template extends `${infer K}:${infer V}` ? GetSignType<V> extends boolean ? {
[C in K]: GetSignType<V>;
} : {
[C in K]?: GetSignType<V>;
} : {};
/**
* Command.
*
* @template Template - Command template
* @template Opts - Command options
*
* @class
*/
export declare class Command<Template extends string = string, Opts extends OptsOrigin = OptsOrigin, Scope extends CommandConfig['scope'] | 'all' = 'all'> {
private static handleDefaultValue;
private static parseArgs;
static run(input: string, data: CommandData): CommandError | {
args: ArgsOrigin;
options: OptsOrigin;
};
private template;
/**
* Command meta data.
*
* @readonly
*/
readonly meta: CommandData<ParseArgs<Template>, Opts, Scope>;
/**
* Create a command instance
*
* @param template - Command template.
* @param config - Command config.
*/
constructor(template: Template, config?: CommandConfig);
private parse;
/**
* Add command alias.
*
* @param alias - Command alias.
* @returns Command instance
*/
alias(alias: string | string[]): this;
/**
* Add command shortcut.
*
* @param short - shortcut name
* @returns Command instance
*/
shortcut(short: string | string[]): this;
/**
* Set the message scope which command require.
*
* @param scope - Message scope.
* @returns Command instance
*/
scope<S extends CommandConfig['scope']>(scope: S): Command<Template, Opts, S>;
/**
* Set the user access level which command require.
*
* @param access - User access level.
* @returns Command instance
*/
access(access: UserAccess): this;
/**
* Add command option.
*
* @param name - Option name.
* @param template - Option template.
* @returns Command instance
*/
option<TemplateOpt extends string>(name: string, template: TemplateOpt): Command<Template, Opts & ParseOpts<TemplateOpt>, "all">;
/**
* Set command action.
*
* @param callback - Command action.
* @returns Command instance
*/
action(callback: CommandAction<ParseArgs<Template>, Opts, Scope>): this;
/**
* Set command help text.
*
* @param text - Command help text.
* @returns Command instance
*/
help(text: string): this;
/**
* Set command hide.
*
* @param isHide - Command hide.
* @returns Command instance
*/
hide(isHide?: boolean): this;
}
export default Command;