update: 删除单例插件的加载
This commit is contained in:
parent
61163fbe30
commit
eb76158787
|
@ -25,4 +25,3 @@ module.exports = {
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
console.log('module.exports', module.exports)
|
|
||||||
|
|
|
@ -5,10 +5,11 @@ import schedule from 'node-schedule'
|
||||||
import { segment } from 'icqq'
|
import { segment } from 'icqq'
|
||||||
import chokidar from 'chokidar'
|
import chokidar from 'chokidar'
|
||||||
import moment from 'moment'
|
import moment from 'moment'
|
||||||
import path from 'node:path'
|
import path, { join } from 'node:path'
|
||||||
import Runtime from './plugins/runtime.js'
|
import Runtime from './plugins/runtime.js'
|
||||||
import Handler from './plugins/handler.js'
|
import Handler from './plugins/handler.js'
|
||||||
import { EventType } from './plugins/types.js'
|
import { EventType } from './plugins/types.js'
|
||||||
|
import { existsSync } from 'node:fs'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载插件
|
* 加载插件
|
||||||
|
@ -79,43 +80,30 @@ class PluginsLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 得到插件地址
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async getPlugins() {
|
#getPlugins = async () => {
|
||||||
|
// 便利得到目录和文件
|
||||||
const files = await fs.readdir(this.dir, { withFileTypes: true })
|
const files = await fs.readdir(this.dir, { withFileTypes: true })
|
||||||
const ret = []
|
const ret = []
|
||||||
for (const val of files) {
|
for (const val of files) {
|
||||||
|
// 是文件
|
||||||
if (val.isFile()) continue
|
if (val.isFile()) continue
|
||||||
const tmp = {
|
|
||||||
name: val.name,
|
|
||||||
path: `../../${this.dir}/${val.name}`
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const dir = `${this.dir}/${val.name}/index.js`
|
let dir = `${this.dir}/${val.name}/index.ts`
|
||||||
|
if (!existsSync(dir)) {
|
||||||
|
dir = `${this.dir}/${val.name}/index.js`
|
||||||
|
}
|
||||||
if (await fs.stat(dir)) {
|
if (await fs.stat(dir)) {
|
||||||
tmp.path = `${tmp.path}/index.js`
|
ret.push({
|
||||||
ret.push(tmp)
|
name: val.name,
|
||||||
|
path: dir
|
||||||
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
//
|
logger.error(err)
|
||||||
}
|
|
||||||
|
|
||||||
const apps = await fs.readdir(`${this.dir}/${val.name}`, {
|
|
||||||
withFileTypes: true
|
|
||||||
})
|
|
||||||
for (const app of apps) {
|
|
||||||
if (!app.isFile()) continue
|
|
||||||
// 解析js和ts
|
|
||||||
if (!/(.js|.ts)$/.test(app.name)) continue
|
|
||||||
ret.push({
|
|
||||||
name: `${tmp.name}/${app.name}`,
|
|
||||||
path: `${tmp.path}/${app.name}`
|
|
||||||
})
|
|
||||||
/** 监听热更新 */
|
|
||||||
this.watch(val.name, app.name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
|
@ -126,11 +114,15 @@ class PluginsLoader {
|
||||||
* @param isRefresh 是否刷新
|
* @param isRefresh 是否刷新
|
||||||
*/
|
*/
|
||||||
async load(isRefresh = false) {
|
async load(isRefresh = false) {
|
||||||
|
// 重置
|
||||||
this.delCount()
|
this.delCount()
|
||||||
|
// 累计
|
||||||
if (isRefresh) this.priority = []
|
if (isRefresh) this.priority = []
|
||||||
|
// 如果
|
||||||
if (this.priority.length) return
|
if (this.priority.length) return
|
||||||
|
|
||||||
const files = await this.getPlugins()
|
// 得到插件地址
|
||||||
|
const files = await this.#getPlugins()
|
||||||
|
|
||||||
logger.info('-----------')
|
logger.info('-----------')
|
||||||
logger.info('加载插件中...')
|
logger.info('加载插件中...')
|
||||||
|
@ -138,8 +130,9 @@ class PluginsLoader {
|
||||||
this.pluginCount = 0
|
this.pluginCount = 0
|
||||||
const packageErr = []
|
const packageErr = []
|
||||||
|
|
||||||
|
// 返回成功的
|
||||||
await Promise.allSettled(
|
await Promise.allSettled(
|
||||||
files.map(file => this.importPlugin(file, packageErr))
|
files.map(file => this.#importPlugin(file, packageErr))
|
||||||
)
|
)
|
||||||
|
|
||||||
this.packageTips(packageErr)
|
this.packageTips(packageErr)
|
||||||
|
@ -157,18 +150,18 @@ class PluginsLoader {
|
||||||
* @param file
|
* @param file
|
||||||
* @param packageErr
|
* @param packageErr
|
||||||
*/
|
*/
|
||||||
async importPlugin(file, packageErr?: any) {
|
#importPlugin = async (file, packageErr?: any) => {
|
||||||
try {
|
try {
|
||||||
let app = await import(file.path)
|
const app = await import(`file://${join(process.cwd(), file.path)}`)
|
||||||
if (app.apps) app = { ...app.apps }
|
|
||||||
const pluginArray = []
|
const pluginArray = []
|
||||||
lodash.forEach(app, p => pluginArray.push(this.loadPlugin(file, p)))
|
lodash.forEach(app.apps, p => pluginArray.push(this.loadPlugin(file, p)))
|
||||||
for (const i of await Promise.allSettled(pluginArray))
|
for (const i of await Promise.allSettled(pluginArray))
|
||||||
if (i?.status && i.status != 'fulfilled') {
|
if (i?.status && i.status != 'fulfilled') {
|
||||||
logger.error(`加载插件错误:${logger.red(file.name)}`)
|
logger.error(`加载插件错误:${logger.red(file.name)}`)
|
||||||
logger.error(decodeURI(i.reason))
|
logger.error(decodeURI(i.reason))
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
if (packageErr && error.stack.includes('Cannot find package')) {
|
if (packageErr && error.stack.includes('Cannot find package')) {
|
||||||
packageErr.push({ error, file })
|
packageErr.push({ error, file })
|
||||||
} else {
|
} else {
|
||||||
|
@ -185,6 +178,7 @@ class PluginsLoader {
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async loadPlugin(file, p) {
|
async loadPlugin(file, p) {
|
||||||
|
// 不存在原型链
|
||||||
if (!p?.prototype) return
|
if (!p?.prototype) return
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -390,23 +384,19 @@ class PluginsLoader {
|
||||||
|
|
||||||
for (const plugin of priority) {
|
for (const plugin of priority) {
|
||||||
if (!Array.isArray(plugin?.rule) || plugin.rule.length < 1) continue
|
if (!Array.isArray(plugin?.rule) || plugin.rule.length < 1) continue
|
||||||
|
|
||||||
for (const v of plugin.rule) {
|
for (const v of plugin.rule) {
|
||||||
/**
|
/**
|
||||||
* 判断事件
|
* 判断事件
|
||||||
*/
|
*/
|
||||||
if (v.event && !this.filtEvent(e, v)) continue
|
if (v.event && !this.filtEvent(e, v)) continue
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
if (!new RegExp(v.reg).test(e.msg)) continue
|
if (!new RegExp(v.reg).test(e.msg)) continue
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
e.logFnc = `[${plugin.name}][${v.fnc}]`
|
e.logFnc = `[${plugin.name}][${v.fnc}]`
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -415,27 +405,21 @@ class PluginsLoader {
|
||||||
`${e.logFnc}${e.logText} ${lodash.truncate(e.msg, { length: 100 })}`
|
`${e.logFnc}${e.logText} ${lodash.truncate(e.msg, { length: 100 })}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断权限
|
* 判断权限
|
||||||
*/
|
*/
|
||||||
if (!this.filtPermission(e, v)) break
|
if (!this.filtPermission(e, v)) break
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
const start = Date.now()
|
const start = Date.now()
|
||||||
|
|
||||||
// 不是函数。
|
// 不是函数。
|
||||||
if (typeof plugin[v.fnv] !== 'function') {
|
if (typeof plugin[v.fnv] !== 'function') {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await plugin[v.fnc](e)
|
const res = await plugin[v.fnc](e)
|
||||||
|
|
||||||
// 非常规返回,不是true,直接结束。
|
// 非常规返回,不是true,直接结束。
|
||||||
|
|
||||||
if (typeof res != 'boolean' && res !== true) {
|
if (typeof res != 'boolean' && res !== true) {
|
||||||
/**
|
/**
|
||||||
* 设置冷却cd
|
* 设置冷却cd
|
||||||
|
@ -448,7 +432,6 @@ class PluginsLoader {
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`${e.logFnc}`)
|
logger.error(`${e.logFnc}`)
|
||||||
|
@ -485,7 +468,6 @@ class PluginsLoader {
|
||||||
*/
|
*/
|
||||||
filtPermission(e, v) {
|
filtPermission(e, v) {
|
||||||
if (v.permission == 'all' || !v.permission) return true
|
if (v.permission == 'all' || !v.permission) return true
|
||||||
|
|
||||||
if (v.permission == 'master') {
|
if (v.permission == 'master') {
|
||||||
if (e.isMaster) {
|
if (e.isMaster) {
|
||||||
return true
|
return true
|
||||||
|
@ -494,7 +476,6 @@ class PluginsLoader {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.isGroup) {
|
if (e.isGroup) {
|
||||||
if (!e.member?._info) {
|
if (!e.member?._info) {
|
||||||
e.reply('数据加载中,请稍后再试')
|
e.reply('数据加载中,请稍后再试')
|
||||||
|
@ -513,7 +494,6 @@ class PluginsLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1199,7 +1179,7 @@ class PluginsLoader {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
await this.importPlugin({
|
await this.#importPlugin({
|
||||||
name: key,
|
name: key,
|
||||||
path: `../../${this.dir}/${key}?${moment().format('X')}`
|
path: `../../${this.dir}/${key}?${moment().format('X')}`
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue