From bca3842ae6d47fa7d75e4b97de454b5632e3ebb5 Mon Sep 17 00:00:00 2001 From: ningmengchongshui <916415899@qq.com> Date: Mon, 17 Jun 2024 22:49:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/config.ts | 4 +- src/core/bot.ts | 30 ----------- src/core/plugins/common.ts | 6 --- src/core/plugins/functional.ts | 8 +-- src/core/plugins/runtime.ts | 18 +++---- src/init/redis.ts | 2 - src/init/run.ts | 91 ++++++++++++++++------------------ 7 files changed, 59 insertions(+), 100 deletions(-) diff --git a/src/config/config.ts b/src/config/config.ts index f0e3b90..abe5255 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -110,7 +110,7 @@ class ConfigController { * @param groupId * @returns */ - getGroup(groupId = '') { + getGroup(groupId: number | string = '') { const config = this.getConfig('group') const defCfg = this.getdefSet('group') if (config[groupId]) { @@ -162,7 +162,7 @@ class ConfigController { * @param type 默认跑配置-defSet,用户配置-config * @param name 名称 */ - getYaml(type, name) { + getYaml(type: string, name: string) { const file = `config/${type}/${name}.yaml` const key = `${type}.${name}` if (this.config[key]) return this.config[key] diff --git a/src/core/bot.ts b/src/core/bot.ts index af6108b..1a52e25 100644 --- a/src/core/bot.ts +++ b/src/core/bot.ts @@ -39,54 +39,26 @@ export class Client extends IcqqClient { */ static async run() { const bot = new Client(cfg.bot) - - /** - * 加载监听事件 - */ await ListenerLoader.load(bot) - - /** - * 跳过登录 - */ if (cfg.bot.skip_login) { return await this.skip_login(bot) } - - /** - * 正常的登录 - */ await bot.login(cfg.qq, cfg.pwd) - bot[bot.uin] = bot - - /** - * 全局变量 bot - */ global.Bot = bot - return bot } - /** * 跳过登录ICQQ * @param bot * @returns */ static async skip_login(bot) { - // bot.uin = 88888 - // bot[bot.uin] = bot - /** - * 全局变量 bot - */ global.Bot = bot - /** - * - */ return bot } - /** * 加载插件 * @param bot @@ -95,8 +67,6 @@ export class Client extends IcqqClient { static async PluginsLoader() { await PluginsLoader.load() } - - // } /** diff --git a/src/core/plugins/common.ts b/src/core/plugins/common.ts index 94697e9..3564294 100644 --- a/src/core/plugins/common.ts +++ b/src/core/plugins/common.ts @@ -1,9 +1,3 @@ -/** - * ************ - * 关于bot处理,属于core - * ************ - */ - /** * 发送私聊消息,仅给好友发送 * @param userId qq号 diff --git a/src/core/plugins/functional.ts b/src/core/plugins/functional.ts index 586815c..79e1dd5 100644 --- a/src/core/plugins/functional.ts +++ b/src/core/plugins/functional.ts @@ -1,5 +1,5 @@ import { MessageCallBackType } from './types.js' -import { plugin } from './index.js' +import { Plugin } from './index.js' import { EventMap } from 'icqq' /** @@ -60,7 +60,7 @@ export class Messages { */ get ok() { const App = this - class Children extends plugin { + class Children extends Plugin { constructor() { super({ ...App.#init, @@ -91,14 +91,14 @@ export class Events { * */ #data: { - [key: string]: typeof plugin + [key: string]: typeof Plugin } = {} /** * * @param val */ - use(val: typeof plugin) { + use(val: typeof Plugin) { this.#count++ this.#data[this.#count] = val } diff --git a/src/core/plugins/runtime.ts b/src/core/plugins/runtime.ts index 5ab3057..2bee981 100644 --- a/src/core/plugins/runtime.ts +++ b/src/core/plugins/runtime.ts @@ -1,5 +1,5 @@ import lodash from 'lodash' -import fs from 'node:fs' +import { existsSync, mkdirSync, writeFileSync } from 'node:fs' import { gsCfg, mysApi as MysApi, @@ -265,16 +265,16 @@ export default class Runtime { let currDir = `${process.cwd()}/temp` for (let p of check.split('/')) { currDir = `${currDir}/${p}` - if (!fs.existsSync(currDir)) { - fs.mkdirSync(currDir) + if (!existsSync(currDir)) { + 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 pluResPath = `../../../${lodash.repeat('../', paths.length)}plugins/${plugin_name}/resources/` + const miaoResPath = `../../../${lodash.repeat('../', paths.length)}plugins/miao-plugin/resources/` const layoutPath = process.cwd() + '/plugins/miao-plugin/resources/common/layout/' // 渲染data data = { @@ -309,12 +309,12 @@ export default class Runtime { 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)) + const saveDir = mkdir(`ViewData/${plugin_name}`) + const file = `${saveDir}/${data._htmlPath.split('/').join('_')}.json` + writeFileSync(file, JSON.stringify(data)) } // 截图 - let base64 = await puppeteer.screenshot(`${plugin_name}/${path}`, data) + const base64 = await puppeteer.screenshot(`${plugin_name}/${path}`, data) if (cfg.retType === 'base64') { return base64 } diff --git a/src/init/redis.ts b/src/init/redis.ts index ef68f3e..f0902ec 100644 --- a/src/init/redis.ts +++ b/src/init/redis.ts @@ -44,8 +44,6 @@ async function redisInit() { }) /** 全局变量 redis */ global.redis = client as any - - // logger.info('Redis 连接成功') return client } diff --git a/src/init/run.ts b/src/init/run.ts index 2b642f6..228a077 100644 --- a/src/init/run.ts +++ b/src/init/run.ts @@ -3,65 +3,62 @@ import { createRequire } from 'module' import { existsSync } from 'fs' import pm2 from 'pm2' const require = createRequire(import.meta.url) + /** - * 校验运行 + * 询问运行情况 * @returns */ -export function checkRun() { +function inquiryProcess() { return new Promise((resolve, reject) => { - if (process.argv[1].includes('pm2')) { - resolve(true) + // 开始询问是否有正在运行的同实例进程 + const dir = join(process.cwd(), 'pm2.config.cjs') + if (!existsSync(dir)) { + // 不存在配置,错误 + reject(false) } - if (process.argv[1].includes('test')) { - resolve(true) - } - // 不是生产运行 - if (process.env.NODE_ENV != 'production') { - const dir = join(process.cwd(), 'pm2.config.cjs') - if (!existsSync(dir)) { - reject(false) + const cfg = require(dir) + pm2.connect(err => { + if (err) { + reject(err) + return } - const cfg = require('../../pm2.config.cjs') - pm2.connect(err => { + pm2.list((err, processList) => { if (err) { + pm2.disconnect() reject(err) return } - // - pm2.list((err, processList) => { - if (err) { - pm2.disconnect() - reject(err) - return - } - const app = processList.find(p => p.name === cfg.apps[0].name) - if (app && app.pm2_env.status === 'online') { - console.log('检测到后台正在运行') - // 关闭 - pm2.stop(cfg.apps[0].name, err => { - if (err) { - reject(err) - } else { - console.log('已停止后台进程,防止重复运行') - } - pm2.disconnect() - resolve(true) - }) - } else { - // 断开连接 + const app = processList.find(p => p.name === cfg.apps[0].name) + if (app && app.pm2_env.status === 'online') { + console.log('检测到后台正在运行') + // 关闭 + pm2.stop(cfg.apps[0].name, err => { + if (err) { + reject(err) + } else { + console.log('已停止后台进程,防止重复运行') + } pm2.disconnect() resolve(true) - } - }) + }) + } else { + // 断开连接 + pm2.disconnect() + resolve(true) + } }) - } else { - resolve(true) - } + }) + }) +} + +/** + * 询问 + */ +if (process.env.NODE_ENV != 'production') { + await inquiryProcess().catch(err => { + // 打印错误 + console.error(err) + // 关闭进程 + process.exit(2) }) } -await checkRun().catch(err => { - // 打印错误 - console.error(err) - // 关闭进程 - process.exit(2) -})