diff --git a/src/bot.ts b/src/bot.ts index 99ad7a0..e150bbb 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -20,11 +20,17 @@ import ListenerLoader from './core/events.loader.js' * 扩展 */ import { Client, segment } from 'icqq' +/** + * + */ import { plugin } from './core/plugins/index.js' /** - * global + * global.plugin */ global.plugin = plugin +/** + * global.segment + */ global.segment = segment /** * diff --git a/src/config/check.ts b/src/config/check.ts index f30956b..de23787 100644 --- a/src/config/check.ts +++ b/src/config/check.ts @@ -1,6 +1,4 @@ -import fs from 'fs' -import { exec } from 'child_process' -import { join } from 'path' +import { execAsync, readJSON } from '../utils/index.js' /** * 校验运行 @@ -11,14 +9,18 @@ export async function checkRun() { * */ if (process.argv[1].includes('pm2')) return - if (process.argv[1].includes('test')) return - /** * */ - const cfg = pm2Cfg() + if (process.argv[1].includes('test')) return + /** + * + */ + const cfg = readJSON('./config/pm2/pm2.json') + /** + * + */ if (!cfg) return - /** * */ @@ -31,38 +33,5 @@ export async function checkRun() { logger.mark('已停止后台进程,防止重复运行') execAsync(`pm2 stop ${cfg.apps[0].name}`).catch(logger.error) } - - - }).catch(()=>{}) - -} - -/** - * - * @param cmd - * @returns - */ -function execAsync(cmd: string): Promise<{ - stdout: string, - stderr: string -}> { - return new Promise((resolve, reject) => { - exec(cmd, (error, stdout, stderr) => { - if (error) reject(error) - resolve({ stdout, stderr }) - }) - }) -} - -/** - * - * @returns - */ -function pm2Cfg() { - try { - const cfg = fs.readFileSync(join(process.cwd(), './config/pm2/pm2.json'), 'utf-8') - return JSON.parse(cfg) - } catch { - return false - } -} + }).catch(() => { }) +} \ No newline at end of file diff --git a/src/config/config.ts b/src/config/config.ts index fb1bf59..9a3c6ce 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -1,7 +1,7 @@ import YAML from 'yaml' -import fs from 'node:fs' import chokidar from 'chokidar' import { join } from 'node:path' +import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync } from 'node:fs' import { CONFIG_DEFAULT_PATH, CONFIG_INIT_PATH } from './system.js' /** @@ -35,14 +35,14 @@ class Cfg { initCfg() { const path = CONFIG_INIT_PATH const pathDef = CONFIG_DEFAULT_PATH - const files = fs.readdirSync(pathDef).filter(file => file.endsWith('.yaml')) + const files = readdirSync(pathDef).filter(file => file.endsWith('.yaml')) for (let file of files) { - if (!fs.existsSync(`${path}${file}`)) { - fs.copyFileSync(`${pathDef}${file}`, `${path}${file}`) + if (!existsSync(`${path}${file}`)) { + copyFileSync(`${pathDef}${file}`, `${path}${file}`) } } - if (!fs.existsSync("data")) fs.mkdirSync("data") - if (!fs.existsSync("resources")) fs.mkdirSync("resources") + if (!existsSync("data")) mkdirSync("data") + if (!existsSync("resources")) mkdirSync("resources") } /** @@ -124,7 +124,7 @@ class Cfg { get package() { if (this._package) return this._package try { - const data = fs.readFileSync('package.json', 'utf8') + const data = readFileSync('package.json', 'utf8') this._package = JSON.parse(data) return this._package } catch { @@ -195,7 +195,7 @@ class Cfg { if (this.config[key]) return this.config[key] this.config[key] = YAML.parse( - fs.readFileSync(file, 'utf8') + readFileSync(file, 'utf8') ) this.watch(file, name, type) @@ -210,7 +210,7 @@ class Cfg { * @param type * @returns */ - watch(file:string, name:string, type = 'default_config') { + watch(file: string, name: string, type = 'default_config') { const key = `${type}.${name}` if (this.watcher[key]) return const watcher = chokidar.watch(file) diff --git a/src/config/index.ts b/src/config/index.ts index 5728445..7507b17 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,9 +1,28 @@ -export { checkRun } from './check.js' + import config from './config.js' -export { checkInit, UpdateTitle as checkUpdateTitle } from './init.js' -export const ConfigController = config -import QQ from './qq.js' -export const createQQ = QQ import RedisInit from './redis.js' +import QQ from './qq.js' +/** + * + */ +export { checkRun } from './check.js' +/** + * + */ +export { checkInit, UpdateTitle as checkUpdateTitle } from './init.js' +/** + * 配置控制器 + */ +export const ConfigController = config +/** + * 创建qq配置 + */ +export const createQQ = QQ +/** + * 初始化redis全局对象 + */ export const redisInit = RedisInit +/** + * + */ export * from './system.js' \ No newline at end of file diff --git a/src/config/qq.ts b/src/config/qq.ts index a479f8c..e27a165 100644 --- a/src/config/qq.ts +++ b/src/config/qq.ts @@ -30,7 +30,7 @@ export default async function createQQ() { /** * */ - let propmtList = [ + const propmtList = [ { type: 'Input', message: '请输入机器人QQ号(建议用小号):', diff --git a/src/config/redis.ts b/src/config/redis.ts index 7745926..7790ee2 100644 --- a/src/config/redis.ts +++ b/src/config/redis.ts @@ -10,8 +10,9 @@ export default async function redisInit() { const rc = cfg.redis const redisUn = rc.username || "" let redisPw = rc.password ? `:${rc.password}` : "" - if (rc.username || rc.password) + if (rc.username || rc.password) { redisPw += "@" + } const redisUrl = `redis://${redisUn}${redisPw}${rc.host}:${rc.port}/${rc.db}` let client = createClient({ url: redisUrl }) @@ -57,9 +58,7 @@ export default async function redisInit() { * @returns */ async function aarch64() { - if (process.platform == "win32") { - return "" - } + if (process.platform == "win32") return "" return await execAsync("uname -m").then(async arch => { if (arch.stdout && arch.stdout.includes("aarch64")) { /** 判断redis版本 */ diff --git a/src/core/plugins/index.ts b/src/core/plugins/index.ts index 09683f1..25cfbc9 100644 --- a/src/core/plugins/index.ts +++ b/src/core/plugins/index.ts @@ -1,3 +1,5 @@ + +import { segment } from 'icqq' import { Common } from '../../miao.js' import { EventType } from './types.js' @@ -18,6 +20,9 @@ export class plugin { * @deprecated 已废弃 */ task = null + /** + * + */ rule: { reg?: RegExp | string fnc: string @@ -25,11 +30,30 @@ export class plugin { log?: boolean permission?: string }[] = [] + /** + * + */ event = 'message' + /** + * + */ priority = 9999 + /** + * + */ namespace = null + + /** + * + */ handler = null - e: EventType + + /** + * + */ + e: EventType & { + segment: typeof segment + } /** * @param name 插件名称 @@ -103,6 +127,10 @@ export class plugin { this.handler = handler this.namespace = namespace || '' } + + // 携带segment + this.e.segment = segment + } /** diff --git a/src/mys/apiTool.ts b/src/mys/apiTool.ts index 60ca08a..09b36a4 100644 --- a/src/mys/apiTool.ts +++ b/src/mys/apiTool.ts @@ -1,4 +1,5 @@ -import { randomRange } from './mysApi.js' +import { randomRange } from '../utils/index.js' + /** * 整合接口用于查询数据 * 方便后续用于解耦 diff --git a/src/mys/index.ts b/src/mys/index.ts index 4dd6e8b..2f3f676 100644 --- a/src/mys/index.ts +++ b/src/mys/index.ts @@ -15,4 +15,16 @@ export { BaseModel, DailyCache, MysUser, MysUtil, NoteUser } /** * @deprecated 已废弃 */ -export { apiTool, gsCfg, mysApi, mysInfo } +export { apiTool } +/** + * @deprecated 已废弃 + */ +export { gsCfg } +/** + * @deprecated 已废弃 + */ +export { mysApi } +/** + * @deprecated 已废弃 + */ +export { mysInfo } diff --git a/src/mys/mysApi.ts b/src/mys/mysApi.ts index 66608f9..c1c9194 100644 --- a/src/mys/mysApi.ts +++ b/src/mys/mysApi.ts @@ -342,17 +342,3 @@ export default class MysApi { return result } } - -/** - * - * @returns - */ -export function randomRange() { - let randomStr = '' - let charStr = 'abcdef0123456789' - for (let i = 0; i < 64; i++) { - let index = Math.round(Math.random() * (charStr.length - 1)) - randomStr += charStr.substring(index, index + 1) - } - return randomStr -} diff --git a/src/utils/common.ts b/src/utils/common.ts index 0b0a05a..e4594b3 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -4,6 +4,7 @@ import fetch from 'node-fetch' import fs from 'node:fs' import path from 'node:path' import { exec } from 'child_process' +import { join } from 'path' /** * 休眠函数 @@ -66,3 +67,31 @@ export function execAsync(cmd: string): Promise<{ }) }) } + +/** + * + * @param dir + * @returns + */ +export function readJSON(dir: string) { + try { + const cfg = fs.readFileSync(join(process.cwd(), dir), 'utf-8') + return JSON.parse(cfg) + } catch { + return false + } +} + +/** + * + * @returns + */ +export function randomRange() { + let randomStr = '' + let charStr = 'abcdef0123456789' + for (let i = 0; i < 64; i++) { + let index = Math.round(Math.random() * (charStr.length - 1)) + randomStr += charStr.substring(index, index + 1) + } + return randomStr +}