Skip to content

Commit

Permalink
feat(core): support functional optionConfig.hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 31, 2021
1 parent a970d83 commit c72f607
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
9 changes: 7 additions & 2 deletions packages/koishi-core/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -111,12 +112,16 @@ function formatCommands(path: string, session: Session<ValidationField>, childre
return output
}

function getOptionVisibility(option: Domain.OptionConfig, session: Session<ValidationField>) {
if (session.user && option.authority > session.user.authority) return false
return !session.resolveValue(option.hidden)
}

function getOptions(command: Command, session: Session<ValidationField>, 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)
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class Session<
}))
}

private _getValue<T>(source: T | ((session: Session) => T)): T {
resolveValue<T>(source: T | ((session: Session) => T)): T {
return typeof source === 'function' ? Reflect.apply(source, null, [this]) : source
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit c72f607

Please sign in to comment.