From 1307679dae2cfc8386e379513a5348075f2dcfe9 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Wed, 20 Mar 2024 18:25:09 +0800 Subject: [PATCH] fix: stranger info --- src/common/config.ts | 2 +- src/ntqqapi/api/index.ts | 7 +++ src/ntqqapi/hook.ts | 62 ++++++++++--------- src/ntqqapi/types/user.ts | 49 ++++++++++++++- src/onebot11/action/Debug.ts | 29 --------- .../action/go-cqhttp/GetStrangerInfo.ts | 26 ++------ src/onebot11/action/index.ts | 2 +- src/onebot11/action/llonebot/Debug.ts | 31 ++++++++++ src/onebot11/constructor.ts | 17 ++++- src/onebot11/types.ts | 5 +- 10 files changed, 146 insertions(+), 84 deletions(-) create mode 100644 src/ntqqapi/api/index.ts delete mode 100644 src/onebot11/action/Debug.ts create mode 100644 src/onebot11/action/llonebot/Debug.ts diff --git a/src/common/config.ts b/src/common/config.ts index d8336e2..39269cb 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 = true; +export const HOOK_LOG = false; export const ALLOW_SEND_TEMP_MSG = false; diff --git a/src/ntqqapi/api/index.ts b/src/ntqqapi/api/index.ts new file mode 100644 index 0000000..461f2a7 --- /dev/null +++ b/src/ntqqapi/api/index.ts @@ -0,0 +1,7 @@ +export * from "./file"; +export * from "./friend"; +export * from "./group"; +export * from "./msg"; +export * from "./user"; +export * from "./webapi"; +export * from "./window"; diff --git a/src/ntqqapi/hook.ts b/src/ntqqapi/hook.ts index efe9105..e66c292 100644 --- a/src/ntqqapi/hook.ts +++ b/src/ntqqapi/hook.ts @@ -61,6 +61,7 @@ let receiveHooks: Array<{ export function hookNTQQApiReceive(window: BrowserWindow) { const originalSend = window.webContents.send; const patchSend = (channel: string, ...args: NTQQApiReturnData) => { + console.log("hookNTQQApiReceive", channel, args) let isLogger = false try { isLogger = args[0]?.eventName?.startsWith("ns-LoggerApi") @@ -69,43 +70,47 @@ export function hookNTQQApiReceive(window: BrowserWindow) { } if (!isLogger) { try { - HOOK_LOG && log(`received ntqq api message: ${channel}`, JSON.stringify(args)) + HOOK_LOG && log(`received ntqq api message: ${channel}`, args) }catch (e) { - + log("hook log error", e, args) } } - if (args?.[1] instanceof Array) { - for (let receiveData of args?.[1]) { - const ntQQApiMethodName = receiveData.cmdName; - // log(`received ntqq api message: ${channel} ${ntQQApiMethodName}`, JSON.stringify(receiveData)) - for (let hook of receiveHooks) { - if (hook.method.includes(ntQQApiMethodName)) { - new Promise((resolve, reject) => { - try { - let _ = hook.hookFunc(receiveData.payload) - if (hook.hookFunc.constructor.name === "AsyncFunction") { - (_ as Promise).then() + try { + if (args?.[1] instanceof Array) { + for (let receiveData of args?.[1]) { + const ntQQApiMethodName = receiveData.cmdName; + // log(`received ntqq api message: ${channel} ${ntQQApiMethodName}`, JSON.stringify(receiveData)) + for (let hook of receiveHooks) { + if (hook.method.includes(ntQQApiMethodName)) { + new Promise((resolve, reject) => { + try { + let _ = hook.hookFunc(receiveData.payload) + if (hook.hookFunc.constructor.name === "AsyncFunction") { + (_ as Promise).then() + } + } catch (e) { + log("hook error", e, receiveData.payload) } - } catch (e) { - log("hook error", e, receiveData.payload) - } - }).then() + }).then() + } } } } - } - if (args[0]?.callbackId) { - // log("hookApiCallback", hookApiCallbacks, args) - const callbackId = args[0].callbackId; - if (hookApiCallbacks[callbackId]) { - // log("callback found") - new Promise((resolve, reject) => { - hookApiCallbacks[callbackId](args[1]); - }).then() - delete hookApiCallbacks[callbackId]; + if (args[0]?.callbackId) { + // log("hookApiCallback", hookApiCallbacks, args) + const callbackId = args[0].callbackId; + if (hookApiCallbacks[callbackId]) { + // log("callback found") + new Promise((resolve, reject) => { + hookApiCallbacks[callbackId](args[1]); + }).then() + delete hookApiCallbacks[callbackId]; + } } + }catch (e) { + log("hookNTQQApiReceive error", e.stack.toString(), args) } - return originalSend.call(window.webContents, channel, ...args); + originalSend.call(window.webContents, channel, ...args); } window.webContents.send = patchSend; } @@ -117,6 +122,7 @@ export function hookNTQQApiCall(window: BrowserWindow) { const proxyIpcMsg = new Proxy(ipc_message_proxy, { apply(target, thisArg, args) { + console.log(thisArg, args); let isLogger = false try { isLogger = args[3][0].eventName.startsWith("ns-LoggerApi") diff --git a/src/ntqqapi/types/user.ts b/src/ntqqapi/types/user.ts index 891b8a4..d316619 100644 --- a/src/ntqqapi/types/user.ts +++ b/src/ntqqapi/types/user.ts @@ -18,7 +18,54 @@ export interface User { longNick?: string; // 签名 remark?: string; sex?: Sex; - "qqLevel"?: QQLevel + qqLevel?: QQLevel, + qid?: string + "birthday_year"?: number, + "birthday_month"?: number, + "birthday_day"?: number, + "topTime"?: string, + "constellation"?: number, + "shengXiao"?: number, + "kBloodType"?: number, + "homeTown"?: string, //"0-0-0", + "makeFriendCareer"?: number, + "pos"?: string, + "eMail"?: string + "phoneNum"?: string, + "college"?: string, + "country"?: string, + "province"?: string, + "city"?: string, + "postCode"?: string, + "address"?: string, + "isBlock"?: boolean, + "isSpecialCareOpen"?: boolean, + "isSpecialCareZone"?: boolean, + "ringId"?: string, + "regTime"?: number, + interest?: string, + "labels"?: string[], + "isHideQQLevel"?: number, + "privilegeIcon"?: { + "jumpUrl": string, + "openIconList": unknown[], + "closeIconList": unknown[] + }, + "photoWall"?: { + "picList": unknown[] + }, + "vipFlag"?: boolean, + "yearVipFlag"?: boolean, + "svipFlag"?: boolean, + "vipLevel"?: number, + "status"?: number, + "qidianMasterFlag"?: number, + "qidianCrewFlag"?: number, + "qidianCrewFlag2"?: number, + "extStatus"?: number, + "recommendImgFlag"?: number, + "disableEmojiShortCuts"?: number, + "pendantId"?: string, } export interface SelfInfo extends User { diff --git a/src/onebot11/action/Debug.ts b/src/onebot11/action/Debug.ts deleted file mode 100644 index e3064d6..0000000 --- a/src/onebot11/action/Debug.ts +++ /dev/null @@ -1,29 +0,0 @@ -import BaseAction from "./BaseAction"; -import {NTQQApi} from "../../ntqqapi/ntcall"; -import {ActionName} from "./types"; - -import {log} from "../../common/utils/log"; - -interface Payload { - method: string, - args: any[], -} - -export default class Debug extends BaseAction { - actionName = ActionName.Debug - - protected async _handle(payload: Payload): Promise { - log("debug call ntqq api", payload); - const method = NTQQApi[payload.method] - if (!method) { - throw `${method} 不存在` - } - const result = method(...payload.args); - if (method.constructor.name === "AsyncFunction") { - return await result - } - return result - // const info = await NTQQApi.getUserDetailInfo(friends[0].uid); - // return info - } -} \ No newline at end of file diff --git a/src/onebot11/action/go-cqhttp/GetStrangerInfo.ts b/src/onebot11/action/go-cqhttp/GetStrangerInfo.ts index 340742f..80fab52 100644 --- a/src/onebot11/action/go-cqhttp/GetStrangerInfo.ts +++ b/src/onebot11/action/go-cqhttp/GetStrangerInfo.ts @@ -1,36 +1,20 @@ import BaseAction from "../BaseAction"; import {OB11User} from "../../types"; -import {getFriend, getGroupMember, groups} from "../../../common/data"; +import {getUidByUin, uidMaps} from "../../../common/data"; import {OB11Constructor} from "../../constructor"; import {ActionName} from "../types"; -import {isNull, log} from "../../../common/utils"; import {NTQQUserApi} from "../../../ntqqapi/api/user"; -import {Friend, GroupMember} from "../../../ntqqapi/types"; export default class GoCQHTTPGetStrangerInfo extends BaseAction<{ user_id: number }, OB11User> { actionName = ActionName.GoCQHTTP_GetStrangerInfo - private async refreshInfo(user: Friend | GroupMember){ - if (isNull(user.sex)){ - let info = (await NTQQUserApi.getUserDetailInfo(user.uid)) - Object.assign(user, info); - } - } protected async _handle(payload: { user_id: number }): Promise { const user_id = payload.user_id.toString() - const friend = await getFriend(user_id) - if (friend) { - await this.refreshInfo(friend); - return OB11Constructor.friend(friend); + const uid = getUidByUin(user_id) + if (!uid) { + throw new Error("查无此人") } - for (const group of groups) { - const member = await getGroupMember(group.groupCode, user_id) - if (member) { - await this.refreshInfo(member); - return OB11Constructor.groupMember(group.groupCode, member) as OB11User - } - } - throw new Error("查无此人") + return OB11Constructor.stranger(await NTQQUserApi.getUserDetailInfo(uid)) } } \ No newline at end of file diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index dff68b4..6461779 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -20,7 +20,7 @@ import SendLike from "./SendLike"; import SetGroupAddRequest from "./SetGroupAddRequest"; import SetGroupLeave from "./SetGroupLeave"; import GetGuildList from "./GetGuildList"; -import Debug from "./Debug"; +import Debug from "./llonebot/Debug"; import SetFriendAddRequest from "./SetFriendAddRequest"; import SetGroupWholeBan from "./SetGroupWholeBan"; import SetGroupName from "./SetGroupName"; diff --git a/src/onebot11/action/llonebot/Debug.ts b/src/onebot11/action/llonebot/Debug.ts new file mode 100644 index 0000000..7402612 --- /dev/null +++ b/src/onebot11/action/llonebot/Debug.ts @@ -0,0 +1,31 @@ +import BaseAction from "../BaseAction"; +import * as ntqqApi from "../../../ntqqapi/api"; +import {ActionName} from "../types"; +import {log} from "../../../common/utils/log"; + +interface Payload { + method: string, + args: any[], +} + +export default class Debug extends BaseAction { + actionName = ActionName.Debug + + protected async _handle(payload: Payload): Promise { + log("debug call ntqq api", payload); + for (const ntqqApiClass in ntqqApi) { + const method = ntqqApi[ntqqApiClass][payload.method] + if (method) { + const result = method(...payload.args); + if (method.constructor.name === "AsyncFunction") { + return await result + } + return result + } + } + throw `${payload.method}方法 不存在` + + // const info = await NTQQApi.getUserDetailInfo(friends[0].uid); + // return info + } +} \ No newline at end of file diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index fa223c1..7fde2d4 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -311,14 +311,14 @@ export class OB11Constructor { nickname: friend.nick, remark: friend.remark, sex: OB11Constructor.sex(friend.sex), - qq_level: friend.qqLevel && calcQQLevel(friend.qqLevel) || 0 + level: friend.qqLevel && calcQQLevel(friend.qqLevel) || 0 } } static selfInfo(selfInfo: SelfInfo): OB11User { return { user_id: parseInt(selfInfo.uin), - nickname: selfInfo.nick + nickname: selfInfo.nick, } } @@ -365,6 +365,19 @@ export class OB11Constructor { } } + static stranger(user: User): OB11User { + return { + ...user, + user_id: parseInt(user.uin), + nickname: user.nick, + sex: OB11Constructor.sex(user.sex), + age: 0, + qid: user.qid, + login_days: 0, + level: user.qqLevel && calcQQLevel(user.qqLevel) || 0, + } + } + static groupMembers(group: Group): OB11GroupMember[] { log("construct ob11 group members", group) return group.members.map(m => OB11Constructor.groupMember(group.groupCode, m)) diff --git a/src/onebot11/types.ts b/src/onebot11/types.ts index db7bdf5..bb6f09c 100644 --- a/src/onebot11/types.ts +++ b/src/onebot11/types.ts @@ -6,7 +6,10 @@ export interface OB11User { nickname: string; remark?: string; sex?: OB11UserSex; - qq_level?: number; + level?: number; + age?: number; + qid?: string; + login_days?: number; } export enum OB11UserSex {