From 85cb06bbe4b79f6e118e3875adffe51f708912fb Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Tue, 18 Feb 2020 09:56:52 +0800 Subject: [PATCH] feat(plugin-common): support functional usage --- packages/koishi-core/src/command.ts | 5 +++-- packages/plugin-common/src/help.ts | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/koishi-core/src/command.ts b/packages/koishi-core/src/command.ts index a139ee5009..f4b0b6b888 100644 --- a/packages/koishi-core/src/command.ts +++ b/packages/koishi-core/src/command.ts @@ -23,6 +23,7 @@ export interface ParsedCommandLine extends Partial { } export type UserType = T | ((user: UserData) => T) +export type CommandUsage = string | ((this: Command, meta: Meta) => string | Promise) export interface CommandConfig { /** disallow unknown options */ @@ -80,7 +81,7 @@ export class Command { _aliases: string[] = [] _options: CommandOption[] = [] - _usage?: string + _usage?: CommandUsage _examples: string[] = [] _shortcuts: Record = {} _userFields = new Set() @@ -185,7 +186,7 @@ export class Command { return this } - usage (text: string) { + usage (text: CommandUsage) { this._usage = text return this } diff --git a/packages/plugin-common/src/help.ts b/packages/plugin-common/src/help.ts index 6ac09ea7aa..d47f66a5ed 100644 --- a/packages/plugin-common/src/help.ts +++ b/packages/plugin-common/src/help.ts @@ -69,15 +69,15 @@ export const GLOBAL_HELP_EPILOGUE = [ '输入“帮助+指令名”查看特定指令的语法和使用示例。', ].join('\n') -function showGlobalHelp (context: Context, meta: Meta<'message'>, options: any) { +function showGlobalHelp (context: Context, meta: Meta<'message'>, config: any) { return meta.$send([ GLOBAL_HELP_PROLOGUE, - ...getCommandList(context, meta, null, options.expand), + ...getCommandList(context, meta, null, config.expand), GLOBAL_HELP_EPILOGUE, ].join('\n')) } -async function showCommandHelp (command: Command, meta: Meta<'message'>, options: any) { +async function showCommandHelp (command: Command, meta: Meta<'message'>, config: any) { const output = [command.name + command.declaration, command.config.description] if (command.context.database) { meta.$user = await command.context.database.observeUser(meta.userId) @@ -109,19 +109,20 @@ async function showCommandHelp (command: Command, meta: Meta<'message'>, options } } - if (command._usage) { - output.push(command._usage) + const usage = command._usage + if (usage) { + output.push(typeof usage === 'string' ? usage : await usage.call(command, meta)) } - const _options = command._options.filter(option => !option.hidden) - if (_options.length) { - if (_options.some(o => o.authority)) { + const options = command._options.filter(option => !option.hidden) + if (options.length) { + if (options.some(o => o.authority)) { output.push('可用的选项有(括号内为额外要求的权限等级):') } else { output.push('可用的选项有:') } - _options.forEach((option) => { + options.forEach((option) => { const authority = option.authority ? `(${option.authority}) ` : '' let line = ` ${authority}${option.rawName} ${option.description}` if (option.notUsage && maxUsage !== Infinity) { @@ -138,7 +139,7 @@ async function showCommandHelp (command: Command, meta: Meta<'message'>, options if (command.children.length) { output.push( '可用的子指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):', - ...getCommandList(command.context, meta, command, options.expand), + ...getCommandList(command.context, meta, command, config.expand), ) }