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 31, 2020
2 parents 570c71a + 5a5e39d commit 5c07fb1
Show file tree
Hide file tree
Showing 70 changed files with 1,669 additions and 643 deletions.
Binary file removed .github/wechat.png
Binary file not shown.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ temp

/addons
/atri
/coverage
/docs
/shiki
/coverage
/plugins
/test

todo.md
yarn.lock
Expand Down
1 change: 1 addition & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'packages/koishi-utils/tests/*.spec.ts',
'packages/koishi-test-utils/tests/*.spec.ts',
'packages/plugin-eval/tests/*.spec.ts',
'packages/plugin-github/tests/*.spec.ts',
'packages/plugin-teach/tests/*.spec.ts',
],
require: [
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Koishi 是一个在 [Node.js](https://nodejs.org/) 环境下运行的机器人

这个项目的名字和图标来源于东方 Project 中的角色古明地恋 (Komeiji Koishi)。

![demo](./.github/demo.png)
<div align="center">
<img src="./.github/demo.png" alt="demo" width="640">
</div>

## 安装

Expand Down Expand Up @@ -56,10 +58,12 @@ koishi run

[CQHTTP (OneBot)](https://github.com/howmanybots/onebot) 协议支持,可与下列实现该协议的框架完美对接:

- [richardchien/coolq-http-api](https://github.com/richardchien/coolq-http-api)
- [richardchien/coolq-http-api](https://github.com/richardchien/coolq-http-api)(可配合 [iTXTech/mirai-native](https://github.com/iTXTech/mirai-native) 使用)
- [Mrs4s/go-cqhttp](https://github.com/Mrs4s/go-cqhttp)
- [yyuueexxiinngg/cqhttp-mirai](https://github.com/yyuueexxiinngg/cqhttp-mirai)

请注意:尽管 Koishi 使用的协议是 [MIT](https://choosealicense.com/licenses/mit/),但上面陈述的三种途径的相关框架都使用了基于 [AGPL 3.0](https://choosealicense.com/licenses/agpl-3.0/) 的协议。因此如果你使用 koishi-adapter-cqhttp 运行你的机器人,你将可能受到 AGPL 3.0 协议的限制,**必须将你的代码开源并保持同协议**。Koishi 及其作者对使用上述框架或违反上述限制的行为所可能造成的法律后果概不负责。

## 数据库支持

### [koishi-plugin-mongo](./packages/plugin-mongo) [![npm](https://img.shields.io/npm/v/koishi-plugin-mongo?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-mongo)
Expand All @@ -85,7 +89,7 @@ koishi-plugin-common 包含了一些常用功能,它们在你使用 koishi 库
- 输出聊天记录到控制台
- 欢迎入群,复读,处理申请,频率限制,自定义回复……

### [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](https://koishi.js.org/plugins/eval.html) [![npm](https://img.shields.io/npm/v/koishi-plugin-eval?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 供用户调用,结合教学功能可以在客户端实现复杂的行为。

Expand All @@ -99,20 +103,26 @@ koishi-plugin-eval-addons 在前一个插件的基础上,允许用户编写自

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

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

### [koishi-plugin-recorder](./packages/plugin-recorder) [![npm](https://img.shields.io/npm/v/koishi-plugin-recorder/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-recorder)
koishi-plugin-puppeteer 本身提供了网页截图(shot)指令和 TeX 渲染指令(tex),同时也封装了一系列与网页进行交互的接口。利用这些接口我们可以开发更多以渲染图片为基础的插件,如 koishi-plugin-chess 等。

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

koishi-plugin-rss 提供了 [RSS](https://en.wikipedia.org/wiki/RSS) 支持,允许不同的群订阅不同的 RSS 信息源并实时进行通知。

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

koishi-plugin-schedule 允许用户设置定时任务并执行。这些计划任务会被存储在数据库中,即使重启机器人也能继续工作。

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

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

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

## 支持作者
## 协议

[MIT](./LICENSE)

<img src="./.github/wechat.png" alt="wechat" width="320">
Copyright © 2019-present, Shigma
11 changes: 10 additions & 1 deletion build/bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function bumpPkg(source: Package, flag: BumpType, only = false) {
const { name } = source
if (target.name === name) return
Object.entries({ devDependencies, peerDependencies, dependencies, optionalDependencies })
.filter(([_, dependencies = {}]) => dependencies[name])
.filter(([, dependencies = {}]) => dependencies[name])
.forEach(([type]) => {
target.meta[type][name] = '^' + newVersion
target.dirty = true
Expand Down Expand Up @@ -150,4 +150,13 @@ const flag = options.major ? 'major' : options.minor ? 'minor' : options.patch ?
}
return pkg.save()
}))

const ecosystem: Record<string, Pick<PackageJson, 'version' | 'description'>> = {}
for (const path in packages) {
if (!path.startsWith('packages/') || path.startsWith('packages/koishi')) continue
const { name, version, description } = packages[path].meta
ecosystem[name] = { version, description }
}

await writeJson(resolve(__dirname, '../packages/koishi/ecosystem.json'), ecosystem, { spaces: 2 })
})()
3 changes: 2 additions & 1 deletion build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type DependencyType = 'dependencies' | 'devDependencies' | 'peerDependenc

export interface PackageJson extends Partial<Record<DependencyType, Record<string, string>>> {
name?: string
description?: string
private?: boolean
version?: string
}
Expand All @@ -36,7 +37,7 @@ export function spawnSync(command: string, silent?: boolean) {
export function spawnAsync(command: string) {
const args = command.split(/\s+/)
const child = spawn(args[0], args.slice(1), { stdio: 'inherit' })
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
child.on('close', resolve)
})
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"workspaces": [
"atri",
"addons",
"test",
"shiki",
"shiki/core",
"shiki/cosmos",
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-cqhttp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^2.0.2"
"koishi-core": "^2.1.0"
},
"devDependencies": {
"@types/ms": "^0.7.31",
Expand All @@ -43,6 +43,6 @@
"axios": "^0.20.0",
"ms": "^2.1.2",
"ws": "^7.3.1",
"koishi-utils": "^3.1.2"
"koishi-utils": "^3.1.3"
}
}
4 changes: 2 additions & 2 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.2",
"version": "2.1.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down Expand Up @@ -44,7 +44,7 @@
"koa": "^2.13.0",
"koa-bodyparser": "^4.3.0",
"koa-router": "^9.4.0",
"koishi-utils": "^3.1.2",
"koishi-utils": "^3.1.3",
"leven": "^3.1.0",
"lru-cache": "^6.0.0"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class App extends Context {
_commands: Command[]
_commandMap: Record<string, Command>
_hooks: Record<keyof any, [Context, (...args: any[]) => any][]>
_userCache: LruCache<number, Observed<Partial<User>>>
_groupCache: LruCache<number, Observed<Partial<Group>>>
_userCache: LruCache<number, Observed<Partial<User>, Promise<void>>>
_groupCache: LruCache<number, Observed<Partial<Group>, Promise<void>>>

private _nameRE: RegExp
private _prefixRE: RegExp
Expand Down
2 changes: 2 additions & 0 deletions packages/koishi-core/tests/command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

import { App } from 'koishi-test-utils'
import { spyOn } from 'jest-mock'
import { Logger, noop } from 'koishi-utils'
Expand Down
2 changes: 2 additions & 0 deletions packages/koishi-core/tests/help.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

import { memory, App } from 'koishi-test-utils'
import { Time } from 'koishi-utils'
import { Message } from 'koishi-core'
Expand Down
30 changes: 29 additions & 1 deletion packages/koishi-core/tests/server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { App, BASE_SELF_ID, memory } from 'koishi-test-utils'
import { App as RealApp, Group, Session } from 'koishi-core'
import { App as RealApp, extendDatabase, Group, Session } from 'koishi-core'
import { expect } from 'chai'
import { fn, spyOn } from 'jest-mock'
import { Logger } from 'koishi-utils'

describe('Server API', () => {
describe('Adaptation API', () => {
Expand Down Expand Up @@ -96,4 +97,31 @@ describe('Server API', () => {
expect(sendGroupMsg.mock.calls).to.deep.equal([[123, 'foo'], [456, 'foo']])
})
})

describe('Database API', () => {
const app = new App()
const warn = spyOn(new Logger('app'), 'warn')

it('override database', () => {
warn.mockClear()
app.database = {} as any
expect(warn.mock.calls).to.have.length(0)
app.database = {} as any
expect(warn.mock.calls).to.have.length(1)
})

it('extend database', () => {
const callback = fn()
const id = 'this-module-does-not-exist'
extendDatabase(id, callback)
expect(callback.mock.calls).to.have.length(0)
class CustomDatabase {}
const module = require.cache[require.resolve('koishi-core/src/database.ts')]
const mockedRequire = spyOn(module, 'require')
mockedRequire.mockReturnValue({ default: CustomDatabase })
extendDatabase(id, callback)
expect(callback.mock.calls).to.deep.equal([[CustomDatabase]])
mockedRequire.mockRestore()
})
})
})
4 changes: 2 additions & 2 deletions packages/koishi-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"dependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"koishi-core": "^2.0.2",
"koishi-utils": "^3.1.2"
"koishi-core": "^2.1.0",
"koishi-utils": "^3.1.3"
},
"devDependencies": {
"@types/chai": "^4.2.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-utils",
"description": "Utilities for Koishi",
"version": "3.1.2",
"version": "3.1.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down
12 changes: 5 additions & 7 deletions packages/koishi-utils/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const instances: Record<string, Logger> = {}

type LogFunction = (format: any, ...param: any[]) => void

type LogType = 'success' | 'error' | 'info' | 'warn' | 'debug'

export interface Logger extends Record<LogType, LogFunction> {}

export class Logger {
static readonly SUCCESS = 1
static readonly ERROR = 1
Expand Down Expand Up @@ -44,12 +48,6 @@ export class Logger {
private code: number
private displayName: string

public success: LogFunction
public error: LogFunction
public info: LogFunction
public warn: LogFunction
public debug: LogFunction

constructor(private name: string, private showDiff = false) {
if (name in instances) return instances[name]
let hash = 0
Expand All @@ -71,7 +69,7 @@ export class Logger {
return Logger.color(this.code, value, decoration)
}

private createMethod(name: string, prefix: string, minLevel: number) {
private createMethod(name: LogType, prefix: string, minLevel: number) {
this[name] = (...args: [any, ...any[]]) => {
if (this.level < minLevel) return
process.stderr.write(prefix + this.displayName + this.format(...args) + '\n')
Expand Down
64 changes: 64 additions & 0 deletions packages/koishi/ecosystem.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"koishi-adapter-cqhttp": {
"version": "1.0.1",
"description": "CQHTTP adapter for Koishi"
},
"koishi-plugin-chess": {
"version": "2.0.0-beta.10",
"description": "Chess Plugin for Koishi"
},
"koishi-plugin-common": {
"version": "3.0.0-beta.15",
"description": "Common plugins for Koishi"
},
"koishi-plugin-eval": {
"version": "2.0.0",
"description": "Execute JavaScript in Koishi"
},
"koishi-plugin-eval-addons": {
"version": "1.0.0-beta.10",
"description": "Execute JavaScript in Koishi"
},
"koishi-plugin-github": {
"version": "2.0.0-beta.10",
"description": "GitHub webhook plugin for Koishi"
},
"koishi-plugin-image-search": {
"version": "2.0.0",
"description": "Image searching plugin for Koishi"
},
"koishi-plugin-mongo": {
"version": "1.0.2",
"description": "MongoDB support for Koishi"
},
"koishi-plugin-monitor": {
"version": "1.0.0-beta.14"
},
"koishi-plugin-mysql": {
"version": "2.0.0",
"description": "MySQL support for Koishi"
},
"koishi-plugin-puppeteer": {
"version": "1.0.0",
"description": "Take Screenshots in Koishi"
},
"koishi-plugin-rss": {
"version": "1.0.0",
"description": "Subscribe RSS Url for Koishi"
},
"koishi-plugin-schedule": {
"version": "2.0.1",
"description": "Schedule plugin for Koishi"
},
"koishi-plugin-status": {
"version": "2.0.0-beta.13",
"description": "Show Status of Koishi"
},
"koishi-plugin-teach": {
"version": "1.0.2",
"description": "Teach plugin for Koishi"
},
"koishi-plugin-tools": {
"version": "1.0.0"
}
}
14 changes: 4 additions & 10 deletions packages/koishi/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "koishi",
"description": "A QQ bot framework based on CQHTTP",
"version": "2.0.2",
"version": "2.1.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
"node": ">=12.0.0"
},
"files": [
"dist"
"dist",
"ecosystem.json"
],
"bin": "dist/cli.js",
"author": "Shigma <1700011071@pku.edu.cn>",
Expand All @@ -34,21 +35,14 @@
"koishi"
],
"devDependencies": {
"@types/js-yaml": "^3.12.5",
"@types/prompts": "^2.0.8"
},
"dependencies": {
"cac": "^6.6.1",
"kleur": "^4.1.1",
"koishi-core": "^2.0.2",
"koishi-adapter-cqhttp": "^1.0.1",
"koishi-core": "^2.1.0",
"koishi-plugin-common": "^3.0.0-beta.15",
"koishi-plugin-mysql": "^2.0.0",
"koishi-plugin-mongo": "^1.0.1",
"koishi-plugin-schedule": "^2.0.1",
"koishi-plugin-status": "^2.0.0-beta.13",
"koishi-plugin-teach": "^1.0.2",
"koishi-utils": "^3.1.2",
"prompts": "^2.3.2"
}
}
Loading

0 comments on commit 5c07fb1

Please sign in to comment.