From c72f607befaf12e918b097bc76aee0a320900af1 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Thu, 1 Apr 2021 03:36:01 +0800 Subject: [PATCH] feat(core): support functional optionConfig.hidden --- packages/koishi-core/src/help.ts | 9 +++++++-- packages/koishi-core/src/parser.ts | 2 +- packages/koishi-core/src/session.ts | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/koishi-core/src/help.ts b/packages/koishi-core/src/help.ts index c7eda0531f..d3f3013efb 100644 --- a/packages/koishi-core/src/help.ts +++ b/packages/koishi-core/src/help.ts @@ -3,6 +3,7 @@ import { TableType } from './database' import { Session, FieldCollector } from './session' import { template } from 'koishi-utils' import { Context } from './context' +import { Domain } from './parser' interface HelpConfig { showHidden?: boolean @@ -111,12 +112,16 @@ function formatCommands(path: string, session: Session, childre return output } +function getOptionVisibility(option: Domain.OptionConfig, session: Session) { + if (session.user && option.authority > session.user.authority) return false + return !session.resolveValue(option.hidden) +} + function getOptions(command: Command, session: Session, maxUsage: number, config: HelpConfig) { if (command.config.hideOptions && !config.showHidden) return [] const options = config.showHidden ? Object.values(command._options) - : Object.values(command._options) - .filter(option => !option.hidden && (!session.user || option.authority <= session.user.authority)) + : Object.values(command._options).filter(option => getOptionVisibility(option, session)) if (!options.length) return [] const output = config.authority && options.some(o => o.authority) diff --git a/packages/koishi-core/src/parser.ts b/packages/koishi-core/src/parser.ts index 1e0926abdc..9fd30ac468 100644 --- a/packages/koishi-core/src/parser.ts +++ b/packages/koishi-core/src/parser.ts @@ -166,7 +166,7 @@ export namespace Domain { fallback?: any type?: T /** hide the option by default */ - hidden?: boolean + hidden?: boolean | ((session: Session) => boolean) authority?: number notUsage?: boolean } diff --git a/packages/koishi-core/src/session.ts b/packages/koishi-core/src/session.ts index 42da40ac82..942a871643 100644 --- a/packages/koishi-core/src/session.ts +++ b/packages/koishi-core/src/session.ts @@ -180,7 +180,7 @@ export class Session< })) } - private _getValue(source: T | ((session: Session) => T)): T { + resolveValue(source: T | ((session: Session) => T)): T { return typeof source === 'function' ? Reflect.apply(source, null, [this]) : source } @@ -219,7 +219,7 @@ export class Session< if (hasActiveCache) return this.channel = cache as any // 绑定一个新的可观测频道实例 - const assignee = this._getValue(this.app.options.autoAssign) ? this.selfId : '' + const assignee = this.resolveValue(this.app.options.autoAssign) ? this.selfId : '' const data = await this.getChannel(channelId, assignee, fieldArray) const newChannel = observe(data, diff => this.database.setChannel(platform, channelId, diff), `channel ${this.cid}`) this.app._channelCache.set(this.cid, newChannel) @@ -266,7 +266,7 @@ export class Session< // 确保匿名消息不会写回数据库 if (this.author?.anonymous) { const fallback = User.create(this.platform, userId) - fallback.authority = this._getValue(this.app.options.autoAuthorize) + fallback.authority = this.resolveValue(this.app.options.autoAuthorize) const user = observe(fallback, () => Promise.resolve()) return this.user = user } @@ -278,7 +278,7 @@ export class Session< if (hasActiveCache) return this.user = cache as any // 绑定一个新的可观测用户实例 - const data = await this.getUser(userId, this._getValue(this.app.options.autoAuthorize), fieldArray) + const data = await this.getUser(userId, this.resolveValue(this.app.options.autoAuthorize), fieldArray) const newUser = observe(data, diff => this.database.setUser(this.platform, userId, diff), `user ${this.uid}`) userCache.set(userId, newUser) return this.user = newUser