Commit ec4ea5af authored by nanahira's avatar nanahira

add missing things in PutOption

parent 62b42e1f
...@@ -23,7 +23,11 @@ import { ...@@ -23,7 +23,11 @@ import {
} from 'koishi'; } from 'koishi';
import { Metadata } from '../meta/metadata.decorators'; import { Metadata } from '../meta/metadata.decorators';
import { CommandPut, DoRegister } from '../registry'; import { CommandPut, DoRegister } from '../registry';
import { adaptLocaleDict, registerTemplate } from '../utility'; import {
adaptLocaleDict,
applyOptionToCommand,
registerTemplate,
} from '../utility';
// Register method // Register method
...@@ -139,17 +143,9 @@ export const CommandOption = ( ...@@ -139,17 +143,9 @@ export const CommandOption = (
desc: string, desc: string,
config: CommandOptionConfigWithDescription = {}, config: CommandOptionConfigWithDescription = {},
) => ) =>
CommandDef((cmd, ctx) => { CommandDef((cmd, ctx) =>
if (config.description) { applyOptionToCommand(ctx, cmd, { name, desc, config }),
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);
});
export const CommandUserFields = (fields: FieldCollector<'user'>) => export const CommandUserFields = (fields: FieldCollector<'user'>) =>
CommandDef((cmd) => cmd.userFields(fields)); CommandDef((cmd) => cmd.userFields(fields));
...@@ -180,7 +176,7 @@ export const PutArgs = () => CommandPut.decorate('args'); ...@@ -180,7 +176,7 @@ export const PutArgs = () => CommandPut.decorate('args');
export const PutOption = ( export const PutOption = (
name: string, name: string,
desc: string, desc: string,
config: Argv.OptionConfig = {}, config: CommandOptionConfigWithDescription = {},
) => CommandPut.decorate('option', { name, desc, config }); ) => CommandPut.decorate('option', { name, desc, config });
export const PutUser = (field: FieldCollector<'user'>) => export const PutUser = (field: FieldCollector<'user'>) =>
......
...@@ -111,7 +111,7 @@ export interface CommandConfigExtended extends Command.Config { ...@@ -111,7 +111,7 @@ export interface CommandConfigExtended extends Command.Config {
export interface CommandOptionConfig { export interface CommandOptionConfig {
name: string; name: string;
desc: string; desc: string;
config?: Argv.OptionConfig; config?: CommandOptionConfigWithDescription;
} }
export type CommandDefinitionFun = (cmd: Command, ctx: Context) => Command; export type CommandDefinitionFun = (cmd: Command, ctx: Context) => Command;
......
...@@ -7,7 +7,7 @@ import { ...@@ -7,7 +7,7 @@ import {
TemplateConfig, TemplateConfig,
} from '../../def'; } from '../../def';
import { MethodRegistry } from '../abstract-registry'; import { MethodRegistry } from '../abstract-registry';
import { registerTemplate } from '../../utility'; import { applyOptionToCommand, registerTemplate } from '../../utility';
// eslint-disable-next-line @typescript-eslint/no-namespace // eslint-disable-next-line @typescript-eslint/no-namespace
export namespace CommandPut { export namespace CommandPut {
...@@ -35,8 +35,8 @@ export namespace CommandPut { ...@@ -35,8 +35,8 @@ export namespace CommandPut {
[Command, Context] [Command, Context]
>(); >();
preRegistry.extend('option', (data, cmd) => preRegistry.extend('option', (data, cmd, ctx) =>
cmd.option(data.name, data.desc, data.config), applyOptionToCommand(ctx, cmd, data),
); );
preRegistry.extend('user', (data, cmd) => { preRegistry.extend('user', (data, cmd) => {
......
import { Command, Context, Dict } from 'koishi'; import { Command, Context, Dict } from 'koishi';
import { ContextSelector, OnContextFunction, TemplateConfig } from '../def'; import {
CommandOptionConfig,
ContextSelector,
OnContextFunction,
TemplateConfig,
} from '../def';
export function applySelector( export function applySelector(
ctx: Context, ctx: Context,
...@@ -49,3 +54,20 @@ export const registerTemplate = ( ...@@ -49,3 +54,20 @@ export const registerTemplate = (
ctx.i18n.define(locale, key, text); 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