Commit ec4ea5af authored by nanahira's avatar nanahira

add missing things in PutOption

parent 62b42e1f
......@@ -23,7 +23,11 @@ import {
} from 'koishi';
import { Metadata } from '../meta/metadata.decorators';
import { CommandPut, DoRegister } from '../registry';
import { adaptLocaleDict, registerTemplate } from '../utility';
import {
adaptLocaleDict,
applyOptionToCommand,
registerTemplate,
} from '../utility';
// Register method
......@@ -139,17 +143,9 @@ export const CommandOption = (
desc: string,
config: CommandOptionConfigWithDescription = {},
) =>
CommandDef((cmd, ctx) => {
if (config.description) {
const desc = adaptLocaleDict(config.description);
for (const [locale, text] of Object.entries(desc)) {
ctx.i18n.define(locale, `commands.${cmd.name}.options.${name}`, text);
}
}
const clonedConfig = { ...config };
delete clonedConfig.description;
return cmd.option(name, desc, clonedConfig);
});
CommandDef((cmd, ctx) =>
applyOptionToCommand(ctx, cmd, { name, desc, config }),
);
export const CommandUserFields = (fields: FieldCollector<'user'>) =>
CommandDef((cmd) => cmd.userFields(fields));
......@@ -180,7 +176,7 @@ export const PutArgs = () => CommandPut.decorate('args');
export const PutOption = (
name: string,
desc: string,
config: Argv.OptionConfig = {},
config: CommandOptionConfigWithDescription = {},
) => CommandPut.decorate('option', { name, desc, config });
export const PutUser = (field: FieldCollector<'user'>) =>
......
......@@ -111,7 +111,7 @@ export interface CommandConfigExtended extends Command.Config {
export interface CommandOptionConfig {
name: string;
desc: string;
config?: Argv.OptionConfig;
config?: CommandOptionConfigWithDescription;
}
export type CommandDefinitionFun = (cmd: Command, ctx: Context) => Command;
......
......@@ -7,7 +7,7 @@ import {
TemplateConfig,
} from '../../def';
import { MethodRegistry } from '../abstract-registry';
import { registerTemplate } from '../../utility';
import { applyOptionToCommand, registerTemplate } from '../../utility';
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace CommandPut {
......@@ -35,8 +35,8 @@ export namespace CommandPut {
[Command, Context]
>();
preRegistry.extend('option', (data, cmd) =>
cmd.option(data.name, data.desc, data.config),
preRegistry.extend('option', (data, cmd, ctx) =>
applyOptionToCommand(ctx, cmd, data),
);
preRegistry.extend('user', (data, cmd) => {
......
import { Command, Context, Dict } from 'koishi';
import { ContextSelector, OnContextFunction, TemplateConfig } from '../def';
import {
CommandOptionConfig,
ContextSelector,
OnContextFunction,
TemplateConfig,
} from '../def';
export function applySelector(
ctx: Context,
......@@ -49,3 +54,20 @@ export const registerTemplate = (
ctx.i18n.define(locale, key, text);
}
};
export function applyOptionToCommand(
ctx: Context,
cmd: Command,
def: CommandOptionConfig,
) {
const { name, desc, config } = def;
if (config?.description) {
const desc = adaptLocaleDict(config.description);
for (const [locale, text] of Object.entries(desc)) {
ctx.i18n.define(locale, `commands.${cmd.name}.options.${name}`, text);
}
}
const clonedConfig = { ...(config || {}) };
delete clonedConfig.description;
return cmd.option(name, desc, clonedConfig);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment