diff --git a/src/common/config.ts b/src/common/config.ts index f1613b4..84442bb 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -6,7 +6,7 @@ import path from "node:path"; import {selfInfo} from "./data"; import {DATA_DIR} from "./utils"; -export const HOOK_LOG = false; +export const HOOK_LOG = true; export const ALLOW_SEND_TEMP_MSG = false; diff --git a/src/common/utils/log.ts b/src/common/utils/log.ts index 114e5f1..e957851 100644 --- a/src/common/utils/log.ts +++ b/src/common/utils/log.ts @@ -4,16 +4,18 @@ import path from "node:path"; import {DATA_DIR, truncateString} from "./index"; import {getConfigUtil} from "../config"; +const date = new Date(); +const logFileName = `llonebot-${date.toLocaleString("zh-CN")}.log`.replace(/\//g, "-").replace(/:/g, "-"); +const logDir = path.join(DATA_DIR, "logs"); +if (!fs.existsSync(logDir)) { + fs.mkdirSync(logDir, {recursive: true}); +} + export function log(...msg: any[]) { if (!getConfigUtil().getConfig().log) { return //console.log(...msg); } - let currentDateTime = new Date().toLocaleString(); - const date = new Date(); - const year = date.getFullYear(); - const month = date.getMonth() + 1; - const day = date.getDate(); - const currentDate = `${year}-${month}-${day}`; + const userInfo = selfInfo.uin ? `${selfInfo.nick}(${selfInfo.uin})` : "" let logMsg = ""; for (let msgItem of msg) { @@ -25,10 +27,11 @@ export function log(...msg: any[]) { } logMsg += msgItem + " "; } + let currentDateTime = new Date().toLocaleString(); logMsg = `${currentDateTime} ${userInfo}: ${logMsg}\n\n` // sendLog(...msg); // console.log(msg) - fs.appendFile(path.join(DATA_DIR, `llonebot-${currentDate}.log`), logMsg, (err: any) => { + fs.appendFile(path.join(logDir, logFileName), logMsg, (err: any) => { }) } \ No newline at end of file diff --git a/src/main/main.ts b/src/main/main.ts index 1d63eed..deda648 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -389,24 +389,29 @@ function onLoad() { selfInfo.nick = selfInfo.uin; } catch (e) { log("retry get self info", e); + selfInfo.uin = globalThis.authData?.uin; + selfInfo.uid = globalThis.authData?.uid; + selfInfo.nick = selfInfo.uin; } - log("self info", selfInfo); + log("self info", selfInfo, globalThis.authData); if (selfInfo.uin) { - try { - const userInfo = (await NTQQUserApi.getUserDetailInfo(selfInfo.uid)); - log("self info", userInfo); - if (userInfo) { - selfInfo.nick = userInfo.nick; - } else { + async function getUserNick(){ + try { getSelfNickCount++; - if (getSelfNickCount < 10) { - return setTimeout(init, 1000); + const userInfo = (await NTQQUserApi.getUserDetailInfo(selfInfo.uid)); + log("self info", userInfo); + if (userInfo) { + selfInfo.nick = userInfo.nick; + return } + } catch (e) { + log("get self nickname failed", e.stack); + } + if (getSelfNickCount < 10) { + return setTimeout(getUserNick, 1000); } - } catch (e) { - log("get self nickname failed", e.toString()); - return setTimeout(init, 1000); } + getUserNick().then() start().then(); } else { setTimeout(init, 1000)