Skip to content

Commit

Permalink
feat(plugin-common): support functional usage
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 18, 2020
1 parent 068135e commit 85cb06b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 3 additions & 2 deletions packages/koishi-core/src/command.ts
Expand Up @@ -23,6 +23,7 @@ export interface ParsedCommandLine extends Partial<ParsedLine> {
}

export type UserType <T> = T | ((user: UserData) => T)
export type CommandUsage = string | ((this: Command, meta: Meta) => string | Promise<string>)

export interface CommandConfig {
/** disallow unknown options */
Expand Down Expand Up @@ -80,7 +81,7 @@ export class Command {

_aliases: string[] = []
_options: CommandOption[] = []
_usage?: string
_usage?: CommandUsage
_examples: string[] = []
_shortcuts: Record<string, ShortcutConfig> = {}
_userFields = new Set<UserField>()
Expand Down Expand Up @@ -185,7 +186,7 @@ export class Command {
return this
}

usage (text: string) {
usage (text: CommandUsage) {
this._usage = text
return this
}
Expand Down
21 changes: 11 additions & 10 deletions packages/plugin-common/src/help.ts
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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),
)
}

Expand Down

0 comments on commit 85cb06b

Please sign in to comment.