Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 17, 2021
2 parents 01d0562 + a86a74b commit 33f04ff
Show file tree
Hide file tree
Showing 65 changed files with 677 additions and 150 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ rules:
default-param-last: off
'@typescript-eslint/default-param-last': error

func-call-spacing: off
'@typescript-eslint/func-call-spacing': error

keyword-spacing: off
'@typescript-eslint/keyword-spacing': error

Expand Down
2 changes: 2 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ for (const name of readdirSync(__dirname + '/packages')) {
const specs = [
'packages/koishi-core/tests/*.spec.ts',
'packages/koishi-utils/tests/*.spec.ts',
'packages/koishi-dev-utils/tests/*.spec.ts',
'packages/koishi-test-utils/tests/*.spec.ts',
'packages/plugin-common/tests/*.spec.ts',
'packages/plugin-eval/tests/*.spec.ts',
Expand All @@ -41,5 +42,6 @@ function getSpecFromArgv() {

module.exports = {
exit: true,
timeout: 5000,
spec: getSpecFromArgv(),
}
4 changes: 2 additions & 2 deletions build/publish.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cwd, PackageJson, getWorkspaces, spawnAsync, spawnSync } from './utils'
import { gt, prerelease } from 'semver'
import { gt, maxSatisfying, prerelease } from 'semver'
import { Octokit } from '@octokit/rest'
import { draft } from './release'
import { writeJson } from 'fs-extra'
Expand Down Expand Up @@ -79,7 +79,7 @@ if (CI && (GITHUB_REF !== 'refs/heads/master' || GITHUB_EVENT_NAME !== 'push'))
return console.log(`Tag ${version} already exists.`)
}

const body = draft(tags[tags.length - 1], bumpMap)
const body = draft(maxSatisfying(tags, '*'), bumpMap)
console.log(body)

if (!GITHUB_TOKEN) return
Expand Down
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module.exports = {
'/guide/adapter.md',
'/guide/logger.md',
'/guide/unit-tests.md',
'/guide/decorator.md',
],
}],
'/api': [{
Expand Down
7 changes: 4 additions & 3 deletions docs/.vuepress/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@

::-webkit-scrollbar {
width: 6px;
height: 6px;}
height: 6px;
}

::-webkit-scrollbar-track-piece {
background-color: #eaecef;
background-color: var(--c-scroll-track);
}

::-webkit-scrollbar-thumb {
background-color: #5546a3;
background-color: var(--c-brand);
}

@media (max-width: 960px /* $MQMobile */) {
Expand Down
12 changes: 11 additions & 1 deletion docs/.vuepress/styles/palette.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
$accentColor: #5546a3;
:root {
--c-brand: #5546a3;
--c-brand-light: #473a88;
--c-scroll-track: #eaecef;
}

.dark:root {
--c-brand: #33c0f6;
--c-brand-light: #7289da;
--c-scroll-track: #151310;
}

$textShadow: 1px 1px 1px rgba(23, 31, 35, 0.5);
2 changes: 1 addition & 1 deletion docs/api/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ externalIcon: false

- **cli:** 修复了生成的配置文件中,含有特殊字符的插件名没有引号的问题 (#275) (=2badfdfb645f7d5f6d10d1fa3e8ed27bf8d0fd12)
- **cli:** 修复了当一个插件含有文件级命名子插件时,子插件重载可能失败的问题 (=3a0d25d372e966c1e1124615c17ea08caad301c0)
- **webui:** 补全了依赖库 (#277) (=4053909020a8d1827b2d8b6bee39bb4068aaf68c)
- **github:** 支持了 request review from team (=b60c8903f6b2de3bc7caaffe107628ae560eb862)
- **webui:** 补全了缺失的依赖库 (#277) (=4053909020a8d1827b2d8b6bee39bb4068aaf68c)

## [Koishi 3.11.1](https://github.com/koishijs/koishi/releases/tag/3.11.1)

Expand Down
119 changes: 119 additions & 0 deletions docs/guide/decorator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
sidebarDepth: 2
---

# 使用装饰器 <Badge text="beta" type="warning"/>

koishi-dev-utils 允许你使用装饰器开发 Koishi 插件。下面是一个最简单的例子:

```ts
import { Plugin, PluginContext } from 'koishi-dev-utils'

@Plugin('decorator-example')
class MyPlugin extends PluginContext {
@Middleware()
hello(session, next) {
return session.send('hello')
}

@Command('echo')
echo(_, text) {
return text
}
}

export = new MyPlugin()
```

它等价于下面的代码:

```ts
import { Context } from 'koishi-core'

export const name = 'decorator-example'

export function apply(ctx: Context) {
ctx.middleware((session, next) => {
return session.send('hello')
})

ctx.command('echo').action((_, text) => {
return text
})
}
```

## 插件上下文

在上面的例子中,我们使用 `PluginContext` 作为插件的基类。在这个类中你可以直接使用 `this` 作为插件的上下文;同时这个类的实例也正好是一个合法的插件。

特别地,插件上下文还允许带有一个类型参数,作为插件的配置项。你可以使用 `this.config` 访问到它:

```ts
interface Config {
text: string
}

@Plugin('welcome')
class WelcomePlugin extends PluginContext<Config> {
@Event('group-member-added')
welcome(session) {
session.send(this.config.text)
}
}

app.plugin(new WelcomePlugin(), { text: '欢迎新人' })
```

除去中间件、指令和事件以外,如果有其他需求也可以直接通过 `@Apply` 实现:

```ts
@Plugin()
class ApplyPlugin extends PluginContext {
@Apply
someWorks() {
// 比如这里还可以注册其他插件
this.plugin(new WelcomePlugin())
}

@Apply
otherWorks() {
// 你可以写多个 @Apply 方法,它们都会被按顺序执行
}
}
```

## 使用选择器

你也可以在插件上下文中使用选择器:

```ts
@Plugin()
class SelectorPlugin extends PluginContext {
@User('123')
@Middleware()
callback1() {}

@Select('database')
@Command()
callback2() {}

@Channel.Except('456', '789')
@Apply
callback3() {}
}
```

这里的每一个回调函数都有独立的上下文选择器,相当于下面的代码:

```ts
function apply(ctx) {
const ctx1 = ctx.user('123')
ctx1.middleware(callback1.bind(ctx1))

const ctx2 = ctx.select('database')
ctx2.command().action(callback2.bind(ctx2))

callback3.call(ctx.channel.except('456', '789'))
}
```
7 changes: 7 additions & 0 deletions docs/plugins/eval/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ evaluate 指令和插值所使用的 Loader。内置的可选值包括 default,

要在子线程执行的文件名的键值对。键表示你希望在报错信息中显示的模块名,值表示文件的实际路径。如果你要扩展 eval 插件在子线程的行为,你可能需要这个选项。

## serializer

- 类型: `'v8' | 'yaml'`
- 默认值: `'v8'`

要使用的序列化方法。此配置将会影响 [storage](./sandbox.md#storage) 能够支持的类型。

## inspect

- 类型: [`InspectOptions`](https://nodejs.org/api/util.html#util_util_formatwithoptions_inspectoptions_format_args)
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-discord/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-adapter-discord",
"description": "Discord adapter for Koishi",
"version": "1.1.3",
"version": "1.2.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand All @@ -28,7 +28,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.11.2"
"koishi-core": "^3.12.0"
},
"devDependencies": {
"@types/ws": "^7.4.2",
Expand Down
8 changes: 4 additions & 4 deletions packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ export class DiscordBot extends Bot<'discord'> {
return sentMessageId
}

async sendMessage(channelId: string, content: string) {
const session = this.createSession({ channelId, content })
async sendMessage(channelId: string, content: string, groupId?: string) {
const session = this.createSession({ channelId, content, groupId, subtype: groupId ? 'group' : 'private' })
if (await this.app.serial(session, 'before-send', session)) return

const chain = segment.parse(session.content)
Expand All @@ -155,10 +155,10 @@ export class DiscordBot extends Bot<'discord'> {
message_id: quote,
} : undefined

const sentMessageId = await this.sendFullMessage(`/channels/${channelId}/messages`, session.content, { message_reference })
session.messageId = await this.sendFullMessage(`/channels/${channelId}/messages`, session.content, { message_reference })

this.app.emit(session, 'send', session)
return session.messageId = sentMessageId
return session.messageId
}

async deleteMessage(channelId: string, messageId: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-kaiheila/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-adapter-kaiheila",
"description": "Kaiheila adapter for Koishi",
"version": "1.1.4",
"version": "1.2.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand All @@ -24,7 +24,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.11.2"
"koishi-core": "^3.12.0"
},
"devDependencies": {
"koishi-test-utils": "^6.0.0"
Expand Down
10 changes: 4 additions & 6 deletions packages/adapter-kaiheila/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,17 @@ export class KaiheilaBot extends Bot {
return result.data
}

private _prepareHandle(channelId: string, content: string): SendHandle {
private _prepareHandle(channelId: string, content: string, groupId: string): SendHandle {
let path: string
const params = {} as KHL.MessageParams
const session = this.createSession({ channelId, content })
const session = this.createSession({ channelId, content, groupId })
if (channelId.length > 30) {
params.chatCode = channelId
session.subtype = 'private'
path = '/user-chat/create-msg'
} else {
params.targetId = channelId
session.subtype = 'group'
// FIXME this is incorrect but to workarournd ctx.group()
session.groupId = 'unknown'
path = '/message/create'
}
return [path, params, session]
Expand Down Expand Up @@ -189,8 +187,8 @@ export class KaiheilaBot extends Bot {
await flush()
}

async sendMessage(channelId: string, content: string) {
const handle = this._prepareHandle(channelId, content)
async sendMessage(channelId: string, content: string, groupId?: string) {
const handle = this._prepareHandle(channelId, content, groupId)
const [, params, session] = handle
if (await this.app.serial(session, 'before-send', session)) return

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-onebot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.11.2"
"koishi-core": "^3.12.0"
},
"devDependencies": {
"@types/ws": "^7.4.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-telegram/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.11.2"
"koishi-core": "^3.12.0"
},
"devDependencies": {
"koishi-test-utils": "^6.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-tomon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.11.2"
"koishi-core": "^3.12.0"
},
"devDependencies": {
"@types/pako": "^1.0.1",
Expand Down
3 changes: 1 addition & 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": "3.11.2",
"version": "3.12.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"engines": {
Expand Down Expand Up @@ -35,7 +35,6 @@
"dependencies": {
"@koa/router": "^10.0.0",
"@types/koa__router": "^8.0.4",
"@types/koa-bodyparser": "^4.3.0",
"@types/lru-cache": "^5.1.0",
"@types/ws": "^7.4.2",
"axios": "^0.21.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export namespace Adapter {
const callback = typeof target === 'string' ? () => target : target
return class {
constructor(app: App, bot: BotOptions) {
logger.info('infer type as %c', bot.type = callback(bot))
logger.debug('infer type as %c', bot.type = callback(bot))
return from(app, bot)
}
} as Constructor
Expand Down Expand Up @@ -183,7 +183,7 @@ export interface Bot<P = Platform> extends BotOptions, UserBase {
getStatus(): Promise<Bot.Status>

// message
sendMessage(channelId: string, content: string): Promise<string>
sendMessage(channelId: string, content: string, groupId?: string): Promise<string>
sendPrivateMessage(userId: string, content: string): Promise<string>
getMessage(channelId: string, messageId: string): Promise<MessageInfo>
editMessage(channelId: string, messageId: string, content: string): Promise<void>
Expand Down Expand Up @@ -260,7 +260,7 @@ export class Bot<P extends Platform> {
for (let index = 0; index < channels.length; index++) {
if (index && delay) await sleep(delay)
try {
messageIds.push(await this.sendMessage(channels[index], content))
messageIds.push(await this.sendMessage(channels[index], content, 'unknown'))
} catch (error) {
this.app.logger('bot').warn(error)
}
Expand Down
Loading

0 comments on commit 33f04ff

Please sign in to comment.