Skip to content

Commit

Permalink
feat(common): add feedback command
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 10, 2020
1 parent 831fe19 commit ced29c3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/plugin-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Context } from 'koishi-core'
import { DebugOptions } from './debug'
import repeater, { RepeaterOptions } from './repeater'
import handler, { HandlerOptions } from './handler'
import sender from './sender'
import sender, { SenderConfig } from './sender'

export * from './admin'
export * from './info'
export * from './repeater'

export interface Config extends HandlerOptions, RepeaterOptions {
export interface Config extends HandlerOptions, RepeaterOptions, SenderConfig {
debug?: DebugOptions
}

Expand All @@ -20,6 +20,6 @@ export function apply(ctx: Context, config: Config = {}) {
ctx.plugin(sender, config)

ctx.plugin(require('./admin'))
if (config.debug) ctx.plugin(require('./debug'), config.debug)
ctx.plugin(require('./debug'), config.debug)
ctx.plugin(require('./info'))
}
27 changes: 26 additions & 1 deletion packages/plugin-common/src/sender.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Context, getTargetId, Group, Session, User } from 'koishi-core'
import { CQCode } from 'koishi-utils'

export default function apply(ctx: Context) {
export interface SenderConfig {
operator?: number
}

export default function apply(ctx: Context, config: SenderConfig = {}) {
ctx.command('broadcast <message...>', '全服广播', { authority: 4 })
.before(session => !session.$app.database)
.option('forced', '-f 无视 silent 标签进行广播')
Expand Down Expand Up @@ -40,6 +44,27 @@ export default function apply(ctx: Context) {
return message
})

const interactions: Record<number, number> = {}

config.operator && ctx.command('feedback', '发送反馈信息给作者')
.userFields(['name', 'id'])
.action(async ({ session }, text) => {
if (!text) return '请输入要发送的文本。'
const { $username: name, userId } = session
const nickname = name === '' + userId ? userId : `${name} (${userId})`
const message = `收到来自 ${nickname} 的反馈信息:\n${text}`
const id = await session.$bot.sendPrivateMsg(config.operator, message)
interactions[id] = userId
return '反馈信息发送成功!'
})

ctx.middleware((session, next) => {
const { $reply, $parsed } = session
const userId = interactions[$reply]
if (!$parsed || !userId) return next()
return session.$bot.sendPrivateMsg(userId, $parsed)
})

ctx.command('contextify <message...>', '在特定上下文中触发指令', { authority: 3 })
.alias('ctxf')
.userFields(['authority'])
Expand Down
20 changes: 19 additions & 1 deletion packages/plugin-common/tests/sender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const app = new App({ mockDatabase: true })
const session1 = app.session(123)
const session2 = app.session(123, 456)

app.plugin(common)
app.plugin(common, {
operator: 999,
})

app.command('show-context')
.userFields(['id'])
Expand Down Expand Up @@ -46,6 +48,22 @@ describe('Sender Commands', () => {
expect(sendGroupMsg.mock.calls).to.have.length(3)
})

it('feedback', async () => {
const sendPrivateMsg = app.bots[0].sendPrivateMsg = fn(async () => 1000)
await session1.shouldReply('feedback', '请输入要发送的文本。')
expect(sendPrivateMsg.mock.calls).to.have.length(0)
await session1.shouldReply('feedback foo', '反馈信息发送成功!')
expect(sendPrivateMsg.mock.calls).to.have.length(1)
expect(sendPrivateMsg.mock.calls).to.have.shape([[999, '收到来自 123 的反馈信息:\nfoo']])

sendPrivateMsg.mockClear()
await session1.shouldNotReply('bar')
expect(sendPrivateMsg.mock.calls).to.have.length(0)
await session1.shouldNotReply('[CQ:reply,id=1000] bar')
expect(sendPrivateMsg.mock.calls).to.have.length(1)
expect(sendPrivateMsg.mock.calls).to.have.shape([[123, 'bar']])
})

describe('Contextify', () => {
app.bots[0].getStrangerInfo = fn(async () => ({} as StrangerInfo))
app.bots[0].getGroupMemberInfo = fn(async () => ({} as GroupMemberInfo))
Expand Down

0 comments on commit ced29c3

Please sign in to comment.