From 29beb552ee4987f3d434d6a8fb13c9fe4cdc2262 Mon Sep 17 00:00:00 2001 From: "Lain." Date: Wed, 24 Apr 2024 18:35:00 +0800 Subject: [PATCH] =?UTF-8?q?:fire:=20fix:=20=E4=BF=AE=E5=A4=8D=E7=BE=A4?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E8=AE=BE=E7=BD=AE=20=E7=83=AD=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=8F=92=E4=BB=B6=E5=8C=85=E9=94=99=E8=AF=AF=20?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/adapter/onebot/OneBot11.js | 41 +++------------ lib/config/config.js | 6 +-- lib/event/event.js | 1 + lib/event/message.js | 9 ++-- lib/plugins/app.js | 94 +++++++++++++++++++++------------- lib/plugins/loader.js | 24 +++++++-- 6 files changed, 92 insertions(+), 83 deletions(-) diff --git a/lib/adapter/onebot/OneBot11.js b/lib/adapter/onebot/OneBot11.js index 8f8d631b..b2d1a2d4 100644 --- a/lib/adapter/onebot/OneBot11.js +++ b/lib/adapter/onebot/OneBot11.js @@ -118,15 +118,12 @@ class OneBot11 { this.#meta_event_event(data) break case 'message': - data = this.norm(data) this.#message_event(data) break case 'notice': - data = this.norm(data) this.#notice_event(data) break case 'request': - data = this.norm(data) this.#request_event(data) break default: @@ -134,33 +131,6 @@ class OneBot11 { } } - /** 构建通用字段 */ - norm (data) { - data = { - ...data, - SendApi: (action, params) => this.SendApi(action, params), - /** - * 获取头像url - * @param size 头像大小,默认`0` - * @param qq 用户qq,默认为消息发送者 - * @returns 头像的url地址 - */ - getAvatarUrl: (size = 0, qq = data.user_id) => this.getAvatarUrl(size, qq), - /** - * 快速回复 - * @param {message} message - 消息内容 - */ - reply: async (message) => { - if (data.message_type === 'private') { - return await this.send_private_msg(data.user_id, message) - } else { - return await this.send_group_msg(data.group_id, message) - } - } - } - return data - } - /** 元事件 */ #meta_event_event (data) { switch (data.meta_event_type) { @@ -177,11 +147,6 @@ class OneBot11 { /** 消息事件 */ async #message_event (data) { - /** - * 快速撤回,可键入指定message_id - * @param {number} message_id - 消息ID - */ - data.delete_msg = async message_id => this.delete_msg(message_id || data.message_id) let message = { self_id: data.self_id, user_id: data.sender.user_id, @@ -217,6 +182,12 @@ class OneBot11 { const e = new KarinMessage(message) + /** + * 快速撤回,可键入指定message_id + * @param {number} message_id - 消息ID + */ + data.delete_msg = async message_id => this.delete_msg(message_id || data.message_id) + /** * 快速回复 * @param {message} message - 消息内容 diff --git a/lib/config/config.js b/lib/config/config.js index 6dbfd900..070d0c4b 100644 --- a/lib/config/config.js +++ b/lib/config/config.js @@ -79,9 +79,9 @@ class Cfg { * @param {string} group_id 群号 */ group (group_id = '') { - const Config = this.getConfig('group').default - const defCfg = this.getdefSet('group').default - return { ...defCfg, ...Config, ...(Config[group_id] || {}) } + const Config = this.getConfig('group') + const defCfg = this.getdefSet('group') + return { ...defCfg.default, ...Config.default, ...(Config[group_id] || {}) } } /** diff --git a/lib/event/event.js b/lib/event/event.js index b9c7ec20..72d7c9ae 100644 --- a/lib/event/event.js +++ b/lib/event/event.js @@ -125,6 +125,7 @@ export default class Event { } try { + Bot.emit('karin:count:send', 1) /** 取结果 */ msgRes = await msgRes logger.debug(common.logger(e.self_id, `回复消息结果: ${JSON.stringify(msgRes, null, 2)}`)) diff --git a/lib/event/message.js b/lib/event/message.js index 92240700..1848bea5 100644 --- a/lib/event/message.js +++ b/lib/event/message.js @@ -1,12 +1,12 @@ -import { Cfg, common, logger } from '#Karin' import lodash from 'lodash' import util from 'node:util' +import Event from './event.js' +import other from '../config/other.js' import loader from '../plugins/loader.js' import { stateArr } from '../plugins/plugin.js' -import Event from './event.js' +import { Bot, Cfg, common, logger } from '#Karin' // eslint-disable-next-line no-unused-vars import { KarinMessage } from '../bot/KarinMessage.js' -import other from '../config/other.js' /** 消息事件 */ class Message extends Event { @@ -15,6 +15,7 @@ class Message extends Event { * @param {KarinMessage} e - 消息事件 */ async deal (e) { + Bot.emit('karin:count:recv', 1) /** cd */ if (other.cd(e)) return logger.debug('[消息拦截] 正在冷却中') /** 检查白名单用户 */ @@ -81,7 +82,7 @@ class Message extends Event { /** 计算插件处理时间 */ let start = Date.now() - + Bot.emit('karin:count:fnc', e.logFnc) if (util.types.isPromise(res)) res = await res v.log(e.self_id, `${e.logFnc} ${lodash.truncate(e.msg, { length: 80 })} 处理完成 ${logger.green(Date.now() - start + 'ms')}`) if (res !== false) break a diff --git a/lib/plugins/app.js b/lib/plugins/app.js index ef65ae6f..6050d0e1 100644 --- a/lib/plugins/app.js +++ b/lib/plugins/app.js @@ -1,3 +1,4 @@ +import lodash from 'lodash' import plugin from './plugin.js' class App { @@ -40,25 +41,28 @@ class App { if (typeof rule.fnc === 'string') { if (!rule[rule.fnc]) throw new Error(`[插件构建] ${rule.fnc} 方法不存在`) this[rule.fnc] = rule[rule.fnc] + /** 删除掉这个函数 */ + delete rule[rule.fnc] } else if (typeof rule.fnc === 'function') { /** 随机生成一个方法名 */ - let fncNme = `fnc_${Math.random().toString(36)}` + let fnc_name = `fnc_${Math.random().toString(36)}` const fnc = rule.fnc - this[fncNme] = fnc - rule.fnc = fncNme - } else { - throw new Error('[插件构建] fnc类型错误') - } - if (!rule.reg) rule.reg = '' - let data = { - fnc: rule.fnc, - reg: rule.reg + this[fnc_name] = fnc + rule.fnc = fnc_name + delete rule[fnc_name] } - if (rule.log === false) data.log = false - if (rule.permission) data.permission = rule.permission + /** 将函数注册到当前对象中 */ + lodash.forIn(rule, (value, key) => { + if (typeof value === 'function') this[key] = value + }) - this.rule.push(data) + this.rule.push({ + fnc: rule.fnc, + reg: rule.reg || '', + log: rule.log || true, + permission: rule.permission || 'all' + }) } /** @@ -72,14 +76,29 @@ class App { this[name] = fnc } - /** 设置accept方法 */ + /** + * 设置插件接收函数 + * @param {Function} fnc - 接收函数 + */ accept (fnc) { if (typeof fnc === 'function') { this.accept = fnc } } - /** 构建定时任务 */ + /** + * 新增插件定时任务 + * @param {{ + * name: string, + * cron: string, + * fnc: string|Function, + * log?: boolean, + * }} task - 插件定时任务对象 + * @param {string} task.name - 定时任务名称 + * @param {string} task.cron - 定时任务表达式 + * @param {string|Function} task.fnc - 定时任务函数名或函数 + * @param {boolean} [task.log=true] - 是否记录日志 + */ cron (task) { /** 检查传入的参数是否符合规则 */ if (!task.name) throw new Error('[插件构建][定时任务] 缺少name') @@ -87,27 +106,28 @@ class App { if (!task.fnc) throw new Error('[插件构建][定时任务] 缺少fnc') if (typeof task.fnc !== 'function') throw new Error('[插件构建][定时任务] fnc类型错误,必须为function') - let data = { + /** 如果fnc是字符串,则在传入的对象中查找对应的方法 */ + if (typeof task.fnc === 'string') { + this[task.fnc] = task[task.fnc] + } else if (typeof task.fnc === 'function') { + /** 随机生成一个方法名 */ + let fnc_name = `fnc_${Math.random().toString(36)}` + const fnc = task.fnc + this[fnc_name] = fnc + task.fnc = fnc_name + } + + /** 将函数注册到当前对象中 */ + lodash.forIn(task, (value, key) => { + if (typeof value === 'function') this[key] = value + }) + + this.task.push({ name: task.name, cron: task.cron, - fnc: task.fnc - } - if (task.log === false) data.log = false - if (!Array.isArray(task)) { - /** 检查是否为对象 */ - if (typeof task === 'object') { - /** 检查是否为初始参数 */ - if (!this.task.name) { - this.task = data - } else { - /** 不是则合并为数组 */ - this.task = [this.task, data] - } - } - } else { - /** 合并数组 */ - this.task = this.task.push(data) - } + fnc: task.fnc, + log: task.log || true + }) } /** @@ -135,9 +155,9 @@ class App { } } // 循环app中的所有函数,构建到cla的原型中 - for (let i in app) { - if (typeof app[i] === 'function') cla.prototype[i] = app[i] - } + lodash.forIn(app, (value, key) => { + if (typeof value === 'function') cla.prototype[key] = value + }) return cla } } diff --git a/lib/plugins/loader.js b/lib/plugins/loader.js index 42027282..45525892 100644 --- a/lib/plugins/loader.js +++ b/lib/plugins/loader.js @@ -309,7 +309,16 @@ class Plugins { /** 载入插件 */ const res = await this.createdApp(dir, name, true) if (!res) return - logger.mark(`[新增插件][${dir}][${name}]`) + /** 延迟1秒 等待卸载完成 */ + + common.sleep(1000).then(() => { + /** 停止整个文件夹监听 */ + watcher.close() + /** 新增插件之后重新监听文件夹 */ + delete this.watcher[dir] + this.watchDir(dir) + logger.mark(`[新增插件][${dir}][${name}]`) + }) }) /** 监听修改 */ @@ -335,14 +344,21 @@ class Plugins { /** 卸载 */ this.uninstallApp(dir, name) /** 停止监听 */ - watcher.removeAllListeners('change') + watcher.close() + /** 重新监听文件夹 */ delete this.watcher[dir] - + this.watchDir(dir) logger.mark(`[卸载插件][${dir}][${name}]`) }) }, 500) - this.watcher[dir] = watcher + /** 生成随机数0.5-2秒 */ + const random = Math.floor(Math.random() * 1000) + 500 + common.sleep(random).then(() => { + /** 这里需要检查一下是否已经存在,已经存在就关掉之前的监听 */ + if (this.watcher[dir]) this.watcher[dir].close() + this.watcher[dir] = watcher + }) } }