Commit 3e057beb authored by nanahira's avatar nanahira

single picsource base plugin

parent 4d9ec83b
......@@ -52,6 +52,8 @@ export class PicSource {
#### 单图源
对于单图源的插件,我们提供了 `PicSourcePlugin` 基类,只需要集成该类即可快速开发单图源插件。
```ts
import { Context } from "koishi";
import { DefinePlugin, RegisterSchema, SchemaProperty, LifecycleEvents } from "koishi-thirdeye";
......@@ -65,25 +67,10 @@ export class Config extends PicSourceConfig {
@DefinePlugin({ name: 'my-picsource', schema: Config })
export default class MyPicSource extends PicSource implements LifecycleEvents {
constructor(ctx: Context, config: Partial<Config>) {
super(ctx);
}
@InjectConfig()
private config: Config;
@Inject(true)
private pics: PicsContainer;
export default class MyPicSource extends PicSourcePlugin<Config> {
async randomPic(tags: string[]) {
return { url: `https://cdn02.moecube.com:444/images/ygopro-images-zh-CN/${this.config.code}.jpg`, description: `${this.config.code}` };
}
onApply() {
this.config.applyTo(this);
this.pics.addSource(this);
}
}
```
......
This diff is collapsed.
......@@ -57,7 +57,7 @@
"ws": "^8.3.0"
},
"peerDependencies": {
"koishi": "^4.6.0"
"koishi": "^4.6.1"
},
"jest": {
"moduleFileExtensions": [
......
// import 'source-map-support/register';
import {
Context,
Assets,
Awaitable,
Random,
Logger,
Bot,
remove,
} from 'koishi';
import {
PicMiddlewareConfig,
PicMiddlewareInfo,
PicSourceInfo,
PicsPluginConfig,
} from './config';
import { Context, Awaitable, Random, Logger, Bot, remove } from 'koishi';
import { PicSourceConfig, PicSourceInfo, PicsPluginConfig } from './config';
import _ from 'lodash';
import { segment, Quester } from 'koishi';
import {
BasePlugin,
Caller,
ClassType,
DefinePlugin,
Inject,
InjectConfig,
InjectLogger,
LifecycleEvents,
PartialDeep,
Provide,
} from 'koishi-thirdeye';
import { AxiosRequestConfig } from 'axios';
......@@ -77,6 +65,28 @@ export class PicSource implements PicSourceInfo {
}
}
export class PicSourcePlugin<
C extends PicSourceConfig = PicSourceConfig,
> extends PicSource {
constructor(ctx: Context, config: PartialDeep<C>) {
super(ctx);
}
@InjectConfig()
protected config: C;
@Inject(true)
protected pics: PicsContainer;
@InjectLogger()
protected logger: Logger;
onApply() {
this.config.applyTo(this);
this.pics.addSource(this);
}
}
@Provide('pics', { immediate: true })
@DefinePlugin({ name: 'pics', schema: PicsPluginConfig })
export default class PicsContainer
......
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