Skip to content

Commit

Permalink
feat(core): support help / rate-limit command config injection
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 20, 2023
1 parent 47fa5aa commit b150279
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
5 changes: 1 addition & 4 deletions packages/core/src/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,6 @@ export namespace Command {
}

export const Config: Schema<Config> = 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),
})
}
2 changes: 2 additions & 0 deletions packages/core/src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export class Commander {
return session.execute(message, next)
})
})

ctx.schema.extend('command', Command.Config, 1000)
}

private _resolvePrefixes(session: Session) {
Expand Down
16 changes: 12 additions & 4 deletions plugins/common/help/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -16,13 +16,13 @@ declare module 'koishi' {
/** hide all options by default */
hideOptions?: boolean
/** hide command */
hidden?: boolean
hidden?: Computed<boolean>
}
}

namespace Argv {
interface OptionConfig {
hidden?: boolean | ((session: Session) => boolean)
hidden?: Computed<boolean>
}
}
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -155,7 +163,7 @@ export function apply(ctx: Context, config: Config) {

function* getCommands(session: Session<'authority'>, commands: Command[], showHidden = false): Generator<Command> {
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 {
Expand Down
5 changes: 5 additions & 0 deletions plugins/common/rate-limit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b150279

Please sign in to comment.