Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 29, 2020
2 parents 8a1bcf6 + 0e19aa9 commit 570c71a
Show file tree
Hide file tree
Showing 82 changed files with 561 additions and 570 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ rules:
yoda: off
standard/no-callback-literal: off
'@typescript-eslint/keyword-spacing': error
'@typescript-eslint/no-unused-vars': warn
'@typescript-eslint/space-before-function-paren':
- error
- anonymous: always
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,22 @@ MySQL 5.7 支持。

### [koishi-plugin-common](./packages/plugin-common) [![npm](https://img.shields.io/npm/v/koishi-plugin-common/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-common)

### [koishi-plugin-eval](./packages/plugin-eval) [![npm](https://img.shields.io/npm/v/koishi-plugin-eval/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-eval)
koishi-plugin-common 包含了一些常用功能,它们在你使用 koishi 库时是默认安装的。包含下列功能:

### [koishi-plugin-eval-addons](./packages/plugin-eval-addons) [![npm](https://img.shields.io/npm/v/koishi-plugin-eval-addons/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-eval-addons)
- 显示用户信息
- 管理用户和群数据
- 向一个或多个上下文发送消息
- 模拟来自其他会话的输入
- 输出聊天记录到控制台
- 欢迎入群,复读,处理申请,频率限制,自定义回复……

### [koishi-plugin-eval](https://koishi.js.org/plugins/eval.html) [![npm](https://img.shields.io/npm/v/koishi-plugin-eval/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-eval)

