Commit d630cd10 authored by nanahira's avatar nanahira

Migrate schema to Config

parent ad258bf6
import 'source-map-support/register'; import 'source-map-support/register';
import { Context } from 'koishi'; import { Context } from 'koishi';
import { Config, MyPlugin } from './plugin'; import { PluginConfig, MyPlugin } from './plugin';
export { Config } from './plugin'; export * from './plugin';
export const name = 'fortune'; export const name = 'fortune';
const plugin = new MyPlugin(); const plugin = new MyPlugin();
export const schema = plugin.schema; export const Config = plugin.schema;
export function apply(ctx: Context, config: Config) { export function apply(ctx: Context, config: PluginConfig) {
ctx.plugin(plugin, config); ctx.plugin(plugin, config);
} }
...@@ -4,7 +4,7 @@ import { pickOne } from './random'; ...@@ -4,7 +4,7 @@ import { pickOne } from './random';
import moment from 'moment'; import moment from 'moment';
import Mustache from 'mustache'; import Mustache from 'mustache';
export interface Config { export interface PluginConfig {
header?: string; header?: string;
masterKey?: string; masterKey?: string;
results?: string[]; results?: string[];
...@@ -13,13 +13,13 @@ export interface Config { ...@@ -13,13 +13,13 @@ export interface Config {
export class MyPlugin { export class MyPlugin {
private useDatabase = false; private useDatabase = false;
private config: Config; private config: PluginConfig;
private ctx: Context; private ctx: Context;
name = 'fortune-main'; name = 'fortune-main';
private render(template: string, view: any) { private render(template: string, view: any) {
return Mustache.render(template, view, null, { escape: (v) => v }); return Mustache.render(template, view, null, { escape: (v) => v });
} }
schema: Schema<Config> = Schema.object({ schema: Schema<PluginConfig> = Schema.object({
header: Schema.string().description('占卜结果的标题,会出现在结果首部。'), header: Schema.string().description('占卜结果的标题,会出现在结果首部。'),
masterKey: Schema.string().description( masterKey: Schema.string().description(
'占卜随机密钥。占卜结果会由 **日期** **用户ID** **masterKey** 唯一确定。', '占卜随机密钥。占卜结果会由 **日期** **用户ID** **masterKey** 唯一确定。',
...@@ -49,7 +49,7 @@ export class MyPlugin { ...@@ -49,7 +49,7 @@ export class MyPlugin {
); );
return result; return result;
} }
apply(ctx: Context, config: Config) { apply(ctx: Context, config: PluginConfig) {
ctx.on('service', (name) => { ctx.on('service', (name) => {
if (this.config.useDatabase && name === 'database') this.useDatabase = !!ctx.database; if (this.config.useDatabase && name === 'database') this.useDatabase = !!ctx.database;
}); });
......
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