Skip to content

Commit

Permalink
fix(rate-limit): --help is not a usage, fix #772
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 13, 2022
1 parent bf39c27 commit b07aba7
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
2 changes: 0 additions & 2 deletions build/.eslintrc.yml

This file was deleted.

2 changes: 2 additions & 0 deletions plugins/a11y/rate-limit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export function apply(ctx: Context) {
const { maxUsage, minInterval } = command.config
let shouldFetchUsage = !!(maxUsage || minInterval)
for (const { name, notUsage } of Object.values(command._options)) {
// --help is not a usage (#772)
if (name === 'help') continue
if (name in options && notUsage) shouldFetchUsage = false
}
if (shouldFetchUsage) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/a11y/rate-limit/src/locales/zh.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
internal:
usage-exhausted: 调用次数已达上限。
too-frequent: 调用过于频繁,请稍后再试。
option-not-usage: 不计入调用
option-not-usage: ' (不计入调用)'
command-max-usage: 已调用次数:{0}/{1}。
command-min-interval: 距离下次调用还需:{0}/{1} 秒。

Expand Down
7 changes: 3 additions & 4 deletions plugins/a11y/rate-limit/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { App, Time } from 'koishi'
import mock from '@koishijs/plugin-mock'
import memory from '@koishijs/plugin-database-memory'
import * as help from '@koishijs/plugin-help'
import * as admin from '@koishijs/plugin-admin'
import * as rate from '@koishijs/plugin-rate-limit'
import { install } from '@sinonjs/fake-timers'

Expand All @@ -12,7 +11,6 @@ let now = Date.now()
app.plugin(help)
app.plugin(mock)
app.plugin(memory)
app.plugin(admin)
app.plugin(rate)

const client1 = app.mock.client('123')
Expand All @@ -35,12 +33,13 @@ describe('@koishijs/plugin-rate-limit', () => {
.action(() => 'test')

it('Extended Help', async () => {
await client1.shouldReply('help foo', [
await client1.shouldReply('help foo -H', [
'foo',
'指令1',
'已调用次数:1/3。',
'可用的选项有:',
' --opt1 选项1(不计入调用)',
' -h, --help 显示此信息 (不计入调用)',
' --opt1 选项1 (不计入调用)',
' --opt2 选项2',
].join('\n'))
})
Expand Down
2 changes: 2 additions & 0 deletions plugins/common/help/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const Config: Schema<Config> = Schema.object({
export function enableHelp<U extends User.Field, G extends Channel.Field, A extends any[], O extends {}>(cmd: Command<U, G, A, O>) {
return cmd.option('help', '-h', {
hidden: true,
// @ts-ignore
notUsage: true,
descPath: 'commands.help.options.help',
})
}
Expand Down

0 comments on commit b07aba7

Please sign in to comment.