diff --git a/src/common/db.ts b/src/common/db.ts index 9cdb597..0449b91 100644 --- a/src/common/db.ts +++ b/src/common/db.ts @@ -27,33 +27,12 @@ class DBUtil { * */ constructor() { - let initCount = 0 - new Promise((resolve, reject) => { - const initDB = () => { - initCount++ - // if (initCount > 50) { - // return reject("init db fail") - // } - - try { - if (!selfInfo.uin) { - setTimeout(initDB, 300) - return - } - const DB_PATH = DATA_DIR + `/msg_${selfInfo.uin}` - this.db = new Level(DB_PATH, { valueEncoding: 'json' }) - console.log('llonebot init db success') - resolve(null) - } catch (e: any) { - console.log('init db fail', e.stack.toString()) - setTimeout(initDB, 300) - } - } - setTimeout(initDB) - }).then() + } + init(uin: string) { + const DB_PATH = DATA_DIR + `/msg_${uin}` + this.db = new Level(DB_PATH, { valueEncoding: 'json' }) const expiredMilliSecond = 1000 * 60 * 60 - setInterval(() => { // this.cache = {} // 清理时间较久的缓存 diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index 2acd233..2e60cd9 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -25,7 +25,7 @@ export function checkFileReceived(path: string, timeout: number = 3000): Promise } else if (Date.now() - startTime > timeout) { reject(new Error(`文件不存在: ${path}`)) } else { - setTimeout(check, 100) + setTimeout(check, 200) } } diff --git a/src/main/main.ts b/src/main/main.ts index 42000b8..440119c 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -45,12 +45,12 @@ import { GroupDecreaseSubType, OB11GroupDecreaseEvent } from '../onebot11/event/ import '../ntqqapi/wrapper' import { NTEventDispatch } from '../common/utils/EventTask' import { wrapperConstructor, getSession } from '../ntqqapi/wrapper' +import { sleep } from '../common/utils/helper' let mainWindow: BrowserWindow | null = null // 加载插件时触发 function onLoad() { - log('llonebot main onLoad') ipcMain.handle(CHANNEL_CHECK_VERSION, async (event, arg) => { return checkNewVersion() }) @@ -428,6 +428,7 @@ function onLoad() { } llonebotError.otherError = '' startTime = Date.now() + dbUtil.init(selfInfo.uin) NTEventDispatch.init({ ListenerMap: wrapperConstructor, WrapperSession: getSession()! }) log('start activate group member info') NTQQGroupApi.activateMemberInfoChange().then().catch(log) @@ -452,21 +453,12 @@ function onLoad() { let getSelfNickCount = 0 const init = async () => { - try { - log('start get self info') - const _ = await NTQQUserApi.getSelfInfo() - log('get self info api result:', _) - Object.assign(selfInfo, _) - selfInfo.nick = selfInfo.uin - } catch (e) { - log('retry get self info', e) - } if (!selfInfo.uin) { selfInfo.uin = globalThis.authData?.uin selfInfo.uid = globalThis.authData?.uid selfInfo.nick = selfInfo.uin } - log('self info', selfInfo, globalThis.authData) + //log('self info', selfInfo, globalThis.authData) if (selfInfo.uin) { async function getUserNick() { try { @@ -481,18 +473,19 @@ function onLoad() { log('get self nickname failed', e.stack) } if (getSelfNickCount < 10) { - return setTimeout(getUserNick, 1000) + await sleep(1000) + return await getUserNick() } } - getUserNick().then() - start().then() + await getUserNick() + start() } else { setTimeout(init, 1000) } } - setTimeout(init, 1000) + init() } // 创建窗口时触发 diff --git a/src/ntqqapi/constructor.ts b/src/ntqqapi/constructor.ts index 31987e8..949d6e2 100644 --- a/src/ntqqapi/constructor.ts +++ b/src/ntqqapi/constructor.ts @@ -175,7 +175,6 @@ export class SendMsgElementConstructor { setTimeout(useDefaultThumb, 5000) ffmpeg(filePath) - .on('end', () => { }) .on('error', (err) => { if (diyThumbPath) { fs.copyFile(diyThumbPath, thumbPath) diff --git a/src/ntqqapi/hook.ts b/src/ntqqapi/hook.ts index df22738..b1432d2 100644 --- a/src/ntqqapi/hook.ts +++ b/src/ntqqapi/hook.ts @@ -242,18 +242,7 @@ async function updateGroups(_groups: Group[], needUpdate: boolean = true) { continue } log('update group', group) - // if (!activatedGroups.includes(group.groupCode)) { - NTQQMsgApi.activateChat({ peerUid: group.groupCode, chatType: ChatType.group }) - .then((r) => { - // activatedGroups.push(group.groupCode); - // log(`激活群聊天窗口${group.groupName}(${group.groupCode})`, r) - // if (r.result !== 0) { - // setTimeout(() => NTQQMsgApi.activateGroupChat(group.groupCode).then(r => log(`再次激活群聊天窗口${group.groupName}(${group.groupCode})`, r)), 500); - // }else { - // } - }) - .catch(log) - // } + NTQQMsgApi.activateChat({ peerUid: group.groupCode, chatType: ChatType.group }).then().catch(log) let existGroup = groups.find((g) => g.groupCode == group.groupCode) if (existGroup) { Object.assign(existGroup, group)