From ced29c3e0c2a7c60ed7575e346f60c5193420a90 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Thu, 10 Sep 2020 22:47:28 +0800 Subject: [PATCH] feat(common): add feedback command --- packages/plugin-common/src/index.ts | 6 ++--- packages/plugin-common/src/sender.ts | 27 ++++++++++++++++++++- packages/plugin-common/tests/sender.spec.ts | 20 ++++++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/packages/plugin-common/src/index.ts b/packages/plugin-common/src/index.ts index 7cdfce498d..8f043a7102 100644 --- a/packages/plugin-common/src/index.ts +++ b/packages/plugin-common/src/index.ts @@ -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 } @@ -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')) } diff --git a/packages/plugin-common/src/sender.ts b/packages/plugin-common/src/sender.ts index 2e2f6ab0f3..6705b87cf7 100644 --- a/packages/plugin-common/src/sender.ts +++ b/packages/plugin-common/src/sender.ts @@ -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 ', '全服广播', { authority: 4 }) .before(session => !session.$app.database) .option('forced', '-f 无视 silent 标签进行广播') @@ -40,6 +44,27 @@ export default function apply(ctx: Context) { return message }) + const interactions: Record = {} + + 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 ', '在特定上下文中触发指令', { authority: 3 }) .alias('ctxf') .userFields(['authority']) diff --git a/packages/plugin-common/tests/sender.spec.ts b/packages/plugin-common/tests/sender.spec.ts index 5da9a9cfdc..67da45de55 100644 --- a/packages/plugin-common/tests/sender.spec.ts +++ b/packages/plugin-common/tests/sender.spec.ts @@ -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']) @@ -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))