-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
677 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.