koishi-plugin-eval 允许用户直接使用机器人执行脚本。它利用了 Node.js 的 [vm](https://nodejs.org/api/vm.html)[worker_threads](https://nodejs.org/api/worker_threads.html) 模块,在保护执行安全的前提下能够获得较快的响应速度。同时,插件还提供了一些内置的 API 供用户调用,结合教学功能可以在客户端实现复杂的行为。

### [koishi-plugin-eval-addons](https://koishi.js.org/plugins/eval.html) [![npm](https://img.shields.io/npm/v/koishi-plugin-eval-addons/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-eval-addons)

koishi-plugin-eval-addons 在前一个插件的基础上,允许用户编写自己的模块并永久保存。插件将自动加载特定目录下的文件,并将其作为机器人的内置功能。用户可以利用此功能存储较为复杂的代码,甚至扩展新的指令。同时,如果上述目录是一个 git 目录,该插件也提供了自动更新等机制。

### [koishi-plugin-github](./packages/plugin-github) [![npm](https://img.shields.io/npm/v/koishi-plugin-github/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-github)

Expand Down
2 changes: 1 addition & 1 deletion build/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const prefixRegExp = new RegExp(`^(${prefixes.join('|')})(?:\\((\\S+)\\))?: (.+)
auth: GITHUB_TOKEN,
})

const { version } = require('../packages/koishi-cli/package') as PackageJson
const { version } = require('../packages/koishi/package') as PackageJson
const tags = spawnSync('git tag -l').split(/\r?\n/)
if (tags.includes(version)) {
return console.log(`Tag ${version} already exists.`)
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@octokit/rest": "^18.0.3",
"@octokit/rest": "^18.0.4",
"@types/chai": "^4.2.12",
"@types/chai-as-promised": "^7.1.3",
"@types/cross-spawn": "^6.0.2",
"@types/fs-extra": "^9.0.1",
"@types/mocha": "^8.0.3",
"@types/node": "^14.6.0",
"@types/node": "^14.6.1",
"@types/semver": "^7.3.3",
"@typescript-eslint/eslint-plugin": "^3.9.1",
"@typescript-eslint/parser": "^3.9.1",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"c8": "^7.3.0",
"cac": "^6.6.1",
"chai": "^4.2.0",
Expand All @@ -60,8 +60,8 @@
"jest-mock": "^26.3.0",
"kleur": "^4.1.1",
"latest-version": "^5.1.0",
"mocha": "^8.1.1",
"open": "^7.2.0",
"mocha": "^8.1.2",
"open": "^7.2.1",
"ora": "^5.0.0",
"p-map": "^4.0.0",
"prompts": "^2.3.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-cqhttp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^2.0.1"
"koishi-core": "^2.0.2"
},
"devDependencies": {
"@types/ms": "^0.7.31",
"@types/ws": "^7.2.6",
"get-port": "^5.1.1",
"koishi-test-utils": "^4.0.0-beta.9"
"koishi-test-utils": "^4.0.0"
},
"dependencies": {
"axios": "^0.20.0",
"ms": "^2.1.2",
"ws": "^7.3.1",
"koishi-utils": "^3.1.1"
"koishi-utils": "^3.1.2"
}
}
2 changes: 1 addition & 1 deletion packages/adapter-cqhttp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Context.prototype.broadcast = async function (this: Context, message, forced) {
let capture: RegExpExecArray
// eslint-disable-next-line no-cond-assign
while (capture = imageRE.exec(message)) {
const [text, _, url] = capture
const [text, , url] = capture
output += message.slice(0, capture.index)
message = message.slice(capture.index + text.length)
const { data } = await axios.get<ArrayBuffer>(url, { responseType: 'arraybuffer' })
Expand Down
1 change: 0 additions & 1 deletion packages/adapter-cqhttp/tests/mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { snakeCase } from 'koishi-utils'
import { CQResponse } from 'koishi-adapter-cqhttp'
import { expect } from 'chai'
import '@shigma/chai-extended'

export type RequestParams = Record<string, any>
export type RequestData = readonly [string, RequestParams]
Expand Down
25 changes: 0 additions & 25 deletions packages/chai-extended/package.json

This file was deleted.

43 changes: 0 additions & 43 deletions packages/chai-extended/src/index.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/koishi-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-core",
"description": "Core features for Koishi",
"version": "2.0.1",
"version": "2.0.2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down Expand Up @@ -36,15 +36,15 @@
"devDependencies": {
"@types/koa": "^2.11.4",
"@types/lru-cache": "^5.1.0",
"koishi-test-utils": "^4.0.0-beta.9"
"koishi-test-utils": "^4.0.0"
},
"dependencies": {
"@types/koa-bodyparser": "^4.3.0",
"@types/koa-router": "^7.4.1",
"koa": "^2.13.0",
"koa-bodyparser": "^4.3.0",
"koa-router": "^9.4.0",
"koishi-utils": "^3.1.1",
"koishi-utils": "^3.1.2",
"leven": "^3.1.0",
"lru-cache": "^6.0.0"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class App extends Context {
this._middlewareSet.add(counter)
const middlewares: Middleware[] = this._hooks[Context.MIDDLEWARE_EVENT as any]
.filter(([context]) => context.match(session))
.map(([_, middleware]) => middleware)
.map(([, middleware]) => middleware)

// execute middlewares
let index = 0, midStack = '', lastCall = ''
Expand Down Expand Up @@ -250,9 +250,9 @@ export class App extends Context {
await session.$group?._update()
}

private _parse(message: string, { $prefix, $appel, messageType }: Session, builtin: boolean, terminator = '') {
private _parse(message: string, { $reply, $prefix, $appel, messageType }: Session, builtin: boolean, terminator = '') {
// group message should have prefix or appel to be interpreted as a command call
if (builtin && messageType !== 'private' && $prefix === null && !$appel) return
if (builtin && ($reply || messageType !== 'private' && $prefix === null && !$appel)) return
terminator = escapeRegExp(terminator)
const name = message.split(new RegExp(`[\\s${terminator}]`), 1)[0]
const index = name.lastIndexOf('/')
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export namespace Group {

export type Field = keyof Group
export const fields: Field[] = []
export type Observed<K extends Field = Field> = utils.Observed<Pick<Group, K>>
export type Observed<K extends Field = Field> = utils.Observed<Pick<Group, K>, Promise<void>>
type Getter = (id: number, authority: number) => Partial<Group>
const getters: Getter[] = []

Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/plugins/help.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getUsage, getUsageName, ValidationField } from './validate'
import { User, Group, TableType, Tables } from '../database'
import { Command, FieldCollector, ParsedArgv } from '../command'
import { User, Group, TableType } from '../database'
import { Command, FieldCollector } from '../command'
import { Session } from '../session'
import { App } from '../app'
import { Message } from './message'
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/plugins/shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export default function apply(ctx: Context) {
}
})

ctx.on('parse', (message, { $prefix, $appel }, builtin) => {
if (!builtin || $prefix) return
ctx.on('parse', (message, { $reply, $prefix, $appel }, builtin) => {
if (!builtin || $prefix || $reply) return
for (const shortcut of ctx.app._shortcuts) {
const { name, fuzzy, command, oneArg, prefix, options, args = [] } = shortcut
if (prefix && !$appel) continue
Expand Down
55 changes: 41 additions & 14 deletions packages/koishi-core/tests/command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { App } from 'koishi-test-utils'
import { spyOn } from 'jest-mock'
import { Logger, noop } from 'koishi-utils'
import { Session } from 'koishi-core'
import { inspect } from 'util'
import { expect } from 'chai'
import '@shigma/chai-extended'

describe('Command API', () => {
describe('Register Commands', () => {
Expand Down Expand Up @@ -126,29 +128,54 @@ describe('Command API', () => {
expect(() => app.command('c/b')).to.throw()
expect(() => app.command('a/d')).not.to.throw()
})
})

describe('Dispose Commands', () => {
const app = new App()
const foo = app.command('foo')
const bar = foo.subcommand('bar')
const test = bar.subcommand('test')
bar.alias('baz').shortcut('1')
test.alias('it').shortcut('2')
it('dispose commands', () => {
const app = new App()
const foo = app.command('foo')
const bar = foo.subcommand('bar')
const test = bar.subcommand('test')
bar.alias('baz').shortcut('1')
test.alias('it').shortcut('2')

it('before dispose', () => {
// don't forget help
expect(app._commands).to.have.length(4)
expect(app._shortcuts).to.have.length(3)
expect(foo.children).to.have.length(1)
})

it('after dispose', () => {
bar.dispose()
// don't forget help
expect(app._commands).to.have.length(2)
expect(app._shortcuts).to.have.length(1)
expect(foo.children).to.have.length(0)
})
})

describe('Error Handling', () => {
const app = new App()
const command = app.command('test')
const session = new Session(app, {})
const cmdWarn = spyOn(new Logger('command'), 'warn')
const next = fallback => fallback()
const argv = { command, session, next }

it('throw in action', async () => {
command.action(async ({ next }) => {
await next(noop)
throw new Error('message')
})

await expect(command.execute(argv)).to.be.fulfilled
expect(cmdWarn.mock.calls).to.have.length(1)
expect(cmdWarn.mock.calls[0][0]).to.match(/^executing command: test\nError: message/)
})

it('throw in next', async () => {
command.action(({ next }) => {
return next(() => {
throw new Error('message')
})
})

await expect(command.execute(argv)).to.be.rejectedWith('message')
expect(cmdWarn.mock.calls).to.have.length(1)
})
})
})
1 change: 0 additions & 1 deletion packages/koishi-core/tests/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Session } from 'koishi-core'
import { noop } from 'koishi-utils'
import { expect } from 'chai'
import { fn } from 'jest-mock'
import '@shigma/chai-extended'

const app = new App()
const groupSession = new Session(app, { userId: 123, groupId: 456, messageType: 'group' })
Expand Down
1 change: 0 additions & 1 deletion packages/koishi-core/tests/parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Command } from 'koishi-core'
import { App } from 'koishi-test-utils'
import { expect } from 'chai'
import '@shigma/chai-extended'

const app = new App()

Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/tests/runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('Runtime', () => {
await session4.shouldHaveNoReply('cmd2')
await session1.shouldHaveReply('-cmd2', 'cmd2:123')
await session4.shouldHaveReply('-cmd2', 'cmd2:123')
await session4.shouldHaveReply(`[CQ:reply,id=123][CQ:at,qq=${app.selfId}] cmd2`, 'cmd2:123')
await session4.shouldHaveNoReply(`[CQ:reply,id=123][CQ:at,qq=${app.selfId}] cmd2`)
})

it('single nickname', async () => {
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions packages/koishi-test-utils/chai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "chai-extended",
"private": true,
"main": "dist/index.js",
"typings": "index.d.ts",
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
"files": [
"index.d.ts",
"dist"
]
}
Loading

0 comments on commit 570c71a

Please sign in to comment.