From 067497fe207cfe470897ddc81e156065f1109e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=8C=8C?= Date: Thu, 20 Jul 2023 14:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20Bot=20=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/default_config/group.yaml | 19 +++++++-- lib/config/config.js | 69 +++++++++++++++++--------------- lib/plugins/loader.js | 12 +++--- plugins/system/add.js | 6 +-- 4 files changed, 60 insertions(+), 46 deletions(-) diff --git a/config/default_config/group.yaml b/config/default_config/group.yaml index 1cc00ab..43c29fb 100644 --- a/config/default_config/group.yaml +++ b/config/default_config/group.yaml @@ -15,12 +15,23 @@ default: enable: #只启用功能,配置后只有该功能才响应 #禁用功能,功能名称,例如:十连、角色查询、体力查询、用户绑定、抽卡记录、添加表情、欢迎新人、退群通知 - #禁用功能,功能名称,例如:云崽帮助、角色素材、今日素材、养成计算、米游社公告 disable: - 禁用示例 - 支持多个 -# 群单独设置,自动覆盖默认值 +# Bot单独设置 +114514:default: + onlyReplyAt: 1 + botAlias: + - 臭崽 + - 臭宝 + +# 群单独设置 123456: - groupCD: 500 # 群聊中所有指令操作冷却时间,单位毫秒,0则无限制 - singleCD: 2000 # 群聊中个人操作冷却时间,单位毫秒 \ No newline at end of file + groupCD: 500 + singleCD: 2000 + +# [Bot:群]单独设置 +114514:123456: + enable: + disable: \ No newline at end of file diff --git a/lib/config/config.js b/lib/config/config.js index 3191a80..7b996cd 100644 --- a/lib/config/config.js +++ b/lib/config/config.js @@ -1,6 +1,6 @@ -import YAML from 'yaml' -import fs from 'node:fs' -import chokidar from 'chokidar' +import YAML from "yaml" +import fs from "node:fs" +import chokidar from "chokidar" /** 配置文件 */ class Cfg { @@ -15,40 +15,40 @@ class Cfg { /** 初始化配置 */ initCfg () { - let path = './config/config/' - let pathDef = './config/default_config/' - const files = fs.readdirSync(pathDef).filter(file => file.endsWith('.yaml')) + let path = "./config/config/" + let pathDef = "./config/default_config/" + const files = fs.readdirSync(pathDef).filter(file => file.endsWith(".yaml")) for (let file of files) if (!fs.existsSync(`${path}${file}`)) fs.copyFileSync(`${pathDef}${file}`, `${path}${file}`) - if (!fs.existsSync('./data')) fs.mkdirSync('./data') - if (!fs.existsSync('./resources')) fs.mkdirSync('./resources') + if (!fs.existsSync("./data")) fs.mkdirSync("./data") + if (!fs.existsSync("./resources")) fs.mkdirSync("./resources") } /** Bot配置 */ get bot () { - let bot = this.getConfig('bot') - let defbot = this.getdefSet('bot') + let bot = this.getConfig("bot") + let defbot = this.getdefSet("bot") bot = { ...defbot, ...bot } return bot } get other () { - return this.getConfig('other') + return this.getConfig("other") } get redis () { - return this.getConfig('redis') + return this.getConfig("redis") } get renderer() { - return this.getConfig('renderer'); + return this.getConfig("renderer"); } /** 主人账号 */ get masterQQ () { - let masterQQ = this.getConfig('other').masterQQ || [] + let masterQQ = this.getConfig("other").masterQQ || [] if (!Array.isArray(masterQQ)) masterQQ = [masterQQ] @@ -61,14 +61,14 @@ class Cfg { /** Bot账号:[主人帐号] */ get master () { - let master = this.getConfig('other').master || [] + let master = this.getConfig("other").master || [] if (!Array.isArray(master)) master = [master] const masters = {} for (let i of master) { - i = i.split(':') + i = i.split(":") if (Array.isArray(masters[i[0]])) masters[i[0]].push(i[1]) else @@ -89,24 +89,27 @@ class Cfg { get package () { if (this._package) return this._package - this._package = JSON.parse(fs.readFileSync('./package.json', 'utf8')) + this._package = JSON.parse(fs.readFileSync("./package.json", "utf8")) return this._package } /** 群配置 */ - getGroup (groupId = '') { - let config = this.getConfig('group') - let defCfg = this.getdefSet('group') - if (config[groupId]) { - return { ...defCfg.default, ...config.default, ...config[groupId] } + getGroup (bot_id = "", group_id = "") { + const config = this.getConfig("group") + const defCfg = this.getdefSet("group") + return { + ...defCfg.default, + ...config.default, + ...config[`${bot_id}:default`], + ...config[group_id], + ...config[`${bot_id}:${group_id}`], } - return { ...defCfg.default, ...config.default } } /** other配置 */ getOther () { - let def = this.getdefSet('other') - let config = this.getConfig('other') + let def = this.getdefSet("other") + let config = this.getConfig("other") return { ...def, ...config } } @@ -115,12 +118,12 @@ class Cfg { * @param name 配置文件名称 */ getdefSet (name) { - return this.getYaml('default_config', name) + return this.getYaml("default_config", name) } /** 用户配置 */ getConfig (name) { - return this.getYaml('config', name) + return this.getYaml("config", name) } /** @@ -134,7 +137,7 @@ class Cfg { if (this.config[key]) return this.config[key] this.config[key] = YAML.parse( - fs.readFileSync(file, 'utf8') + fs.readFileSync(file, "utf8") ) this.watch(file, name, type) @@ -143,15 +146,15 @@ class Cfg { } /** 监听配置文件 */ - watch (file, name, type = 'default_config') { + watch (file, name, type = "default_config") { let key = `${type}.${name}` if (this.watcher[key]) return const watcher = chokidar.watch(file) - watcher.on('change', path => { + watcher.on("change", path => { delete this.config[key] - if (typeof Bot == 'undefined') return + if (typeof Bot == "undefined") return logger.mark(`[修改配置文件][${type}][${name}]`) if (this[`change_${name}`]) { this[`change_${name}`]() @@ -163,9 +166,9 @@ class Cfg { async change_bot () { /** 修改日志等级 */ - let log = await import('./log.js') + let log = await import("./log.js") log.default() } } -export default new Cfg() +export default new Cfg() \ No newline at end of file diff --git a/lib/plugins/loader.js b/lib/plugins/loader.js index d37b205..709ce03 100644 --- a/lib/plugins/loader.js +++ b/lib/plugins/loader.js @@ -431,7 +431,7 @@ class PluginsLoader { /** 只关注主动at msg处理 */ if (e.msg && e.isGroup) { - let groupCfg = cfg.getGroup(e.group_id) + let groupCfg = cfg.getGroup(e.self_id, e.group_id) let alias = groupCfg.botAlias if (!Array.isArray(alias)) { alias = [alias] @@ -605,7 +605,7 @@ class PluginsLoader { if (e.isGroup && e?.group?.mute_left > 0) return false if (!e.message || e.isPrivate) return true - let config = cfg.getGroup(e.group_id) + let config = cfg.getGroup(e.self_id, e.group_id) if (config.groupCD && this.groupCD[e.group_id]) { return false @@ -631,7 +631,7 @@ class PluginsLoader { /** 设置冷却cd */ setLimit(e) { if (!e.message || e.isPrivate) return - let config = cfg.getGroup(e.group_id) + let config = cfg.getGroup(e.self_id, e.group_id) if (config.groupCD) { this.groupCD[e.group_id] = true @@ -652,7 +652,7 @@ class PluginsLoader { onlyReplyAt(e) { if (!e.message || e.isPrivate) return true - let groupCfg = cfg.getGroup(e.group_id) + let groupCfg = cfg.getGroup(e.self_id, e.group_id) if (groupCfg.onlyReplyAt != 1 || !groupCfg.botAlias) return true @@ -693,7 +693,7 @@ class PluginsLoader { /** 判断是否启用功能 */ checkDisable(e, p) { - let groupCfg = cfg.getGroup(e.group_id) + let groupCfg = cfg.getGroup(e.self_id, e.group_id) if (!lodash.isEmpty(groupCfg.enable)) { if (groupCfg.enable.includes(p.name)) { return true @@ -831,4 +831,4 @@ class PluginsLoader { } } -export default new PluginsLoader() +export default new PluginsLoader() \ No newline at end of file diff --git a/plugins/system/add.js b/plugins/system/add.js index cc67a05..5b84afc 100644 --- a/plugins/system/add.js +++ b/plugins/system/add.js @@ -121,7 +121,7 @@ export class add extends plugin { checkAuth () { if (this.e.isMaster) return true - let groupCfg = cfg.getGroup(this.group_id) + let groupCfg = cfg.getGroup(this.e.self_id, this.group_id) if (groupCfg.imgAddLimit == 2) { this.e.reply('暂无权限,只有主人才能操作') return false @@ -204,7 +204,7 @@ export class add extends plugin { /** 过滤别名 */ trimAlias (msg) { - let groupCfg = cfg.getGroup(this.group_id) + let groupCfg = cfg.getGroup(this.e.self_id, this.group_id) let alias = groupCfg.botAlias if (!Array.isArray(alias)) { alias = [alias] @@ -284,7 +284,7 @@ export class add extends plugin { } async saveImg (url, keyWord) { - let groupCfg = cfg.getGroup(this.group_id) + let groupCfg = cfg.getGroup(this.e.self_id, this.group_id) let savePath = `${this.facePath}${this.group_id}/` if (!fs.existsSync(savePath)) {