From 479e8c9d256b111ccb4bfd73312aba2df523beba Mon Sep 17 00:00:00 2001
From: idranme <96647698+idranme@users.noreply.github.com>
Date: Sat, 9 Nov 2024 22:21:04 +0800
Subject: [PATCH] optimize

---
 package.json          |  8 ++++----
 src/main/main.ts      |  2 --
 src/ntqqapi/hook.ts   |  4 ++--
 src/ntqqapi/ntcall.ts | 38 ++++++++++++--------------------------
 4 files changed, 18 insertions(+), 34 deletions(-)

diff --git a/package.json b/package.json
index 2be116a..9b1837b 100644
--- a/package.json
+++ b/package.json
@@ -26,7 +26,7 @@
     "cosmokit": "^1.6.3",
     "express": "^5.0.1",
     "fluent-ffmpeg": "^2.1.3",
-    "minato": "^3.6.0",
+    "minato": "^3.6.1",
     "protobufjs": "^7.4.0",
     "silk-wasm": "^3.6.3",
     "ts-case-convert": "^2.1.0",
@@ -35,14 +35,14 @@
   "devDependencies": {
     "@types/cors": "^2.8.17",
     "@types/express": "^5.0.0",
-    "@types/fluent-ffmpeg": "^2.1.26",
+    "@types/fluent-ffmpeg": "^2.1.27",
     "@types/node": "^20.14.15",
-    "@types/ws": "^8.5.12",
+    "@types/ws": "^8.5.13",
     "electron": "^31.4.0",
     "electron-vite": "^2.3.0",
     "protobufjs-cli": "^1.1.3",
     "typescript": "^5.6.3",
-    "vite": "^5.4.10 ",
+    "vite": "^5.4.10",
     "vite-plugin-cp": "^4.0.8"
   },
   "packageManager": "yarn@4.5.1"
diff --git a/src/main/main.ts b/src/main/main.ts
index 120f7ba..4a72919 100644
--- a/src/main/main.ts
+++ b/src/main/main.ts
@@ -35,7 +35,6 @@ import {
   NTQQWindowApi
 } from '../ntqqapi/api'
 import { existsSync, mkdirSync } from 'node:fs'
-import { checkChanelId } from '@/ntqqapi/ntcall'
 
 declare module 'cordis' {
   interface Events {
@@ -185,7 +184,6 @@ function onLoad() {
 
       if (config.enableLLOB && (config.satori.enable || config.ob11.enable)) {
         startHook()
-        await checkChanelId()
         await ctx.sleep(600)
       } else {
         llonebotError.otherError = 'LLOneBot 未启动'
diff --git a/src/ntqqapi/hook.ts b/src/ntqqapi/hook.ts
index 59e9c2c..66fe858 100644
--- a/src/ntqqapi/hook.ts
+++ b/src/ntqqapi/hook.ts
@@ -1,4 +1,4 @@
-import { invoke, NTChannel, NTMethod } from './ntcall'
+import { NTMethod } from './ntcall'
 import { log } from '@/common/utils'
 import { randomUUID } from 'node:crypto'
 import { ipcMain } from 'electron'
@@ -40,7 +40,7 @@ const callHooks: Array<{
 }> = []
 
 export function startHook() {
-  log("start hook")
+  log('start hook')
 
   const senderExclude = Symbol()
 
diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts
index 0e1c64e..431937b 100644
--- a/src/ntqqapi/ntcall.ts
+++ b/src/ntqqapi/ntcall.ts
@@ -1,6 +1,6 @@
 import { ipcMain } from 'electron'
-import { hookApiCallbacks, ReceiveCmdS, registerReceiveHook, removeReceiveHook } from './hook'
-import { getBuildVersion, log } from '../common/utils'
+import { hookApiCallbacks, registerReceiveHook, removeReceiveHook } from './hook'
+import { log } from '../common/utils'
 import { randomUUID } from 'node:crypto'
 import {
   GeneralCallResult,
@@ -17,8 +17,6 @@ import {
   NodeIKernelRobotService,
   NodeIKernelNodeMiscService
 } from './services'
-import { CategoryFriend, SimpleInfo, UserDetailInfoByUin } from '@/ntqqapi/types'
-import { selfInfo } from '@/common/globalVars'
 
 export enum NTClass {
   NT_API = 'ns-ntApi',
@@ -110,38 +108,26 @@ interface InvokeOptions<ReturnType> {
   timeout?: number
 }
 
-let availableChannel: NTChannel | undefined = undefined;
+let channel: NTChannel
 
-export async function checkChanelId(){
-  async function testChannel(channel: NTChannel) {
-    await invoke<UserDetailInfoByUin>(
-      'nodeIKernelProfileService/getUserDetailInfoByUin',
-      [{ uin: selfInfo.uin }],
-      { timeout: 1000, channel }
-    )
+function getChannel() {
+  if (channel) {
+    return channel
   }
-
-  for (const channel of [NTChannel.IPC_UP_2, NTChannel.IPC_UP_3]) {
-    // const channel = `IPC_UP_${windowId}` as NTChannel
-    try {
-      await testChannel(channel)
-      log(`check channel ${channel} success`)
-      availableChannel = channel
-      return
-    } catch (e) {
-      log(`check channel ${channel} failed`, e)
-    }
+  if (ipcMain.eventNames().includes(NTChannel.IPC_UP_2)) {
+    return channel = NTChannel.IPC_UP_2
+  } else {
+    return channel = NTChannel.IPC_UP_3
   }
-  availableChannel = getBuildVersion() >= 28788 ? NTChannel.IPC_UP_3 : NTChannel.IPC_UP_2
 }
+
 export function invoke<
   R extends Awaited<ReturnType<Extract<NTService[S][M], (...args: any) => unknown>>>,
   S extends keyof NTService = any,
   M extends keyof NTService[S] & string = any
 >(method: Extract<unknown, `${S}/${M}`> | string, args: unknown[], options: InvokeOptions<R> = {}) {
   const className = options.className ?? NTClass.NT_API
-  // const channel = options.channel ?? getBuildVersion() >= 28788 ? NTChannel.IPC_UP_3 : NTChannel.IPC_UP_2
-  const channel = options.channel ?? availableChannel!
+  const channel = options.channel ?? getChannel()
   const timeout = options.timeout ?? 5000
   const afterFirstCmd = options.afterFirstCmd ?? true
   let eventName = className + '-' + channel[channel.length - 1]