miao-plugin/components/App.js

134 lines
3.2 KiB
JavaScript
Raw Permalink 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'
import { Version, MiaoError } from '#miao'
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',
priority: cfg.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)
}
2023-07-03 18:31:22 +00:00
cls.prototype[key] = async function (e) {
e = this.e || e
const self_id = e.self_id || e.bot?.uin || Bot.uin
2022-09-17 20:40:15 +00:00
if (event === 'poke') {
if (e.notice_type === 'group') {
if (e.target_id !== self_id && !e.isPoke) {
2022-09-17 20:40:15 +00:00
return false
}
// group状态下戳一戳的发起人是operator
if (e.user_id === self_id) {
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
try {
return await app.fn.call(this, e)
} catch (err) {
if (err?.message && (err instanceof MiaoError)) {
// 处理 MiaoError
return e.reply(err.message)
} else {
// 其他错误抛出
throw err
}
}
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
})
2023-07-03 18:31:22 +00:00
cls.prototype[yzKey] = async function (e) {
if (!Version.isMiao && !app.yzCheck()) {
return false
}
2023-07-03 18:31:22 +00:00
e = this.e || 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
}
}
App.init = function (cfg) {
return new App(cfg)
}
export default App