fix: 优化解析
This commit is contained in:
		
							parent
							
								
									8fc6650510
								
							
						
					
					
						commit
						068b0e60b7
					
				|  | @ -1,4 +1,3 @@ | ||||||
| import fs from 'node:fs' |  | ||||||
| import lodash from 'lodash' | import lodash from 'lodash' | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  | @ -8,24 +7,28 @@ class ListenerLoader { | ||||||
|   client = null |   client = null | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * 监听事件加载 |    *  | ||||||
|    * @param client Bot示例 |    * @param listener  | ||||||
|  |    * @param File  | ||||||
|  |    * @returns  | ||||||
|    */ |    */ | ||||||
|   async load(client) { |   init = (listener, File: string) => { | ||||||
|     this.client = client |  | ||||||
| 
 |  | ||||||
|     const files = fs |  | ||||||
|       .readdirSync('./src/core/events') |  | ||||||
|       .filter(file => /(.ts|.js)$/.test(file)) |  | ||||||
| 
 |  | ||||||
|     for (let File of files) { |  | ||||||
|     try { |     try { | ||||||
|         let listener = await import(`./events/${File}`) |       if (!listener.default) return | ||||||
| 
 | 
 | ||||||
|         /* eslint-disable new-cap */ |       /** | ||||||
|         if (!listener.default) continue |         *  | ||||||
|  |         */ | ||||||
|       listener = new listener.default() |       listener = new listener.default() | ||||||
|  | 
 | ||||||
|  |       /** | ||||||
|  |        *  | ||||||
|  |        */ | ||||||
|       listener.client = this.client |       listener.client = this.client | ||||||
|  | 
 | ||||||
|  |       /** | ||||||
|  |        *  | ||||||
|  |        */ | ||||||
|       const on = listener.once ? 'once' : 'on' |       const on = listener.once ? 'once' : 'on' | ||||||
| 
 | 
 | ||||||
|       if (lodash.isArray(listener.event)) { |       if (lodash.isArray(listener.event)) { | ||||||
|  | @ -44,6 +47,28 @@ class ListenerLoader { | ||||||
|       logger.error(e) |       logger.error(e) | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 监听事件加载 | ||||||
|  |    * @param client Bot示例 | ||||||
|  |    */ | ||||||
|  |   async load(client) { | ||||||
|  |     this.client = client | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * **************** | ||||||
|  |      * 不可以加载未知代码 | ||||||
|  |      * ***************** | ||||||
|  |      * 防止被代码植入 | ||||||
|  |      */ | ||||||
|  | 
 | ||||||
|  |     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') | ||||||
|  | 
 | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -67,25 +67,18 @@ export class plugin { | ||||||
|     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 | ||||||
|  |  | ||||||
|  | @ -643,16 +643,17 @@ 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) | ||||||
|       } |       } | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * 创建定时任务 |    * 创建定时任务 | ||||||
|    */ |    */ | ||||||
|   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}`) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue