Skip to content

Commit

Permalink
feat(common): support switch command
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 5, 2021
1 parent 7b5948d commit 1620482
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/plugin-common/src/admin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isInteger, difference, observe, Time, enumKeys, Random, template } from 'koishi-utils'
import { isInteger, difference, observe, Time, enumKeys, Random, template, deduplicate } 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 @@ -361,6 +361,32 @@ export default function apply(ctx: Context, config: AdminConfig = {}) {
return template('admin.channel-updated')
})

ctx.command('channel/switch <command...>', '启用和禁用功能', { authority: 3 })
.channelFields(['disable'])
.userFields(['authority'])
.adminChannel(({ session, target: { disable } }, ...names: string[]) => {
if (!names.length) {
if (!disable.length) return '当前没有禁用功能。'
return '当前禁用的功能有:' + disable.join(', ') + '。'
}

names = deduplicate(names)
const forbidden = names.filter(name => {
const command = ctx.app._commandMap[name]
return command && command.config.authority >= session.user.authority
})
if (forbidden.length) return `您无权修改 ${forbidden.join(', ')} 功能。`

for (const name of names) {
const index = disable.indexOf(name)
if (index >= 0) {
disable.splice(index)
} else {
disable.push(name)
}
}
})

ctx.command('channel.flag [-s|-S] [...flags]', '标记信息', { authority: 3 })
.channelFields(['flag'])
.option('list', '-l 标记列表')
Expand Down
12 changes: 12 additions & 0 deletions packages/plugin-common/tests/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const session = app.session('123', '321')
app.plugin(common)
app.command('foo', { maxUsage: 10 }).action(() => 'bar')
app.command('bar', { minInterval: 1000 }).action(() => 'foo')
app.command('baz').action(() => 'zab')

declare module 'koishi-core' {
namespace User {
Expand Down Expand Up @@ -94,6 +95,17 @@ describe('Admin Commands', () => {
await session.shouldReply('assign -t #321 nan', '参数 bot 输入无效,请指定正确的目标。')
})

it('channel/switch', async () => {
await session.shouldReply('switch', '当前没有禁用功能。')
await session.shouldReply('baz', 'zab')
await session.shouldReply('switch baz', '频道数据已修改。')
await session.shouldReply('switch', '当前禁用的功能有:baz。')
await session.shouldNotReply('baz')
await session.shouldReply('switch baz', '频道数据已修改。')
await session.shouldReply('baz', 'zab')
await session.shouldReply('switch assign', '您无权修改 assign 功能。')
})

it('channel.flag', async () => {
await session.shouldReply('channel.flag', '未设置任何标记。')
await session.shouldReply('channel.flag -s foo', '未找到标记 foo。')
Expand Down

0 comments on commit 1620482

Please sign in to comment.