Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 2, 2020
2 parents 5c07fb1 + fe7570f commit 89cda84
Show file tree
Hide file tree
Showing 44 changed files with 1,951 additions and 286 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ koishi run
- [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 使用的协议是 [MIT](https://choosealicense.com/licenses/mit/) 协议,但上面陈述的三种途径的相关框架都使用了基于 [AGPL 3.0](https://choosealicense.com/licenses/agpl-3.0/) 的协议。因此如果你使用 koishi-adapter-cqhttp 运行你的机器人,你将可能受到 AGPL 3.0 协议的限制,**必须将你的代码开源并保持同协议**。Koishi 及其作者对使用上述框架或违反上述限制的行为所可能造成的一切后果概不负责

## 数据库支持

Expand Down Expand Up @@ -97,7 +97,9 @@ koishi-plugin-eval 允许用户直接使用机器人执行脚本。它利用了

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)
### [koishi-plugin-github](./packages/plugin-github) [![npm](https://img.shields.io/npm/v/koishi-plugin-github?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-github)

koishi-plugin-github 提供了对 GitHub API 和 Webhooks 的全方位集成。它将允许机器人监听并群发目标仓库的更新,同时只需回复机器人的消息就能实现多种在 GitHub 网页中的操作。

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

Expand All @@ -123,6 +125,12 @@ koishi-plugin-schedule 允许用户设置定时任务并执行。这些计划任

## 协议

[MIT](./LICENSE)
[MIT](./LICENSE) 维护良好的开源生态从我做起 (*>ω<)φ

Copyright © 2019-present, Shigma

## 联系

[![QQ群](https://img.shields.io/badge/QQ%E7%BE%A4-963697928-blue.svg?style=flat-square)](https://jq.qq.com/?_wv=1027&k=89G3oKG0)

本群只交流程序开发,不欢迎伸手党,禁止谈论商业行为。
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
"license": "MIT",
"devDependencies": {
"@octokit/rest": "^18.0.4",
"@sinonjs/fake-timers": "^6.0.1",
"@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.1",
"@types/node": "^14.6.2",
"@types/semver": "^7.3.3",
"@types/sinonjs__fake-timers": "^6.0.1",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"c8": "^7.3.0",
Expand All @@ -48,9 +50,9 @@
"cross-env": "^7.0.2",
"cross-spawn": "^7.0.3",
"del": "^5.1.0",
"eslint": "^7.7.0",
"eslint": "^7.8.0",
"eslint-config-standard": "^14.1.1",
"eslint-import-resolver-typescript": "^2.2.1",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-mocha": "^8.0.0",
"eslint-plugin-node": "^11.1.0",
Expand All @@ -61,7 +63,7 @@
"jest-mock": "^26.3.0",
"kleur": "^4.1.1",
"latest-version": "^5.1.0",
"mocha": "^8.1.2",
"mocha": "^8.1.3",
"open": "^7.2.1",
"ora": "^5.0.0",
"p-map": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-cqhttp/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-adapter-cqhttp",
"description": "CQHTTP adapter for Koishi",
"version": "1.0.1",
"version": "1.0.2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -31,7 +31,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^2.1.0"
"koishi-core": "^2.1.1"
},
"devDependencies": {
"@types/ms": "^0.7.31",
Expand Down
10 changes: 5 additions & 5 deletions packages/adapter-cqhttp/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { camelCase, Logger, snakeCase, capitalize } from 'koishi-utils'
import { Bot, AccountInfo, SenderInfo, StatusInfo, StrangerInfo, BotStatus } from 'koishi-core'
import { Bot, AccountInfo, SenderInfo, StatusInfo, StrangerInfo, BotStatusCode } from 'koishi-core'

const logger = new Logger('bot')

Expand Down Expand Up @@ -248,13 +248,13 @@ Bot.prototype.getSelfId = async function getSelfId(this: Bot) {
return userId
}

Bot.prototype.getStatus = async function getStatus(this: Bot) {
if (!this.ready) return BotStatus.BOT_IDLE
Bot.prototype.getStatusCode = async function getStatusCode(this: Bot) {
if (!this.ready) return BotStatusCode.BOT_IDLE
try {
const data = await this.get<StatusInfo>('get_status')
return data.good ? BotStatus.GOOD : data.online ? BotStatus.SERVER_ERROR : BotStatus.BOT_OFFLINE
return data.good ? BotStatusCode.GOOD : data.online ? BotStatusCode.SERVER_ERROR : BotStatusCode.BOT_OFFLINE
} catch {
return BotStatus.NET_ERROR
return BotStatusCode.NET_ERROR
}
}

Expand Down
8 changes: 5 additions & 3 deletions packages/adapter-cqhttp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ Server.types.undefined = CQHTTP
const { broadcast } = Context.prototype
const imageRE = /\[CQ:image,file=([^,]+),url=([^\]]+)\]/

Context.prototype.broadcast = async function (this: Context, message, forced) {
Context.prototype.broadcast = async function (this: Context, ...args: any[]) {
const index = Array.isArray(args[0]) ? 1 : 0
let message = args[index] as string
let output = ''
let capture: RegExpExecArray
// eslint-disable-next-line no-cond-assign
Expand All @@ -63,8 +65,8 @@ Context.prototype.broadcast = async function (this: Context, message, forced) {
const { data } = await axios.get<ArrayBuffer>(url, { responseType: 'arraybuffer' })
output += `[CQ:image,file=base64://${Buffer.from(data).toString('base64')}]`
}
message = output + message
return broadcast.call(this, message, forced)
args[index] = output + message
return broadcast.apply(this, args)
}

Session.prototype.$send = async function $send(this: Session, message: string, autoEscape = false) {
Expand Down
2 changes: 1 addition & 1 deletion 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.1.0",
"version": "2.1.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class App extends Context {

let capture: RegExpMatchArray, atSelf = false
// eslint-disable-next-line no-cond-assign
if (capture = message.match(/^\[CQ:reply,id=(\d+)\]/)) {
if (capture = message.match(/^\[CQ:reply,id=(-?\d+)\]/)) {
session.$reply = +capture[1]
message = message.slice(capture[0].length)
}
Expand Down
8 changes: 6 additions & 2 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ function matchScope(base: ScopeSet, id: number) {
return !id || !(base.positive ^ base.includes(id))
}

function isBailed(value: any) {
return value !== null && value !== false && value !== undefined
}

export class Context {
static readonly MIDDLEWARE_EVENT = Symbol('mid')

Expand Down Expand Up @@ -140,7 +144,7 @@ export class Context {
for (const [context, callback] of this.app._hooks[name] || []) {
if (!context.match(session)) continue
const result = await callback.apply(this, args)
if (result !== undefined) return result
if (isBailed(result)) return result
}
}

Expand All @@ -153,7 +157,7 @@ export class Context {
for (const [context, callback] of this.app._hooks[name] || []) {
if (!context.match(session)) continue
const result = callback.apply(this, args)
if (result !== undefined) return result
if (isBailed(result)) return result
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/plugins/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ Session.prototype.$use = function $use(this: Session, middleware: Middleware) {
}

Session.prototype.$prompt = function $prompt(this: Session, timeout = this.$app.options.promptTimeout) {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
const dispose = this.$use((session) => {
clearTimeout(timer)
dispose()
resolve(session.message)
})
const timer = setTimeout(() => {
dispose()
reject(new Error('prompt timeout'))
resolve('')
}, timeout)
})
}
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export abstract class Server {
}
}

export enum BotStatus {
export enum BotStatusCode {
/** 正常运行 */
GOOD,
/** Bot 处于闲置状态 */
Expand All @@ -112,7 +112,7 @@ export interface Bot extends BotOptions {
ready?: boolean
version?: string
getSelfId(): Promise<number>
getStatus(): Promise<BotStatus>
getStatusCode(): Promise<BotStatusCode>
getMemberMap(groupId: number): Promise<Record<number, string>>
sendGroupMsg(groupId: number, message: string, autoEscape?: boolean): Promise<number>
sendPrivateMsg(userId: number, message: string, autoEscape?: boolean): Promise<number>
Expand Down
12 changes: 6 additions & 6 deletions packages/koishi-core/tests/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ describe('Session API', () => {
app.middleware(async (session, next) => {
if (session.message !== 'prompt') return next()
await session.$send('prompt text')
session.$prompt().then(
message => session.$send('received ' + message),
() => session.$send('received nothing'),
)
;(async () => {
const message = await session.$prompt() || 'nothing'
await session.$send('received ' + message)
})()
})

it('session.$prompt (resolved)', async () => {
it('session.$prompt 1', async () => {
await session.shouldHaveReply('prompt', 'prompt text')
await session.shouldHaveReply('foo', 'received foo')
await session.shouldHaveNoReply('foo')
})

it('session.$prompt (rejected)', async () => {
it('session.$prompt 2', async () => {
app.options.promptTimeout = 0
await session.shouldHaveReply('prompt', 'prompt text')
await sleep(0)
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"koishi-core": "^2.1.0",
"koishi-core": "^2.1.1",
"koishi-utils": "^3.1.3"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi/ecosystem.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"koishi-adapter-cqhttp": {
"version": "1.0.1",
"version": "1.0.2",
"description": "CQHTTP adapter for Koishi"
},
"koishi-plugin-chess": {
Expand All @@ -20,7 +20,7 @@
"description": "Execute JavaScript in Koishi"
},
"koishi-plugin-github": {
"version": "2.0.0-beta.10",
"version": "2.0.0",
"description": "GitHub webhook plugin for Koishi"
},
"koishi-plugin-image-search": {
Expand Down Expand Up @@ -51,11 +51,11 @@
"description": "Schedule plugin for Koishi"
},
"koishi-plugin-status": {
"version": "2.0.0-beta.13",
"version": "2.0.0-beta.14",
"description": "Show Status of Koishi"
},
"koishi-plugin-teach": {
"version": "1.0.2",
"version": "1.0.3",
"description": "Teach plugin for Koishi"
},
"koishi-plugin-tools": {
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi/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": "2.1.0",
"version": "2.1.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down Expand Up @@ -40,8 +40,8 @@
"dependencies": {
"cac": "^6.6.1",
"kleur": "^4.1.1",
"koishi-adapter-cqhttp": "^1.0.1",
"koishi-core": "^2.1.0",
"koishi-adapter-cqhttp": "^1.0.2",
"koishi-core": "^2.1.1",
"koishi-plugin-common": "^3.0.0-beta.15",
"prompts": "^2.3.2"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-chess/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"game"
],
"peerDependencies": {
"koishi-core": "^2.1.0",
"koishi-core": "^2.1.1",
"koishi-plugin-puppeteer": "^1.0.0"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"plugin"
],
"peerDependencies": {
"koishi-core": "^2.1.0"
"koishi-core": "^2.1.1"
},
"devDependencies": {
"koishi-test-utils": "^4.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-eval-addons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"code"
],
"peerDependencies": {
"koishi-core": "^2.1.0",
"koishi-core": "^2.1.1",
"koishi-plugin-eval": "^2.0.0"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-eval/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"code"
],
"peerDependencies": {
"koishi-core": "^2.1.0"
"koishi-core": "^2.1.1"
},
"devDependencies": {
"koishi-test-utils": "^4.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-eval/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ before(() => app.start())

after(() => app.stop())

describe('koishi-plugin-eval', () => {
describe('Plugin Eval', () => {
it('basic support', async () => {
await ses.shouldHaveReply('> 1+1', '2')
await ses.shouldHaveNoReply('>> 1+1')
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-github/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-github",
"description": "GitHub webhook plugin for Koishi",
"version": "2.0.0-beta.10",
"version": "2.0.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -36,10 +36,9 @@
"koishi-test-utils": "^4.0.0"
},
"peerDependencies": {
"koishi-core": "^2.1.0"
"koishi-core": "^2.1.1"
},
"dependencies": {
"@octokit/rest": "^18.0.4",
"@octokit/webhooks": "^7.11.2",
"axios": "^0.20.0",
"koishi-utils": "^3.1.3"
Expand Down
Loading

0 comments on commit 89cda84

Please sign in to comment.