Miao-Yunzai/lib/config/config.js

174 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-07-20 14:17:06 +08:00
import YAML from "yaml"
import fs from "node:fs"
import chokidar from "chokidar"
2023-05-11 16:03:18 +08:00
/** 配置文件 */
class Cfg {
constructor () {
this.config = {}
/** 监听文件 */
this.watcher = { config: {}, defSet: {} }
this.initCfg()
}
/** 初始化配置 */
initCfg () {
2023-09-04 13:46:08 +08:00
let path = "config/config/"
let pathDef = "config/default_config/"
2023-07-20 14:17:06 +08:00
const files = fs.readdirSync(pathDef).filter(file => file.endsWith(".yaml"))
2023-05-11 16:03:18 +08:00
for (let file of files)
if (!fs.existsSync(`${path}${file}`))
fs.copyFileSync(`${pathDef}${file}`, `${path}${file}`)
2023-09-04 13:46:08 +08:00
if (!fs.existsSync("data")) fs.mkdirSync("data")
if (!fs.existsSync("resources")) fs.mkdirSync("resources")
2023-05-11 16:03:18 +08:00
}
/** Bot配置 */
get bot () {
2023-07-20 14:17:06 +08:00
let bot = this.getConfig("bot")
let defbot = this.getdefSet("bot")
2023-05-11 16:03:18 +08:00
bot = { ...defbot, ...bot }
return bot
}
get other () {
2023-07-20 14:17:06 +08:00
return this.getConfig("other")
2023-05-11 16:03:18 +08:00
}
get redis () {
2023-07-20 14:17:06 +08:00
return this.getConfig("redis")
2023-05-11 16:03:18 +08:00
}
get renderer() {
2023-07-20 14:17:06 +08:00
return this.getConfig("renderer");
2023-05-11 16:03:18 +08:00
}
/** 主人账号 */
get masterQQ () {
2023-07-20 14:17:06 +08:00
let masterQQ = this.getConfig("other").masterQQ || []
2023-05-11 16:03:18 +08:00
if (!Array.isArray(masterQQ))
masterQQ = [masterQQ]
const masters = []
for (const i of masterQQ)
masters.push(Number(i) || String(i))
return masters
}
/** Bot账号:[主人帐号] */
get master () {
2023-07-20 14:17:06 +08:00
let master = this.getConfig("other").master || []
2023-05-11 16:03:18 +08:00
if (!Array.isArray(master))
master = [master]
const masters = {}
for (let i of master) {
2023-07-20 14:17:06 +08:00
i = i.split(":")
2023-05-11 16:03:18 +08:00
if (Array.isArray(masters[i[0]]))
masters[i[0]].push(i[1])
else
masters[i[0]] = [i[1]]
}
return masters
}
/** 机器人账号 */
get uin () {
return Object.keys(this.master)
}
get qq () {
return this.uin
}
/** package.json */
get package () {
if (this._package) return this._package
2023-09-04 13:46:08 +08:00
this._package = JSON.parse(fs.readFileSync("package.json", "utf8"))
2023-05-11 16:03:18 +08:00
return this._package
}
/** 群配置 */
2023-07-20 14:17:06 +08:00
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}`],
2023-05-11 16:03:18 +08:00
}
}
/** other配置 */
getOther () {
2023-07-20 14:17:06 +08:00
let def = this.getdefSet("other")
let config = this.getConfig("other")
2023-05-11 16:03:18 +08:00
return { ...def, ...config }
}
/**
* @param app 功能
* @param name 配置文件名称
*/
getdefSet (name) {
2023-07-20 14:17:06 +08:00
return this.getYaml("default_config", name)
2023-05-11 16:03:18 +08:00
}
/** 用户配置 */
getConfig (name) {
2023-07-20 14:17:06 +08:00
return this.getYaml("config", name)
2023-05-11 16:03:18 +08:00
}
/**
* 获取配置yaml
* @param type 默认跑配置-defSet用户配置-config
* @param name 名称
*/
getYaml (type, name) {
2023-09-04 13:46:08 +08:00
let file = `config/${type}/${name}.yaml`
2023-05-11 16:03:18 +08:00
let key = `${type}.${name}`
if (this.config[key]) return this.config[key]
this.config[key] = YAML.parse(
2023-07-20 14:17:06 +08:00
fs.readFileSync(file, "utf8")
2023-05-11 16:03:18 +08:00
)
this.watch(file, name, type)
return this.config[key]
}
/** 监听配置文件 */
2023-07-20 14:17:06 +08:00
watch (file, name, type = "default_config") {
2023-05-11 16:03:18 +08:00
let key = `${type}.${name}`
if (this.watcher[key]) return
const watcher = chokidar.watch(file)
2023-07-20 14:17:06 +08:00
watcher.on("change", path => {
2023-05-11 16:03:18 +08:00
delete this.config[key]
2023-07-20 14:17:06 +08:00
if (typeof Bot == "undefined") return
2023-05-11 16:03:18 +08:00
logger.mark(`[修改配置文件][${type}][${name}]`)
if (this[`change_${name}`]) {
this[`change_${name}`]()
}
})
this.watcher[key] = watcher
}
async change_bot () {
/** 修改日志等级 */
2023-07-20 14:17:06 +08:00
let log = await import("./log.js")
2023-05-11 16:03:18 +08:00
log.default()
}
}
2023-07-20 14:17:06 +08:00
export default new Cfg()