Miao-Yunzai/src/config/config.ts

252 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-06-09 01:00:07 +08:00
import YAML from 'yaml'
import chokidar from 'chokidar'
2024-06-09 20:02:18 +08:00
import { join } from 'node:path'
2024-06-11 20:13:21 +08:00
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync } from 'node:fs'
2024-06-09 20:02:18 +08:00
import { CONFIG_DEFAULT_PATH, CONFIG_INIT_PATH } from './system.js'
2024-06-09 01:00:07 +08:00
2024-06-09 11:40:24 +08:00
/**
* ********
*
* ********
*/
2024-06-12 09:35:17 +08:00
class ConfigController {
2024-06-11 19:45:04 +08:00
/**
*
*/
2024-06-09 11:40:24 +08:00
config = {}
2024-06-09 01:00:07 +08:00
2024-06-11 19:45:04 +08:00
/**
*
*/
2024-06-09 11:40:24 +08:00
watcher = { config: {}, defSet: {} }
2024-06-09 01:00:07 +08:00
2024-06-11 19:45:04 +08:00
/**
*
*/
2024-06-09 20:02:18 +08:00
constructor() {
2024-06-09 01:00:07 +08:00
this.initCfg()
}
2024-06-09 20:02:18 +08:00
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 20:02:18 +08:00
initCfg() {
const path = CONFIG_INIT_PATH
const pathDef = CONFIG_DEFAULT_PATH
2024-06-11 20:13:21 +08:00
const files = readdirSync(pathDef).filter(file => file.endsWith('.yaml'))
2024-06-09 01:00:07 +08:00
for (let file of files) {
2024-06-11 20:13:21 +08:00
if (!existsSync(`${path}${file}`)) {
copyFileSync(`${pathDef}${file}`, `${path}${file}`)
2024-06-09 01:00:07 +08:00
}
}
2024-06-11 20:13:21 +08:00
if (!existsSync("data")) mkdirSync("data")
if (!existsSync("resources")) mkdirSync("resources")
2024-06-09 01:00:07 +08:00
}
2024-06-09 11:40:24 +08:00
/**
* qq号
*/
2024-06-09 20:02:18 +08:00
get qq() {
2024-06-09 01:00:07 +08:00
return Number(this.getConfig('qq').qq)
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 20:02:18 +08:00
get pwd() {
2024-06-09 01:00:07 +08:00
return this.getConfig('qq').pwd
}
2024-06-09 11:40:24 +08:00
/**
* icqq配置
*/
2024-06-09 20:02:18 +08:00
get bot() {
2024-06-09 01:00:07 +08:00
let bot = this.getConfig('bot')
let defbot = this.getdefSet('bot')
2024-06-09 20:02:18 +08:00
const Config = { ...defbot, ...bot }
Config.platform = this.getConfig('qq').platform
/**
* data目录pm2运行时目录不对
*/
Config.data_dir = join(process.cwd(), `/data/icqq/${this.qq}`)
if (!Config.ffmpeg_path) delete Config.ffmpeg_path
if (!Config.ffprobe_path) delete Config.ffprobe_path
return Config
2024-06-09 01:00:07 +08:00
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 20:02:18 +08:00
get other() {
2024-06-09 01:00:07 +08:00
return this.getConfig('other')
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 20:02:18 +08:00
get redis() {
2024-06-09 01:00:07 +08:00
return this.getConfig('redis')
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:07 +08:00
get renderer() {
return this.getConfig('renderer');
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:07 +08:00
get notice() {
return this.getConfig('notice');
}
2024-06-09 11:40:24 +08:00
/**
* qq
*/
2024-06-09 20:02:18 +08:00
get masterQQ() {
2024-06-09 21:21:20 +08:00
const qqs = this.getConfig('other')?.masterQQ || []
if (Array.isArray(qqs)) {
return qqs.map(qq => String(qq))
2024-06-09 01:00:07 +08:00
} else {
2024-06-09 21:21:20 +08:00
return [String(qqs)]
2024-06-09 01:00:07 +08:00
}
}
2024-06-09 20:02:18 +08:00
_package = null
2024-06-09 11:40:24 +08:00
/**
* package.json
*/
2024-06-09 20:02:18 +08:00
get package() {
2024-06-09 01:00:07 +08:00
if (this._package) return this._package
2024-06-09 20:02:18 +08:00
try {
2024-06-11 20:13:21 +08:00
const data = readFileSync('package.json', 'utf8')
2024-06-09 20:02:18 +08:00
this._package = JSON.parse(data)
return this._package
} catch {
return {}
}
2024-06-09 01:00:07 +08:00
}
2024-06-09 11:40:24 +08:00
/**
*
* @param groupId
* @returns
*/
2024-06-09 20:02:18 +08:00
getGroup(groupId = '') {
const config = this.getConfig('group')
const defCfg = this.getdefSet('group')
2024-06-09 01:00:07 +08:00
if (config[groupId]) {
return { ...defCfg.default, ...config.default, ...config[groupId] }
}
return { ...defCfg.default, ...config.default }
}
2024-06-09 11:40:24 +08:00
/**
* other配置
* @returns
*/
2024-06-09 20:02:18 +08:00
getOther() {
const def = this.getdefSet('other')
const config = this.getConfig('other')
2024-06-09 01:00:07 +08:00
return { ...def, ...config }
}
2024-06-09 11:40:24 +08:00
/**
* notice配置
* @returns
*/
2024-06-09 20:02:18 +08:00
getNotice() {
const def = this.getdefSet('notice')
const config = this.getConfig('notice')
2024-06-09 01:00:07 +08:00
return { ...def, ...config }
}
/**
2024-06-09 20:02:18 +08:00
*
2024-06-09 01:00:07 +08:00
* @param name
2024-06-09 11:40:24 +08:00
* @returns
2024-06-09 01:00:07 +08:00
*/
2024-06-09 20:02:18 +08:00
getdefSet(name: string) {
2024-06-09 01:00:07 +08:00
return this.getYaml('default_config', name)
}
2024-06-09 11:40:24 +08:00
/**
2024-06-09 20:02:18 +08:00
*
2024-06-09 11:40:24 +08:00
* @param name
* @returns
*/
2024-06-09 20:02:18 +08:00
getConfig(name: string) {
2024-06-09 01:00:07 +08:00
return this.getYaml('config', name)
}
/**
* yaml
* @param type -defSet-config
* @param name
*/
2024-06-09 20:02:18 +08:00
getYaml(type, name) {
2024-06-09 01:00:07 +08:00
let file = `config/${type}/${name}.yaml`
let key = `${type}.${name}`
if (this.config[key]) return this.config[key]
this.config[key] = YAML.parse(
2024-06-11 20:13:21 +08:00
readFileSync(file, 'utf8')
2024-06-09 01:00:07 +08:00
)
this.watch(file, name, type)
return this.config[key]
}
2024-06-09 11:40:24 +08:00
/**
*
* @param file
* @param name
* @param type
* @returns
*/
2024-06-11 20:13:21 +08:00
watch(file: string, name: string, type = 'default_config') {
2024-06-09 20:02:18 +08:00
const key = `${type}.${name}`
2024-06-09 01:00:07 +08:00
if (this.watcher[key]) return
const watcher = chokidar.watch(file)
2024-06-09 20:02:18 +08:00
watcher.on('change', () => {
2024-06-09 01:00:07 +08:00
delete this.config[key]
if (typeof Bot == 'undefined') return
logger.mark(`[修改配置文件][${type}][${name}]`)
if (this[`change_${name}`]) {
this[`change_${name}`]()
}
})
this.watcher[key] = watcher
}
2024-06-09 11:40:24 +08:00
/**
*
* @returns
*/
2024-06-09 20:02:18 +08:00
change_qq() {
2024-06-09 01:00:07 +08:00
if (process.argv.includes('login') || !this.qq) return
logger.info('修改机器人QQ或密码请手动重启')
}
2024-06-09 11:40:24 +08:00
/**
2024-06-09 20:02:18 +08:00
*
2024-06-09 11:40:24 +08:00
*/
2024-06-09 20:02:18 +08:00
async change_bot() {
2024-06-12 09:35:17 +08:00
const { setLogger } = await import('./log.js')
setLogger && setLogger()
2024-06-09 01:00:07 +08:00
}
2024-06-09 11:40:24 +08:00
2024-06-09 01:00:07 +08:00
}
2024-06-09 11:40:24 +08:00
/**
* **********
*
* ***
*/
2024-06-12 09:35:17 +08:00
export default new ConfigController()