Miao-Yunzai/lib/config/init.js

93 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-12-27 22:54:38 +08:00
import createQQ from "./qq.js"
import setLog from "./log.js"
import redisInit from "./redis.js"
import { checkRun } from "./check.js"
import fs from "node:fs"
import yaml from "yaml"
2023-03-08 01:54:36 +08:00
2023-04-26 04:46:39 +08:00
/** 设置标题 */
2023-12-27 22:54:38 +08:00
process.title = "Miao-Yunzai"
2023-04-26 04:46:39 +08:00
async function UpdateTitle() {
// 添加一些多余的标题内容
2023-12-27 22:54:38 +08:00
let title = "Miao-Yunzai"
let qq = await fs.promises.readFile("./config/config/qq.yaml", "UTF-8").then(yaml.parse).catch(() => null)
2023-04-26 04:46:39 +08:00
if (qq) {
2023-12-27 22:54:38 +08:00
title += `@${qq.qq || ""}`
2023-04-26 04:46:39 +08:00
switch (qq.platform) {
case 1:
2023-12-27 22:54:38 +08:00
title += " 安卓手机"
2023-04-26 04:46:39 +08:00
break
case 2:
2023-12-27 22:54:38 +08:00
title += " aPad"
2023-04-26 04:46:39 +08:00
break
case 3:
2023-12-27 22:54:38 +08:00
title += " 安卓手表"
2023-04-26 04:46:39 +08:00
break
case 4:
2023-12-27 22:54:38 +08:00
title += " MacOS"
2023-04-26 04:46:39 +08:00
break
case 5:
2023-12-27 22:54:38 +08:00
title += " iPad"
2023-04-26 04:46:39 +08:00
break
case 6:
2023-12-27 22:54:38 +08:00
title += " Tim"
2023-04-26 04:46:39 +08:00
break
default:
}
2023-04-01 13:34:20 +08:00
}
2023-04-26 04:46:39 +08:00
/** 设置标题 */
process.title = title
2023-03-08 14:10:45 +08:00
}
2023-03-04 14:30:13 +08:00
/** 设置时区 */
2023-12-27 22:54:38 +08:00
process.env.TZ = "Asia/Shanghai"
2023-03-04 14:30:13 +08:00
/** 捕获未处理的错误 */
2023-12-27 22:54:38 +08:00
process.on("uncaughtException", error => {
if (typeof logger == "undefined") console.log(error)
else logger.error(error)
})
2023-03-04 14:30:13 +08:00
/** 捕获未处理的Promise错误 */
2023-12-27 22:54:38 +08:00
process.on("unhandledRejection", (error, promise) => {
if (typeof logger == "undefined") console.log(error)
else logger.error(error)
2023-03-04 14:30:13 +08:00
})
/** 退出事件 */
2023-12-27 22:54:38 +08:00
process.on("exit", async code => {
if (typeof redis != "undefined" && typeof test == "undefined")
2023-03-04 14:30:13 +08:00
await redis.save()
2023-12-27 22:54:38 +08:00
if (typeof logger == "undefined")
console.log("Miao-Yunzai 已停止运行")
else
logger.mark(logger.magenta("Miao-Yunzai 已停止运行"))
2023-03-04 14:30:13 +08:00
})
await checkInit()
/** 初始化事件 */
2023-12-27 22:54:38 +08:00
async function checkInit() {
2023-03-04 14:30:13 +08:00
/** 检查node_modules */
2023-12-27 22:54:38 +08:00
if (!fs.existsSync("./node_modules") || !fs.existsSync("./node_modules/icqq")) {
console.log("请先运行命令pnpm install -P 安装依赖")
2023-03-04 14:30:13 +08:00
process.exit()
}
/** 检查qq.yaml */
await createQQ()
/** 日志设置 */
setLog()
2023-12-27 22:54:38 +08:00
logger.mark("Miao-Yunzai 启动中...")
2023-03-04 14:30:13 +08:00
await redisInit()
2023-05-28 15:25:23 +08:00
await checkRun()
//** 更新标题 */
await UpdateTitle()
2023-12-27 22:54:38 +08:00
}