update: 补充类型声明

This commit is contained in:
ningmengchongshui 2024-06-15 23:40:16 +08:00
parent 3aac394e4b
commit 610a84c6ea
3 changed files with 133 additions and 110 deletions

View File

@ -152,18 +152,11 @@ class PluginsLoader {
*/
#importPlugin = async (file, packageErr?: any) => {
try {
// 应该去去印插件的路径。
// 才能确定是谁的插件
const app = await import(`file://${join(process.cwd(), file.path)}`)
const pluginArray = []
for (const key in app.apps) {
// console.log('key', key)
pluginArray.push(this.loadPlugin(file, app.apps[key], key))
}
for (const i of await Promise.allSettled(pluginArray))
if (i?.status && i.status != 'fulfilled') {
logger.error(`加载插件错误:${logger.red(file.name)}`)

View File

@ -1,7 +1,5 @@
import * as common from '../../utils/common.js'
import cfg from '../../config/config.js'
import Handler from './handler.js'
import lodash from 'lodash'
import fs from 'node:fs'
import {
gsCfg,
mysApi as MysApi,
@ -9,21 +7,14 @@ import {
NoteUser,
MysUser
} from '../../mys/index.js'
import puppeteer from '../../utils/puppeteer/puppeteer.js'
import lodash from 'lodash'
import fs from 'node:fs'
import * as common from '../../utils/common.js'
import cfg from '../../config/config.js'
import Handler from './handler.js'
import { Version } from '../../miao.js'
/**
* ********************
* e进行重构的危险代码
* ********************
* tudo
*
* @deprecated
*/
export default class Runtime {
e = null
@ -121,7 +112,7 @@ export default class Runtime {
*/
static async init(e) {
await MysInfo.initCache()
let runtime = new Runtime(e)
const runtime = new Runtime(e)
e.runtime = runtime
await runtime.initUser()
return runtime
@ -239,7 +230,7 @@ export default class Runtime {
/**
* @deprecated
* @param plugin plugin key
* @param plugin_name plugin key
* @param path html文件路径plugin resources目录
* @param data
* @param cfg
@ -250,77 +241,91 @@ export default class Runtime {
* @param cfg.beforeRender({data}) data数据
* @returns {Promise<boolean>}
*/
async render (plugin, path, data = {}, cfg = {}) {
// 处理传入的path
path = path.replace(/.html$/, '')
let paths = lodash.filter(path.split('/'), (p) => !!p)
path = paths.join('/')
// 创建目录
const mkdir = (check) => {
let currDir = `${process.cwd()}/temp`
for (let p of check.split('/')) {
currDir = `${currDir}/${p}`
if (!fs.existsSync(currDir)) {
fs.mkdirSync(currDir)
}
}
return currDir
}
mkdir(`html/${plugin}/${path}`)
// 自动计算pluResPath
let pluResPath = `../../../${lodash.repeat('../', paths.length)}plugins/${plugin}/resources/`
let miaoResPath = `../../../${lodash.repeat('../', paths.length)}plugins/miao-plugin/resources/`
const layoutPath = process.cwd() + '/plugins/miao-plugin/resources/common/layout/'
// 渲染data
data = {
sys: {
scale: 1
},
/** miao 相关参数 **/
copyright: `Created By Miao-Yunzai<span class="version">${Version.yunzai}</span> `,
_res_path: pluResPath,
_miao_path: miaoResPath,
_tpl_path: process.cwd() + '/plugins/miao-plugin/resources/common/tpl/',
defaultLayout: layoutPath + 'default.html',
elemLayout: layoutPath + 'elem.html',
async render(
plugin_name: string,
path: string,
data: {
[key: string]: any
saveId?: any,
save_id?: any,
_htmlPath?: any,
} = {}, cfg: {
[key: string]: any
retType?: any,
recallMsg?: any
beforeRender?: any
} = {}
) {
// 处理传入的path
path = path.replace(/.html$/, '')
let paths = lodash.filter(path.split('/'), (p) => !!p)
path = paths.join('/')
// 创建目录
const mkdir = (check) => {
let currDir = `${process.cwd()}/temp`
for (let p of check.split('/')) {
currDir = `${currDir}/${p}`
if (!fs.existsSync(currDir)) {
fs.mkdirSync(currDir)
}
}
return currDir
}
mkdir(`html/${plugin_name}/${path}`)
// 自动计算pluResPath
let pluResPath = `../../../${lodash.repeat('../', paths.length)}plugins/${plugin_name}/resources/`
let miaoResPath = `../../../${lodash.repeat('../', paths.length)}plugins/miao-plugin/resources/`
const layoutPath = process.cwd() + '/plugins/miao-plugin/resources/common/layout/'
// 渲染data
data = {
sys: {
scale: 1
},
/** miao 相关参数 **/
copyright: `Created By Miao-Yunzai<span class="version">${Version.yunzai}</span> `,
_res_path: pluResPath,
_miao_path: miaoResPath,
_tpl_path: process.cwd() + '/plugins/miao-plugin/resources/common/tpl/',
defaultLayout: layoutPath + 'default.html',
elemLayout: layoutPath + 'elem.html',
...data,
...data,
/** 默认参数 **/
_plugin: plugin,
_htmlPath: path,
pluResPath,
tplFile: `./plugins/${plugin}/resources/${path}.html`,
saveId: data.saveId || data.save_id || paths[paths.length - 1],
pageGotoParams: {
waitUntil: 'networkidle2'
}
}
// 处理beforeRender
if (cfg.beforeRender) {
data = cfg.beforeRender({ data }) || data
}
// 保存模板数据
if (process.argv.includes('dev')) {
// debug下保存当前页面的渲染数据方便模板编写与调试
// 由于只用于调试开发者只关注自己当时开发的文件即可暂不考虑app及plugin的命名冲突
let saveDir = mkdir(`ViewData/${plugin}`)
let file = `${saveDir}/${data._htmlPath.split('/').join('_')}.json`
fs.writeFileSync(file, JSON.stringify(data))
}
// 截图
let base64 = await puppeteer.screenshot(`${plugin}/${path}`, data)
if (cfg.retType === 'base64') {
return base64
}
let ret = true
if (base64) {
if (cfg.recallMsg) {
ret = await this.e.reply(base64, false, {})
} else {
ret = await this.e.reply(base64)
}
}
return cfg.retType === 'msgId' ? ret : true
/** 默认参数 **/
_plugin: plugin_name,
_htmlPath: path,
pluResPath,
tplFile: `./plugins/${plugin_name}/resources/${path}.html`,
saveId: data.saveId || data.save_id || paths[paths.length - 1],
pageGotoParams: {
waitUntil: 'networkidle2'
}
}
// 处理beforeRender
if (cfg.beforeRender) {
data = cfg.beforeRender({ data }) || data
}
// 保存模板数据
if (process.argv.includes('dev')) {
// debug下保存当前页面的渲染数据方便模板编写与调试
// 由于只用于调试开发者只关注自己当时开发的文件即可暂不考虑app及plugin的命名冲突
let saveDir = mkdir(`ViewData/${plugin_name}`)
let file = `${saveDir}/${data._htmlPath.split('/').join('_')}.json`
fs.writeFileSync(file, JSON.stringify(data))
}
// 截图
let base64 = await puppeteer.screenshot(`${plugin_name}/${path}`, data)
if (cfg.retType === 'base64') {
return base64
}
let ret = true
if (base64) {
if (cfg.recallMsg) {
ret = await this.e.reply(base64, false, {})
} else {
ret = await this.e.reply(base64)
}
}
return cfg.retType === 'msgId' ? ret : true
}
}

View File

@ -1,28 +1,38 @@
import { type GroupMessage } from 'icqq'
// import { Client } from 'icqq'
// import { PrivateMessage } from 'oicq'
import Runtime from './runtime'
/**
*
*/
export interface EventType extends GroupMessage {
/**
*
*
*/
isMaster: boolean
/**
*
*
*/
// isAdmin: boolean;
/**
*
*/
isGroup: boolean
/**
*
*/
// isGroupAdmin:boolean
/**
*
*/
isPrivate?: any
isPrivate: boolean
/**
*
*/
isGuild?: any
isGuild: boolean
/**
*
*/
user_id: number
/**
*
*/
@ -30,7 +40,7 @@ export interface EventType extends GroupMessage {
/**
*
*/
user_avatar: string | null
user_avatar: string
/**
*
*/
@ -52,9 +62,15 @@ export interface EventType extends GroupMessage {
/**
*
*/
group_avatar: string | null
group_avatar: string
/**
*
* @deprecated
*/
runtime: typeof Runtime.prototype
/**
* @deprecated
*/
group: {
is_owner: any;
@ -62,28 +78,30 @@ export interface EventType extends GroupMessage {
getMemberMap: any;
quit: any;
mute_left: any
pickMember: any
sendMsg: any
};
/**
* @deprecated
*/
file: any;
/**
* @deprecated
*/
bot: any;
/**
*
* @deprecated
*/
approve: any;
/**
*
* @deprecated
*/
member: any;
/**
*
* @deprecated
*/
logText: any;
/**
@ -96,34 +114,41 @@ export interface EventType extends GroupMessage {
isGs?: boolean
/**
*
* @deprecated
*/
self_id?: any
/**
*
* @deprecated
*/
game?: any
/**
*
* @deprecated
*/
detail_type?: any
/**
*
* @deprecated
*/
at?: any
/**
*
* @deprecated
*/
atBot: any;
/**
*
* @deprecated
*/
hasAlias?: any
/**
*
* @deprecated
*/
replyNew?: any
/**
*
* @deprecated
*/
friend?: any
}