Skip to content

Commit

Permalink
feat(common): detailed hint for switch
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 13, 2021
1 parent 6ee80ba commit 389341f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions packages/plugin-common/src/admin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { difference, observe, Time, enumKeys, Random, template, deduplicate } from 'koishi-utils'
import { difference, observe, Time, enumKeys, Random, template, deduplicate, intersection } from 'koishi-utils'
import { Context, User, Channel, Command, Argv, Platform, Session } from 'koishi-core'

type AdminAction<U extends User.Field, G extends Channel.Field, A extends any[], O extends {}, T>
Expand Down Expand Up @@ -377,10 +377,10 @@ export default function apply(ctx: Context, config: AdminConfig = {}) {
ctx.command('channel/switch <command...>', '启用和禁用功能', { authority: 3 })
.channelFields(['disable'])
.userFields(['authority'])
.adminChannel(({ session, target: { disable } }, ...names: string[]) => {
.adminChannel(async ({ session, target }, ...names: string[]) => {
if (!names.length) {
if (!disable.length) return template('switch.none')
return template('switch.list', disable.join(', '))
if (!target.disable.length) return template('switch.none')
return template('switch.list', target.disable.join(', '))
}

names = deduplicate(names)
Expand All @@ -390,14 +390,15 @@ export default function apply(ctx: Context, config: AdminConfig = {}) {
})
if (forbidden.length) return template('switch.forbidden', forbidden.join(', '))

for (const name of names) {
const index = disable.indexOf(name)
if (index >= 0) {
disable.splice(index)
} else {
disable.push(name)
}
}
const add = difference(names, target.disable)
const remove = intersection(names, target.disable)
const preserve = difference(target.disable, names)
const output: string[] = []
if (add.length) output.push(`禁用 ${add.join(', ')} 功能`)
if (remove.length) output.push(`启用 ${remove.join(', ')} 功能`)
target.disable = [...preserve, ...add]
await target._update()
return `已${output.join(',')}。`
})

ctx.command('channel.flag [-s|-S] [...flags]', '标记信息', { authority: 3 })
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-common/tests/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ describe('Admin Commands', () => {
it('channel/switch', async () => {
await session.shouldReply('switch', '当前没有禁用功能。')
await session.shouldReply('baz', 'zab')
await session.shouldReply('switch baz', '频道数据已修改。')
await session.shouldReply('switch baz', '已禁用 baz 功能。')
await session.shouldReply('switch', '当前禁用的功能有:baz')
await session.shouldNotReply('baz')
await session.shouldReply('switch baz', '频道数据已修改。')
await session.shouldReply('switch baz', '已启用 baz 功能。')
await session.shouldReply('baz', 'zab')
await session.shouldReply('switch assign', '您无权修改 assign 功能。')
})
Expand Down

0 comments on commit 389341f

Please sign in to comment.