V3版本App格式适配

This commit is contained in:
yoimiya-kokomi 2022-09-17 05:45:10 +08:00
parent 9b6122a25b
commit 97de796ea7
4 changed files with 45 additions and 19 deletions

View File

@ -10,7 +10,7 @@ const importV3 = async function (file, def, key = 'default') {
return def return def
} }
let MysInfo = await importV3('/plugins/genshin/model/mys/mysInfo.js') let MysInfo = await importV3('/plugins/genshin/model/mys/mysInfo.js', {})
let plugin = await importV3('lib/plugins/plugin.js', _plugin) let plugin = await importV3('lib/plugins/plugin.js', _plugin)
let puppeteer = _puppeteer let puppeteer = _puppeteer
export { export {

View File

@ -10,12 +10,12 @@ export const helpApp = help.v2()
export const statApp = stat.v2() export const statApp = stat.v2()
export const wikiApp = wiki.v2() export const wikiApp = wiki.v2()
let rule = { let apps = { character, admin, help, stat, wiki }
characterApp: character.rule(), let rule = {} // v2
adminApp: character.rule(), let rules = {} // v3
helpApp: help.rule(), for (let key in apps) {
statApp: stat.rule(), rule[`${key}App`] = apps[key].rule()
wikiApp: wiki.rule() rules[`${key}`] = apps[key].app()
} }
export { rule } export { rule, rules as apps }

View File

@ -1,4 +1,6 @@
import lodash from 'lodash'
import { plugin } from '../adapter/index.js' import { plugin } from '../adapter/index.js'
import { getMysApi, checkAuth } from '../adapter/mys.js'
class App { class App {
constructor (cfg) { constructor (cfg) {
@ -8,16 +10,16 @@ class App {
} }
reg (key, fn, cfg = {}) { reg (key, fn, cfg = {}) {
//TODO: 参数校验
this.apps[key] = { this.apps[key] = {
fn, fn,
...cfg ...cfg
} }
} }
getPlugins () { app () {
let cfg = this.cfg || {} let cfg = this.cfg || {}
let rule = this.getV3Rule() let rules = []
let cls = class extends plugin { let cls = class extends plugin {
constructor () { constructor () {
super({ super({
@ -25,14 +27,42 @@ class App {
desc: cfg.desc || '喵喵插件', desc: cfg.desc || '喵喵插件',
event: 'message', event: 'message',
priority: 50, priority: 50,
rule rule: rules
}) })
} }
}
async app (e) { for (let key in this.apps) {
let app = this.apps[key]
key = lodash.camelCase(key)
let rule = app.rule || app.reg || 'noCheck'
if (typeof (rule) === 'string') {
if (rule === '#poke#') {
continue
} else if (rule === 'noCheck') {
rule = '.+'
}
} else {
rule = lodash.trim(rule.toString(), '/')
}
rules.push({
reg: rule,
fnc: key
})
cls.prototype[key] = async function () {
let e = this.e
e.original_msg = e.original_msg || e.msg
e.checkAuth = e.checkAuth || async function (cfg) {
return await checkAuth(e, cfg)
}
e.getMysApi = e.getMysApi || async function (cfg) {
return await getMysApi(e, cfg)
}
return await app.fn.call(this, e)
} }
} }
cls.test = 1
return cls return cls
} }

View File

@ -1,11 +1,7 @@
import { Data, Version } from './components/index.js' import { Data, Version } from './components/index.js'
export * from './apps/index.js' export * from './apps/index.js'
let index = { miao: {} }
if (Version.isV3) {
index = await Data.importModule('adapter/v3-entrance.js')
}
export const miao = index.miao || {}
if (Bot?.logger?.info) { if (Bot?.logger?.info) {
Bot.logger.info('---------^_^---------') Bot.logger.info('---------^_^---------')
Bot.logger.info(`喵喵插件${Version.version}初始化~`) Bot.logger.info(`喵喵插件${Version.version}初始化~`)