Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 15, 2020
2 parents 350a952 + f2e3b8d commit e59a400
Show file tree
Hide file tree
Showing 33 changed files with 465 additions and 201 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ Koishi 在开发时借助了下面的工具:

| 特性 | [koishi<br>1.3.0](https://www.npmjs.com/package/koishi/v/1.3.0) | [cqhttp<br>1.2.0](https://www.npmjs.com/package/cqhttp/v/1.2.0) | [cq-websocket<br>2.0.2](https://www.npmjs.com/package/cq-websocket/v/2.0.2) | [lemon-bot<br>0.6.0](https://www.npmjs.com/package/lemon-bot/v/0.6.0) | [@ionjs/core<br>0.6.5](https://www.npmjs.com/package/@ionjs/core/v/0.6.5) |
|:--:|:--:|:--:|:--:|:--:|:--:|
| 依赖数量 | [22](http://npm.anvaka.com/#/view/2d/koishi/1.3.0) / [10](http://npm.anvaka.com/#/view/2d/koishi-core/1.3.0) | [62](http://npm.anvaka.com/#/view/2d/cqhttp/1.1.1) | [37](http://npm.anvaka.com/#/view/2d/cq-websocket/2.0.2) | [65](http://npm.anvaka.com/#/view/2d/lemon-bot/0.6.0) | [73](http://npm.anvaka.com/#/view/2d/%2540ionjs%252Fcore/0.6.5) |
| 依赖数量 | [10](http://npm.anvaka.com/#/view/2d/koishi-core/1.3.0) / [21](http://npm.anvaka.com/#/view/2d/koishi/1.3.0) | [62](http://npm.anvaka.com/#/view/2d/cqhttp/1.1.1) | [37](http://npm.anvaka.com/#/view/2d/cq-websocket/2.0.2) | [65](http://npm.anvaka.com/#/view/2d/lemon-bot/0.6.0) | [73](http://npm.anvaka.com/#/view/2d/%2540ionjs%252Fcore/0.6.5) |
| 安装体积 (MB) | [0.78](https://packagephobia.now.sh/[email protected]) / [2.14](https://packagephobia.now.sh/[email protected]) | [1.86](https://packagephobia.now.sh/[email protected]) | [2.82](https://packagephobia.now.sh/result?p=cq-websocket) | [2.56](https://packagephobia.now.sh/result?p=lemon-bot) | [2.32](https://packagephobia.now.sh/result?p=@ionjs/core) |
| HTTP | ✔️ | ✔️ || ✔️ | ✔️ |
| WebSocket | ✔️ || ✔️ |||
| 反向 WebSocket ||||||
Expand All @@ -78,7 +79,7 @@ Koishi 在开发时借助了下面的工具:
| 命令行 | ✔️ |||||
| 指令 | ✔️ ||| ✔️ ||

注:依赖数量如果表示为 X/Y,则 X 表示命令行工具依赖数量, Y 表示核心库依赖数量
注:依赖数量如果表示为 X/Y,则 X 表示核心库依赖数量,Y 表示命令行工具依赖数量

## 安装

Expand Down
1 change: 1 addition & 0 deletions build/bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function getPackage (name: string) {
|| packages[`packages/koishi-${name}`]
|| packages[`packages/database-${name}`]
|| packages[`packages/plugin-${name}`]
|| packages[`plugins/plugin-${name}`]
}

function each <T> (callback: (pkg: Package, name: string) => T) {
Expand Down
2 changes: 1 addition & 1 deletion build/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import spawn from 'cross-spawn'
const name = process.argv[2]
if (!name) process.exit(0)

spawn('npx', ['koishi', 'run', ...process.argv.slice(3)], {
spawn('npx', ['koishi', 'run', ...process.argv.slice(3), '--', '-r', 'ts-node/register'], {
cwd: resolve(__dirname, '../bots', name),
stdio: 'inherit',
})
6 changes: 3 additions & 3 deletions packages/database-level/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-database-level",
"description": "Leveldb support for Koishi",
"version": "1.0.4",
"version": "1.0.5",
"main": "dist/index.js",
"files": [
"dist"
Expand Down Expand Up @@ -33,12 +33,12 @@
"leveldb"
],
"devDependencies": {
"koishi-test-utils": "^1.0.3"
"koishi-test-utils": "^1.2.1"
},
"dependencies": {
"@types/leveldown": "^4.0.2",
"@types/levelup": "^4.3.0",
"koishi-core": "^1.3.0",
"koishi-core": "^1.3.1",
"koishi-utils": "^1.0.2",
"leveldown": "^5.4.1",
"levelup": "^4.3.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/database-mysql/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-database-mysql",
"description": "MySQL support for Koishi",
"version": "1.0.4",
"version": "1.0.5",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -35,7 +35,7 @@
"@types/mysql": "^2.15.8"
},
"dependencies": {
"koishi-core": "^1.3.0",
"koishi-core": "^1.3.1",
"koishi-utils": "^1.0.2",
"mysql": "^2.17.1"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/database-sqlite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koishi-database-sqlite",
"version": "1.0.0-alpha.0",
"version": "1.0.0-alpha.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand All @@ -22,10 +22,10 @@
"homepage": "https://github.com/koishijs/koishi/tree/master/packages/database-sqlite#readme",
"devDependencies": {
"@types/sqlite3": "^3.1.6",
"koishi-test-utils": "^1.1.0"
"koishi-test-utils": "^1.2.1"
},
"dependencies": {
"koishi-core": "^1.3.0",
"koishi-core": "^1.3.1",
"koishi-utils": "^1.0.2",
"sqlite3": "^4.1.1"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/database-sqlite/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare module 'koishi-core/dist/database' {
}

interface DatabaseConfig {
sqlite: SqliteConfig
sqlite?: SqliteConfig
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/koishi-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ Koishi 在开发时借助了下面的工具:

| 特性 | [koishi<br>1.3.0](https://www.npmjs.com/package/koishi/v/1.3.0) | [cqhttp<br>1.2.0](https://www.npmjs.com/package/cqhttp/v/1.2.0) | [cq-websocket<br>2.0.2](https://www.npmjs.com/package/cq-websocket/v/2.0.2) | [lemon-bot<br>0.6.0](https://www.npmjs.com/package/lemon-bot/v/0.6.0) | [@ionjs/core<br>0.6.5](https://www.npmjs.com/package/@ionjs/core/v/0.6.5) |
|:--:|:--:|:--:|:--:|:--:|:--:|
| 依赖数量 | [22](http://npm.anvaka.com/#/view/2d/koishi/1.3.0) / [10](http://npm.anvaka.com/#/view/2d/koishi-core/1.3.0) | [62](http://npm.anvaka.com/#/view/2d/cqhttp/1.1.1) | [37](http://npm.anvaka.com/#/view/2d/cq-websocket/2.0.2) | [65](http://npm.anvaka.com/#/view/2d/lemon-bot/0.6.0) | [73](http://npm.anvaka.com/#/view/2d/%2540ionjs%252Fcore/0.6.5) |
| 依赖数量 | [10](http://npm.anvaka.com/#/view/2d/koishi-core/1.3.0) / [21](http://npm.anvaka.com/#/view/2d/koishi/1.3.0) | [62](http://npm.anvaka.com/#/view/2d/cqhttp/1.1.1) | [37](http://npm.anvaka.com/#/view/2d/cq-websocket/2.0.2) | [65](http://npm.anvaka.com/#/view/2d/lemon-bot/0.6.0) | [73](http://npm.anvaka.com/#/view/2d/%2540ionjs%252Fcore/0.6.5) |
| 安装体积 (MB) | [0.78](https://packagephobia.now.sh/[email protected]) / [2.14](https://packagephobia.now.sh/[email protected]) | [1.86](https://packagephobia.now.sh/[email protected]) | [2.82](https://packagephobia.now.sh/result?p=cq-websocket) | [2.56](https://packagephobia.now.sh/result?p=lemon-bot) | [2.32](https://packagephobia.now.sh/result?p=@ionjs/core) |
| HTTP | ✔️ | ✔️ || ✔️ | ✔️ |
| WebSocket | ✔️ || ✔️ |||
| 反向 WebSocket ||||||
Expand All @@ -71,7 +72,7 @@ Koishi 在开发时借助了下面的工具:
| 命令行 | ✔️ |||||
| 指令 | ✔️ ||| ✔️ ||

注:依赖数量如果表示为 X/Y,则 X 表示命令行工具依赖数量, Y 表示核心库依赖数量
注:依赖数量如果表示为 X/Y,则 X 表示核心库依赖数量,Y 表示命令行工具依赖数量

## 安装

Expand Down
8 changes: 4 additions & 4 deletions packages/koishi-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi",
"description": "A QQ bot framework based on CQHTTP",
"version": "1.3.0",
"version": "1.3.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -40,9 +40,9 @@
"cac": "^6.5.4",
"js-yaml": "^3.13.1",
"kleur": "^3.0.3",
"koishi-core": "^1.3.0",
"koishi-plugin-common": "^1.0.4",
"koishi-plugin-schedule": "^1.0.0",
"koishi-core": "^1.3.1",
"koishi-plugin-common": "^1.0.5",
"koishi-plugin-schedule": "^1.0.1",
"koishi-utils": "^1.0.2",
"prompts": "^2.3.0"
}
Expand Down
20 changes: 13 additions & 7 deletions packages/koishi-cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import CAC from 'cac/types/CAC'

process.env.KOISHI_START_TIME = '' + performance.now()

function createWorker () {
const child = fork(resolve(__dirname, 'worker'))
interface WorkerOptions {
'--'?: string[]
}

function createWorker (options: WorkerOptions) {
const child = fork(resolve(__dirname, 'worker'), [], {
execArgv: options['--'],
})
let started = false

child.on('message', (data: any) => {
Expand All @@ -28,20 +34,20 @@ function createWorker () {
} else {
logger.warn('an error was encounted. restarting...')
}
createWorker()
createWorker(options)
})
}

export default function (cli: CAC) {
cli.command('run [file]', 'start a koishi bot')
.alias('start')
.option('--log-level <level>', 'specify log level (default: 3)')
.option('--log-level <level>', 'specify log level (default: 2)')
.option('--silent', 'use log level 0 (print no message)')
.option('--debug', 'use log level 4 (print all messages)')
.option('--debug', 'use log level 3 (print all messages)')
.action((file, options) => {
let logLevel = options.logLevel
if (options.silent) logLevel = 0
if (options.debug) logLevel = 4
if (options.debug) logLevel = 3
if (logLevel !== undefined) {
if (!isInteger(logLevel) || logLevel < 0) {
logger.error('log level should be a positive integer.')
Expand All @@ -50,6 +56,6 @@ export default function (cli: CAC) {
process.env.KOISHI_LOG_LEVEL = '' + logLevel
}
process.env.KOISHI_CONFIG_FILE = file || ''
createWorker()
createWorker(options)
})
}
4 changes: 2 additions & 2 deletions packages/koishi-cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import kleur from 'kleur'

export namespace logger {
export function info (message: string, logLevel?: number) {
if (logLevel < 3) return
if (logLevel < 2) return
console.log(`${kleur.blue('info')}`, message)
}

Expand All @@ -22,7 +22,7 @@ export namespace logger {
}

export function debug (message: string, logLevel?: number) {
if (logLevel < 4) return
if (logLevel < 3) return
console.log(`${kleur.magenta('debug')}`, message)
}
}
10 changes: 4 additions & 6 deletions packages/koishi-cli/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,11 @@ process.on('unhandledRejection', (error) => {
})

appList.forEach((app) => {
const { logLevel = 0, logFilter = {} } = app.options as AppConfig
const { logLevel = 2, logFilter = {} } = app.options as AppConfig

for (const type of logTypes) {
app.receiver.on(`logger/${type}` as LogEvents, (scope, message) => {
logger[type](message, Math.min(logFilter[scope] ?? logLevel, baseLogLevel))
})
}
app.receiver.on('logger', (scope, message, type) => {
logger[type](message, Math.min(logFilter[scope] ?? logLevel, baseLogLevel))
})
})

startAll().catch((error) => {
Expand Down
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": "1.3.0",
"version": "1.3.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -35,7 +35,7 @@
"@types/debug": "^4.1.5",
"@types/ws": "^6.0.4",
"get-port": "^5.1.0",
"koishi-test-utils": "^1.0.3"
"koishi-test-utils": "^1.2.1"
},
"dependencies": {
"axios": "^0.19.1",
Expand Down
12 changes: 7 additions & 5 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { showSuggestions } from './utils'
import { Meta, MessageMeta } from './meta'
import { simplify, noop } from 'koishi-utils'
import { errors, messages } from './messages'
import { ParsedLine } from './parser'

export interface AppOptions {
port?: number
Expand Down Expand Up @@ -287,10 +288,11 @@ export class App extends Context {
if (prefix && !nickname) continue
if (!fuzzy && message !== name) continue
if (message.startsWith(name)) {
let _message = message.slice(name.length)
const _message = message.slice(name.length)
if (fuzzy && !nickname && _message.match(/^\S/)) continue
if (oneArg) _message = `'${_message.trim()}'`
const result = command.parse(_message)
const result: ParsedLine = oneArg
? { rest: '', options: {}, unknown: [], args: [_message.trim()] }
: command.parse(_message)
result.options = { ...options, ...result.options }
result.args.unshift(...args)
parsedArgv = { meta, command, ...result }
Expand All @@ -304,7 +306,7 @@ export class App extends Context {
// attach group data
const groupFields = new Set<GroupField>(['flag', 'assignee'])
this.receiver.emit('before-group', groupFields, parsedArgv || { meta })
const group = await this.database.observeGroup(meta.groupId, 0, Array.from(groupFields))
const group = await this.database.observeGroup(meta.groupId, Array.from(groupFields))
Object.defineProperty(meta, '$group', { value: group, writable: true })

// ignore some group calls
Expand All @@ -320,7 +322,7 @@ export class App extends Context {
// attach user data
const userFields = new Set<UserField>(['name', 'flag'])
this.receiver.emit('before-user', userFields, parsedArgv || { meta })
const user = await this.database.observeUser(meta.userId, 0, Array.from(userFields))
const user = await this.database.observeUser(meta.userId, Array.from(userFields))
Object.defineProperty(meta, '$user', { value: user, writable: true })

// ignore some user calls
Expand Down
2 changes: 2 additions & 0 deletions packages/koishi-core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export enum GroupFlag {
noEmit = 4,
}

export const groupFlags: (keyof typeof GroupFlag)[] = ['noCommand', 'noResponse', 'noEmit']

export type Group<K extends GroupField = GroupField> = Observed<Pick<GroupData, K | 'id'>>
export type GroupField = keyof GroupData
export const groupFields: GroupField[] = []
Expand Down
50 changes: 6 additions & 44 deletions packages/koishi-core/tests/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,22 @@ describe('Middleware API', () => {
})

test('middleware-1', async () => {
await app.receive({
...shared,
messageType: 'private',
subType: 'friend',
message: 'foo',
})

await app.receiveMessage('user', 'foo', 10000)
expect(flag.toString(2).split('').reverse().join('')).toBe('1101')
})

test('middleware-2', async () => {
await app.receive({
...shared,
messageType: 'group',
subType: 'normal',
message: 'bar',
groupId: 20000,
})

await app.receiveMessage('group', 'bar', 10000, 20000)
expect(flag.toString(2).split('').reverse().join('')).toBe('1011')
})

test('middleware-3', async () => {
await app.receive({
...shared,
messageType: 'private',
subType: 'friend',
message: 'baz',
})

await app.receiveMessage('user', 'baz', 10000)
expect(flag.toString(2).split('').reverse().join('')).toBe('1101011')
})

test('middleware-4', async () => {
await app.receive({
...shared,
messageType: 'group',
subType: 'normal',
message: 'baz',
groupId: 20000,
})

await app.receiveMessage('group', 'baz', 10000, 20000)
expect(flag.toString(2).split('').reverse().join('')).toBe('10111')
})
})
Expand All @@ -110,14 +84,7 @@ describe('runtime checks', () => {
next()
})

await app.receive({
...shared,
messageType: 'group',
subType: 'normal',
message: 'bar',
})

await sleep(0)
await app.receiveMessage('group', 'bar', 10000, 20000)

expect(errorCallback).toBeCalledTimes(1)
expect(errorCallback).toBeCalledWith(errors.ISOLATED_NEXT)
Expand All @@ -135,12 +102,7 @@ describe('runtime checks', () => {
throw new Error(errorMessage)
})

await app.receive({
...shared,
messageType: 'group',
subType: 'normal',
message: 'bar',
})
await app.receiveMessage('group', 'bar', 10000, 20000)

expect(errorCallback).toBeCalledTimes(1)
expect(errorCallback).toBeCalledWith(errorMessage)
Expand Down
Loading

0 comments on commit e59a400

Please sign in to comment.