Commit 0f37cf82 authored by nanahira's avatar nanahira

add BasePlugin

parent 77ce2f6b
......@@ -5,7 +5,7 @@
## 安装
```shell
npm install koishi-thirdeye koishi@next
npm install koishi-thirdeye koishi
```
## 快速入门
......@@ -13,11 +13,11 @@ npm install koishi-thirdeye koishi@next
可以简单定义类以快速开发 Koishi 插件。
```ts
import { KoishiPlugin, DefineSchema, CommandUsage, PutOption, UseCommand, OnApply, KoaContext, UseMiddleware, UseEvent, Get } from 'koishi-thirdeye';
import { KoishiPlugin, SchemaProperty, CommandUsage, PutOption, UseCommand, OnApply, KoaContext, UseMiddleware, UseEvent, Get } from 'koishi-thirdeye';
import { Context, Session } from 'koishi';
export class MyPluginConfig {
@DefineSchema({ default: 'bar' })
@SchemaProperty({ default: 'bar' })
foo: string;
}
......@@ -65,6 +65,50 @@ export default class MyPlugin implements OnApply {
koishi-thirdeye 内建了 `schemastery-gen` 的支持。只需要导入这1个包即可。另外,系统会自动进行 `@RegisterSchema` 的配置描述的注册。
最基本的插件定义方式如下:
```ts
import { KoishiPlugin, SchemaProperty, InjectConfig } from 'koishi-thirdeye';
import { Context, Session } from 'koishi';
export class MyPluginConfig {
@SchemaProperty({ default: 'bar' })
foo: string;
}
@KoishiPlugin({ name: 'my-plugin', schema: MyPluginConfig })
export default class MyPlugin {
constructor(private ctx: Context, private config: Partial<MyPluginConfig>) {
}
@InjectConfig()
private config: Config;
}
```
### 插件基类
为了简化不必要的代码,在您的类没有其他继承的情况下,可以继承于 `BasePlugin<Config>` 类,以省去不必要的构造函数等声明。
您可以使用 `this.ctx` 以及 `this.config` 进行访问上下文对象以及插件配置。因此上面的例子可以简化为下面的代码:
> `@KoishiPlugin` 装饰器不可省略。
```ts
import { KoishiPlugin, SchemaProperty, BasePlugin } from 'koishi-thirdeye';
import { Context, Session } from 'koishi';
export class MyPluginConfig {
@SchemaProperty({ default: 'bar' })
foo: string;
}
@KoishiPlugin({ name: 'my-plugin', schema: MyPluginConfig })
export default class MyPlugin extends BasePlugin<MyPluginConfig> {
}
```
## API
### 注入
......
import 'reflect-metadata';
export * from './src/register';
export * from './src/decorators';
export * from './src/base-plugin';
export * from './src/def/interfaces';
export * from 'schemastery-gen';
import { Context } from 'koishi';
import { InjectConfig } from './decorators';
export class BasePlugin<C> {
constructor(protected ctx: Context, config: Partial<C>) {}
@InjectConfig()
protected config: C;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment