fix: 优化解析
This commit is contained in:
parent
8fc6650510
commit
068b0e60b7
|
@ -1,4 +1,3 @@
|
|||
import fs from 'node:fs'
|
||||
import lodash from 'lodash'
|
||||
|
||||
/**
|
||||
|
@ -7,6 +6,48 @@ import lodash from 'lodash'
|
|||
class ListenerLoader {
|
||||
client = null
|
||||
|
||||
/**
|
||||
*
|
||||
* @param listener
|
||||
* @param File
|
||||
* @returns
|
||||
*/
|
||||
init = (listener, File: string) => {
|
||||
try {
|
||||
if (!listener.default) return
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
listener = new listener.default()
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
listener.client = this.client
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const on = listener.once ? 'once' : 'on'
|
||||
|
||||
if (lodash.isArray(listener.event)) {
|
||||
listener.event.forEach(type => {
|
||||
const e = listener[type] ? type : 'execute'
|
||||
this.client[on](listener.prefix + type, event => listener[e](event))
|
||||
})
|
||||
} else {
|
||||
const e = listener[listener.event] ? listener.event : 'execute'
|
||||
this.client[on](listener.prefix + listener.event, event =>
|
||||
listener[e](event)
|
||||
)
|
||||
}
|
||||
} catch (e) {
|
||||
logger.mark(`监听事件错误:${File}`)
|
||||
logger.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听事件加载
|
||||
* @param client Bot示例
|
||||
|
@ -14,36 +55,20 @@ class ListenerLoader {
|
|||
async load(client) {
|
||||
this.client = client
|
||||
|
||||
const files = fs
|
||||
.readdirSync('./src/core/events')
|
||||
.filter(file => /(.ts|.js)$/.test(file))
|
||||
/**
|
||||
* ****************
|
||||
* 不可以加载未知代码
|
||||
* *****************
|
||||
* 防止被代码植入
|
||||
*/
|
||||
|
||||
for (let File of files) {
|
||||
try {
|
||||
let listener = await import(`./events/${File}`)
|
||||
this.init(await import('./events/login.js'), './events/login.js')
|
||||
this.init(await import('./events/message.js'), './events/message.js')
|
||||
this.init(await import('./events/notice.js'), './events/notice.js')
|
||||
this.init(await import('./events/offline.js'), './events/offline.js')
|
||||
this.init(await import('./events/online.js'), './events/online.js')
|
||||
this.init(await import('./events/request.js'), './events/request.js')
|
||||
|
||||
/* eslint-disable new-cap */
|
||||
if (!listener.default) continue
|
||||
listener = new listener.default()
|
||||
listener.client = this.client
|
||||
const on = listener.once ? 'once' : 'on'
|
||||
|
||||
if (lodash.isArray(listener.event)) {
|
||||
listener.event.forEach(type => {
|
||||
const e = listener[type] ? type : 'execute'
|
||||
this.client[on](listener.prefix + type, event => listener[e](event))
|
||||
})
|
||||
} else {
|
||||
const e = listener[listener.event] ? listener.event : 'execute'
|
||||
this.client[on](listener.prefix + listener.event, event =>
|
||||
listener[e](event)
|
||||
)
|
||||
}
|
||||
} catch (e) {
|
||||
logger.mark(`监听事件错误:${File}`)
|
||||
logger.error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -66,26 +66,19 @@ export class plugin {
|
|||
dsc && (this.dsc = dsc)
|
||||
event && (this.event = event)
|
||||
priority && (this.priority = priority)
|
||||
|
||||
/** 插件名称 */
|
||||
this.name = name
|
||||
/** 插件描述 */
|
||||
this.dsc = dsc
|
||||
/** 监听事件,默认message https://oicqjs.github.io/oicq/#events */
|
||||
this.event = event
|
||||
/** 优先级 */
|
||||
this.priority = priority
|
||||
|
||||
/** 定时任务,可以是数组 */
|
||||
this.task = {
|
||||
task && (this.task = {
|
||||
/** 任务名 */
|
||||
name: '',
|
||||
name: task?.name ?? '',
|
||||
/** 任务方法名 */
|
||||
fnc: task.fnc || '',
|
||||
fnc: task?.fnc ?? '',
|
||||
/** 任务cron表达式 */
|
||||
cron: task.cron || ''
|
||||
}
|
||||
cron: task?.cron ?? ''
|
||||
})
|
||||
|
||||
/** 命令规则 */
|
||||
this.rule = rule
|
||||
rule && (this.rule = rule)
|
||||
|
||||
if (handler) {
|
||||
this.handler = handler
|
||||
|
|
|
@ -643,8 +643,9 @@ class PluginsLoader {
|
|||
*/
|
||||
collectTask(task) {
|
||||
for (const i of Array.isArray(task) ? task : [task])
|
||||
if (i.cron && i.name)
|
||||
if (i?.cron && i?.name){
|
||||
this.task.push(i)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -652,7 +653,7 @@ class PluginsLoader {
|
|||
*/
|
||||
createTask() {
|
||||
for (const i of this.task)
|
||||
i.job = schedule.scheduleJob(i.cron, async () => {
|
||||
i.job = schedule.scheduleJob(i?.cron, async () => {
|
||||
try {
|
||||
if (i.log == true)
|
||||
logger.mark(`开始定时任务:${i.name}`)
|
||||
|
|
Loading…
Reference in New Issue