From b1502793002298f21d23c65b88ff3833e22e93bb Mon Sep 17 00:00:00 2001 From: Shigma Date: Mon, 20 Feb 2023 14:11:44 +0800 Subject: [PATCH] feat(core): support help / rate-limit command config injection --- packages/core/src/command/command.ts | 5 +---- packages/core/src/command/index.ts | 2 ++ plugins/common/help/src/index.ts | 16 ++++++++++++---- plugins/common/rate-limit/src/index.ts | 5 +++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/core/src/command/command.ts b/packages/core/src/command/command.ts index 6e98cc882e..9f9e52d737 100644 --- a/packages/core/src/command/command.ts +++ b/packages/core/src/command/command.ts @@ -346,9 +346,6 @@ export namespace Command { } export const Config: Schema = Schema.object({ - authority: Schema.computed(Schema.natural()).default(1), - hidden: Schema.boolean().default(false), - checkArgCount: Schema.boolean().default(false), - checkUnknown: Schema.boolean().default(false), + authority: Schema.computed(Schema.natural()).description('指令的权限等级。').default(1), }) } diff --git a/packages/core/src/command/index.ts b/packages/core/src/command/index.ts index 7c3edda3c3..9b6d5a6569 100644 --- a/packages/core/src/command/index.ts +++ b/packages/core/src/command/index.ts @@ -103,6 +103,8 @@ export class Commander { return session.execute(message, next) }) }) + + ctx.schema.extend('command', Command.Config, 1000) } private _resolvePrefixes(session: Session) { diff --git a/plugins/common/help/src/index.ts b/plugins/common/help/src/index.ts index 96536840fc..bd119b5c48 100644 --- a/plugins/common/help/src/index.ts +++ b/plugins/common/help/src/index.ts @@ -1,4 +1,4 @@ -import { Argv, Channel, Command, Context, FieldCollector, Schema, segment, Session, Tables, User } from 'koishi' +import { Argv, Channel, Command, Computed, Context, FieldCollector, Schema, segment, Session, Tables, User } from 'koishi' import zhCN from './locales/zh-CN.yml' import enUS from './locales/en-US.yml' import jaJP from './locales/ja-JP.yml' @@ -16,13 +16,13 @@ declare module 'koishi' { /** hide all options by default */ hideOptions?: boolean /** hide command */ - hidden?: boolean + hidden?: Computed } } namespace Argv { interface OptionConfig { - hidden?: boolean | ((session: Session) => boolean) + hidden?: Computed } } } @@ -69,6 +69,14 @@ export function apply(ctx: Context, config: Config) { ctx.i18n.define('fr', frFR) ctx.i18n.define('zh-TW', zhTW) + ctx.schema.extend('command', Schema.object({ + hidden: Schema.computed(Schema.boolean()).description('在帮助菜单中隐藏指令。').default(false), + }), 900) + + ctx.schema.extend('command-option', Schema.object({ + hidden: Schema.computed(Schema.boolean()).description('在帮助菜单中隐藏选项。').default(false), + }), 900) + if (config.options !== false) { ctx.$commander._commandList.forEach(cmd => cmd.use(enableHelp)) ctx.on('command-added', cmd => cmd.use(enableHelp)) @@ -155,7 +163,7 @@ export function apply(ctx: Context, config: Config) { function* getCommands(session: Session<'authority'>, commands: Command[], showHidden = false): Generator { for (const command of commands) { - if (!showHidden && command.config.hidden) continue + if (!showHidden && session.resolve(command.config.hidden)) continue if (command.match(session)) { yield command } else { diff --git a/plugins/common/rate-limit/src/index.ts b/plugins/common/rate-limit/src/index.ts index 81f14437d2..1dd72d3ba7 100644 --- a/plugins/common/rate-limit/src/index.ts +++ b/plugins/common/rate-limit/src/index.ts @@ -42,6 +42,11 @@ export function apply(ctx: Context) { timers: 'json', }) + ctx.schema.extend('command', Schema.object({ + maxUsage: Schema.computed(Schema.number()).description('每天的调用次数上限。'), + minInterval: Schema.computed(Schema.number()).description('连续调用的最小间隔。'), + }), 800) + // add user fields ctx.before('command/attach-user', ({ command, options = {} }, fields) => { if (!command) return