Skip to content

Commit

Permalink
feat(plugin-common): optimize subcommand help
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 22, 2020
1 parent d86c5c0 commit 09d9c75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
26 changes: 10 additions & 16 deletions packages/plugin-common/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function showGlobalShortcut (context: Context, meta: Meta<'message'>) {
return meta.$send(`当前可用的全局指令有:${shortcuts.join(',')}。`)
}

function getCommandList (context: Context, meta: Meta<'message'>, parent: Command, expand: boolean) {
function getCommandList (prefix: string, context: Context, meta: Meta<'message'>, parent: Command, expand: boolean) {
let commands = getCommands(context, meta, parent)
if (!expand) {
commands = commands.filter(cmd => cmd.parent === parent)
Expand All @@ -54,26 +54,23 @@ function getCommandList (context: Context, meta: Meta<'message'>, parent: Comman
|| cmd.children.find(cmd => cmd.name.includes('.', startPosition)))
})
}
let hasSubcommand = false
const output = commands.map(({ name, config, children }) => {
if (children.length) hasSubcommand = true
return ` ${name} (${config.authority}${children.length ? '*' : ''}) ${config.description}`
})
output.unshift(`${prefix}(括号内为对应的最低权限等级${hasSubcommand ? ',标有星号的表示含有子指令' : ''}):`)
if (expand) output.push('注:部分指令组已展开,故不再显示。')
return output
}

export const GLOBAL_HELP_PROLOGUE = '当前可用的指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):'
export const GLOBAL_HELP_EPILOGUE = [
'群聊普通指令可以通过“@我+指令名”的方式进行触发。',
'私聊或全局指令则不需要添加上述前缀,直接输入指令名即可触发。',
'输入“全局指令”查看全部可用的全局指令。',
'输入“帮助+指令名”查看特定指令的语法和使用示例。',
].join('\n')

function showGlobalHelp (context: Context, meta: Meta<'message'>, config: any) {
return meta.$send([
GLOBAL_HELP_PROLOGUE,
...getCommandList(context, meta, null, config.expand),
GLOBAL_HELP_EPILOGUE,
...getCommandList('当前可用的指令有', context, meta, null, config.expand),
'群聊普通指令可以通过“@我+指令名”的方式进行触发。',
'私聊或全局指令则不需要添加上述前缀,直接输入指令名即可触发。',
'输入“全局指令”查看全部可用的全局指令。',
'输入“帮助+指令名”查看特定指令的语法和使用示例。',
].join('\n'))
}

Expand Down Expand Up @@ -137,10 +134,7 @@ async function showCommandHelp (command: Command, meta: Meta<'message'>, config:
}

if (command.children.length) {
output.push(
'可用的子指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):',
...getCommandList(command.context, meta, command, config.expand),
)
output.push(...getCommandList('可用的子指令有', command.context, meta, command, config.expand))
}

return meta.$send(output.join('\n'))
Expand Down
10 changes: 6 additions & 4 deletions packages/plugin-common/tests/__snapshots__/help.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Array [
"bar.baz
command with alias and shortcut
中文别名:baz-alias。
相关全局指令:baz-shortcut。",
相关全局指令:baz-shortcut。
usage text",
]
`;

Expand All @@ -20,7 +21,8 @@ Array [
"bar.baz
command with alias and shortcut
中文别名:baz-alias。
相关全局指令:baz-shortcut。",
相关全局指令:baz-shortcut。
usage text",
]
`;

Expand Down Expand Up @@ -64,7 +66,7 @@ usage text
使用示例:
example 1
example 2
可用的子指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):
可用的子指令有(括号内为对应的最低权限等级):
bar.baz (1) command with alias and shortcut",
]
`;
Expand Down Expand Up @@ -101,7 +103,7 @@ Array [
可用的选项有:
-e, --expand 展开指令列表
-s, --shortcut 查看全局指令列表
可用的子指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):
可用的子指令有(括号内为对应的最低权限等级):
注:部分指令组已展开,故不再显示。",
]
`;
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-common/tests/help.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function prepare (app: MockedApp) {
.example('example 1')
.example('example 2')
app.command('bar.baz', 'command with alias and shortcut')
.usage(() => 'usage text')
.alias('baz-alias')
.shortcut('baz-shortcut')
}
Expand Down

0 comments on commit 09d9c75

Please sign in to comment.