From f3090bc8f4fbd22cbec728b6dac86213a322b61c Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sat, 12 Sep 2020 01:13:01 +0800 Subject: [PATCH] fix(common): fix feedback arg, remove deprecated blacklist --- packages/plugin-common/src/handler.ts | 10 +-------- packages/plugin-common/src/sender.ts | 2 +- packages/plugin-common/tests/handler.spec.ts | 21 ++++++++++++++++++- .../plugin-common/tests/respondent.spec.ts | 21 ------------------- 4 files changed, 22 insertions(+), 32 deletions(-) delete mode 100644 packages/plugin-common/tests/respondent.spec.ts diff --git a/packages/plugin-common/src/handler.ts b/packages/plugin-common/src/handler.ts index a0c4616a03..348fe9a10b 100644 --- a/packages/plugin-common/src/handler.ts +++ b/packages/plugin-common/src/handler.ts @@ -22,7 +22,6 @@ export interface HandlerOptions { onFriend?: RequestHandler onGroupAdd?: RequestHandler onGroupInvite?: RequestHandler - blackList?: string[] respondents?: Respondent[] throttle?: ThrottleConfig | ThrottleConfig[] welcome?: WelcomeMessage @@ -50,14 +49,7 @@ export default function apply(ctx: App, options: HandlerOptions = {}) { return result !== undefined && session.$bot.setGroupAddRequest(session.flag, session.subType, result) }) - const { blackList = [], respondents = [], welcome = defaultMessage } = options - - blackList.length && ctx.prependMiddleware((session, next) => { - for (const word of blackList) { - if (session.message.includes(word)) return - } - return next() - }) + const { respondents = [], welcome = defaultMessage } = options respondents.length && ctx.middleware((session, next) => { const message = simplify(session.message) diff --git a/packages/plugin-common/src/sender.ts b/packages/plugin-common/src/sender.ts index 6705b87cf7..6680c45d54 100644 --- a/packages/plugin-common/src/sender.ts +++ b/packages/plugin-common/src/sender.ts @@ -46,7 +46,7 @@ export default function apply(ctx: Context, config: SenderConfig = {}) { const interactions: Record = {} - config.operator && ctx.command('feedback', '发送反馈信息给作者') + config.operator && ctx.command('feedback ', '发送反馈信息给作者') .userFields(['name', 'id']) .action(async ({ session }, text) => { if (!text) return '请输入要发送的文本。' diff --git a/packages/plugin-common/tests/handler.spec.ts b/packages/plugin-common/tests/handler.spec.ts index 06f21c291c..8c2e278785 100644 --- a/packages/plugin-common/tests/handler.spec.ts +++ b/packages/plugin-common/tests/handler.spec.ts @@ -3,14 +3,27 @@ import { fn } from 'jest-mock' import { Meta } from 'koishi-core' import { App } from 'koishi-test-utils' import { sleep } from 'koishi-utils' +import {} from 'koishi-adapter-cqhttp' import * as common from 'koishi-plugin-common' const app = new App({ mockDatabase: true }) -const options: common.Config = {} + +const options: common.Config = { + respondents: [{ + match: '挖坑一时爽', + reply: '填坑火葬场', + }, { + match: /^(.+)一时爽$/, + reply: (_, action) => `一直${action}一直爽`, + }], +} + +const session = app.session(123) app.plugin(common, options) before(async () => { + await app.database.getUser(123, 3) await app.database.getGroup(123, app.selfId) }) @@ -125,6 +138,12 @@ describe('Common Handlers', () => { expect(setGroupAddRequest.mock.calls).to.have.shape([['flag', 'invite', true]]) }) + it('respondent', async () => { + await session.shouldReply('挖坑一时爽', '填坑火葬场') + await session.shouldReply('填坑一时爽', '一直填坑一直爽') + await session.shouldNotReply('填坑一直爽') + }) + it('welcome', async () => { sendGroupMsg.mockClear() await receiveGroupIncrease(321, 456) diff --git a/packages/plugin-common/tests/respondent.spec.ts b/packages/plugin-common/tests/respondent.spec.ts deleted file mode 100644 index 0696e8f908..0000000000 --- a/packages/plugin-common/tests/respondent.spec.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { MockedApp } from 'koishi-test-utils' -import { respondent } from '../src' - -const app = new MockedApp() -const session = app.session(123) - -// make coverage happy -app.plugin(respondent) -app.plugin(respondent, [{ - match: '挖坑一时爽', - reply: '填坑火葬场', -}, { - match: /^(.+)一时爽$/, - reply: (_, action) => `一直${action}一直爽`, -}]) - -it('basic support', async () => { - await session.shouldReply('挖坑一时爽', '填坑火葬场') - await session.shouldReply('填坑一时爽', '一直填坑一直爽') - await session.shouldNotReply('填坑一直爽') -})