Skip to content

Commit

Permalink
🔥 fix: 修复群单独设置 热更新插件包错误 定时任务构建
Browse files Browse the repository at this point in the history
  • Loading branch information
Lain. committed Apr 24, 2024
1 parent a4112f5 commit 29beb55
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 83 deletions.
41 changes: 6 additions & 35 deletions lib/adapter/onebot/OneBot11.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,49 +118,19 @@ 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:
this.logger('info', `未知事件:${JSON.stringify(data)}`)
}
}

/** 构建通用字段 */
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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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 - 消息内容
Expand Down
6 changes: 3 additions & 3 deletions lib/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] || {}) }
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/event/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`))
Expand Down
9 changes: 5 additions & 4 deletions lib/event/message.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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('[消息拦截] 正在冷却中')
/** 检查白名单用户 */
Expand Down Expand Up @@ -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
Expand Down
94 changes: 57 additions & 37 deletions lib/plugins/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import lodash from 'lodash'
import plugin from './plugin.js'

class App {
Expand Down Expand Up @@ -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'
})
}

/**
Expand All @@ -72,42 +76,58 @@ 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')
if (!task.cron) throw new Error('[插件构建][定时任务] 缺少cron')
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
})
}

/**
Expand Down Expand Up @@ -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
}
}
Expand Down
24 changes: 20 additions & 4 deletions lib/plugins/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`)
})
})

/** 监听修改 */
Expand All @@ -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
})
}
}

Expand Down

0 comments on commit 29beb55

Please sign in to comment.