fix: 优化解析

This commit is contained in:
ningmengchongshui 2024-06-09 23:18:36 +08:00
parent 8fc6650510
commit 068b0e60b7
3 changed files with 65 additions and 46 deletions

View File

@ -1,4 +1,3 @@
import fs from 'node:fs'
import lodash from 'lodash' import lodash from 'lodash'
/** /**
@ -7,6 +6,48 @@ import lodash from 'lodash'
class ListenerLoader { class ListenerLoader {
client = null 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示例 * @param client Bot示例
@ -14,36 +55,20 @@ class ListenerLoader {
async load(client) { async load(client) {
this.client = client this.client = client
const files = fs /**
.readdirSync('./src/core/events') * ****************
.filter(file => /(.ts|.js)$/.test(file)) *
* *****************
*
*/
for (let File of files) { this.init(await import('./events/login.js'), './events/login.js')
try { this.init(await import('./events/message.js'), './events/message.js')
let listener = await import(`./events/${File}`) 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)
}
}
} }
} }

View File

@ -66,26 +66,19 @@ export class plugin {
dsc && (this.dsc = dsc) dsc && (this.dsc = dsc)
event && (this.event = event) event && (this.event = event)
priority && (this.priority = priority) 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表达式 */
cron: task.cron || '' cron: task?.cron ?? ''
} })
/** 命令规则 */ /** 命令规则 */
this.rule = rule rule && (this.rule = rule)
if (handler) { if (handler) {
this.handler = handler this.handler = handler

View File

@ -643,8 +643,9 @@ class PluginsLoader {
*/ */
collectTask(task) { collectTask(task) {
for (const i of Array.isArray(task) ? task : [task]) for (const i of Array.isArray(task) ? task : [task])
if (i.cron && i.name) if (i?.cron && i?.name){
this.task.push(i) this.task.push(i)
}
} }
/** /**
@ -652,7 +653,7 @@ class PluginsLoader {
*/ */
createTask() { createTask() {
for (const i of this.task) for (const i of this.task)
i.job = schedule.scheduleJob(i.cron, async () => { i.job = schedule.scheduleJob(i?.cron, async () => {
try { try {
if (i.log == true) if (i.log == true)
logger.mark(`开始定时任务:${i.name}`) logger.mark(`开始定时任务:${i.name}`)