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'
|
2023-03-14 19:11:59 +00:00
|
|
|
|
import { Version } 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 = {}) {
|
2023-02-13 19:47:22 +00:00
|
|
|
|
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 = []
|
2022-09-30 12:20:04 +00:00
|
|
|
|
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',
|
2023-03-14 19:11:59 +00:00
|
|
|
|
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
|
2022-09-30 12:20:04 +00:00
|
|
|
|
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
|
|
|
|
|
2022-09-30 12:20:04 +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') {
|
2022-09-20 19:15:22 +00:00
|
|
|
|
if (e.target_id !== Bot.uin && !e.isPoke) {
|
2022-09-17 20:40:15 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
2022-09-18 20:54:01 +00:00
|
|
|
|
// group状态下,戳一戳的发起人是operator
|
2022-09-20 11:50:27 +00:00
|
|
|
|
if (e.user_id === Bot.uin) {
|
|
|
|
|
e.user_id = e.operator_id
|
|
|
|
|
}
|
2022-09-17 20:40:15 +00:00
|
|
|
|
}
|
|
|
|
|
e.isPoke = true
|
2022-09-18 20:54:01 +00:00
|
|
|
|
// 随便指定一个不太常见的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
|
|
|
|
}
|
2023-02-18 10:29: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 () {
|
2023-03-18 03:49:31 +00:00
|
|
|
|
if (!Version.isMiao && !app.yzCheck()) {
|
2023-02-18 10:29:09 +00:00
|
|
|
|
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 || '',
|
2023-03-14 19:11:59 +00:00
|
|
|
|
priority: cfg.priority || 50,
|
2022-09-16 20:37:09 +00:00
|
|
|
|
hashMark: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v2执行方法
|
2022-09-17 09:48:24 +00:00
|
|
|
|
v2App (e) {
|
2022-09-27 03:05:32 +00:00
|
|
|
|
let cfg = this.cfg || {}
|
|
|
|
|
let event = cfg.event
|
2022-09-16 20:37:09 +00:00
|
|
|
|
let apps = this.apps
|
|
|
|
|
return async function (e) {
|
2022-09-30 12:20:04 +00:00
|
|
|
|
e.original_msg = e.original_msg || e.msg
|
2022-09-16 20:37:09 +00:00
|
|
|
|
let msg = e.original_msg || e.msg || ''
|
2022-09-30 12:20:04 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-02-18 10:29:09 +00:00
|
|
|
|
} 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
|
|
|
|
}
|
2022-09-27 03:05:32 +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
|