miao-plugin/components/App.js

186 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-09-16 21:45:10 +00:00
import lodash from 'lodash'
2023-02-13 20:43:14 +00:00
import Plugin from './common/Plugin.js'
2022-09-16 20:37:09 +00:00
class App {
constructor (cfg) {
this.id = cfg.id
this.cfg = cfg
this.apps = {}
}
reg (key, fn, cfg = {}) {
if (lodash.isPlainObject(key)) {
lodash.forEach(key, (cfg, k) => {
this.reg(k, cfg.fn, cfg)
})
} else {
this.apps[key] = {
fn,
...cfg
}
2022-09-16 20:37:09 +00:00
}
}
2022-09-17 09:48:24 +00:00
// 获取v3执行方法
v3App () {
2022-09-16 20:37:09 +00:00
let cfg = this.cfg || {}
2022-09-16 21:45:10 +00:00
let rules = []
let check = []
2022-09-17 20:40:15 +00:00
let event = cfg.event
2023-02-13 20:43:14 +00:00
let cls = class extends Plugin {
2022-09-16 20:37:09 +00:00
constructor () {
super({
name: `喵喵:${cfg.name || cfg.id}`,
2022-09-17 20:40:15 +00:00
dsc: cfg.desc || cfg.name || '喵喵插件',
2022-09-18 04:05:37 +00:00
event: event === 'poke' ? 'notice.*.poke' : 'message',
2022-09-16 20:37:09 +00:00
priority: 50,
2022-09-16 21:45:10 +00:00
rule: rules
2022-09-16 20:37:09 +00:00
})
}
2022-09-17 20:40:15 +00:00
accept (e) {
e.original_msg = e.original_msg || e.msg
for (let idx = 0; idx < check.length; idx++) {
if (check[idx](e, e.original_msg) === true) {
return true
}
}
2022-09-17 20:40:15 +00:00
}
2022-09-16 21:45:10 +00:00
}
for (let key in this.apps) {
let app = this.apps[key]
key = lodash.camelCase(key)
let rule = app.rule || app.reg || 'noCheck'
2022-09-17 20:40:15 +00:00
if (event !== 'poke') {
if (typeof (rule) === 'string') {
if (rule === 'noCheck') {
rule = '.*'
}
} else {
rule = lodash.trim(rule.toString(), '/')
2022-09-16 21:45:10 +00:00
}
} else {
2022-09-17 20:40:15 +00:00
rule = '.*'
2022-09-16 21:45:10 +00:00
}
rules.push({
reg: rule,
fnc: key
})
2022-09-16 20:37:09 +00:00
if (app.check) {
check.push(app.check)
}
2022-09-16 21:45:10 +00:00
cls.prototype[key] = async function () {
let e = this.e
2022-09-17 20:40:15 +00:00
if (event === 'poke') {
if (e.notice_type === 'group') {
if (e.target_id !== Bot.uin && !e.isPoke) {
2022-09-17 20:40:15 +00:00
return false
}
// group状态下戳一戳的发起人是operator
if (e.user_id === Bot.uin) {
e.user_id = e.operator_id
}
2022-09-17 20:40:15 +00:00
}
e.isPoke = true
// 随便指定一个不太常见的msg以触发msg的正则
2022-09-17 20:40:15 +00:00
e.msg = '#poke#'
}
2022-09-16 21:45:10 +00:00
e.original_msg = e.original_msg || e.msg
return await app.fn.call(this, e)
2022-09-16 20:37:09 +00:00
}
if (app.yzRule && app.yzCheck) {
let yzKey = `Yz${key}`
let yzRule = lodash.trim(app.yzRule.toString(), '/')
rules.push({
reg: yzRule,
fnc: yzKey
})
cls.prototype[yzKey] = async function () {
if (!app.yzCheck()) {
return false
}
let e = this.e
e.original_msg = e.original_msg || e.msg
return await app.fn.call(this, e)
}
}
2022-09-16 20:37:09 +00:00
}
return cls
}
// 获取v2版rule
2022-09-17 09:48:24 +00:00
v2Rule () {
2022-09-16 20:37:09 +00:00
let cfg = this.cfg
return {
reg: 'noCheck',
describe: cfg.desc || '',
priority: 50,
hashMark: true
}
}
// v2执行方法
2022-09-17 09:48:24 +00:00
v2App (e) {
let cfg = this.cfg || {}
let event = cfg.event
2022-09-16 20:37:09 +00:00
let apps = this.apps
return async function (e) {
e.original_msg = e.original_msg || e.msg
2022-09-16 20:37:09 +00:00
let msg = e.original_msg || e.msg || ''
for (let key in apps) {
let app = apps[key]
if (app.check && app.check(e, msg) === true) {
break
}
}
msg = e.msg
2022-09-16 20:37:09 +00:00
for (let key in apps) {
let app = apps[key]
let rule = app.rule || app.reg || 'noCheck'
if (app.rule) {
if (typeof (rule) === 'string') {
if (rule === '#poke#') {
continue
} else if (rule === 'noCheck') {
rule = /.*/
}
rule = new RegExp(rule)
}
if (rule.test(msg)) {
let ret = await app.fn(e, {})
if (ret === true) {
return true
}
} else if (app.yzRule && app.yzCheck()) {
rule = new RegExp(app.yzRule)
if (rule.test(msg)) {
let ret = await app.fn(e, {})
if (ret === true) {
return true
}
}
2022-09-16 20:37:09 +00:00
}
} else if (event === 'poke' && msg === '#poke#') {
let ret = await app.fn(e, {})
if (ret === true) {
return true
}
2022-09-16 20:37:09 +00:00
}
}
return false
}
}
}
App.init = function (cfg) {
return new App(cfg)
}
export default App