From 1b0b5f349477f426c49a64092c7e52d84e81075a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 6 Aug 2025 19:31:24 +0800 Subject: [PATCH] Refactor PacketApi status checks and fix typos Replaced all usages of PacketApi.available with PacketApi.packetStatus for more accurate status checking. Fixed a typo in getFullQQVesion to getFullQQVersion. Updated plugin_onmessage to fetch and parse messages for a user. These changes improve reliability and consistency in API status handling. --- src/common/qq-basic-info.ts | 2 +- src/core/apis/file.ts | 8 ++++---- src/core/apis/packet.ts | 18 +++++++++++++++--- src/onebot/action/msg/SendMsg.ts | 2 +- src/onebot/action/packet/GetPacketStatus.ts | 2 +- src/onebot/api/msg.ts | 6 +++--- src/onebot/index.ts | 2 +- src/plugin/index.ts | 15 ++++++++++++--- 8 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/common/qq-basic-info.ts b/src/common/qq-basic-info.ts index b468e908..7246935d 100644 --- a/src/common/qq-basic-info.ts +++ b/src/common/qq-basic-info.ts @@ -42,7 +42,7 @@ export class QQBasicInfoWrapper { return this.isQuickUpdate ? this.QQVersionConfig?.buildId : this.QQPackageInfo?.buildVersion; } - getFullQQVesion() { + getFullQQVersion() { const version = this.isQuickUpdate ? this.QQVersionConfig?.curVersion : this.QQPackageInfo?.version; if (!version) throw new Error('QQ版本获取失败'); return version; diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index b92eee23..4725aaf8 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -65,7 +65,7 @@ export class NTQQFileApi { } async getFileUrl(chatType: ChatType, peer: string, fileUUID?: string, file10MMd5?: string | undefined) { - if (this.core.apis.PacketApi.available) { + if (this.core.apis.PacketApi.packetStatus) { try { if (chatType === ChatType.KCHATTYPEGROUP && fileUUID) { return this.core.apis.PacketApi.pkt.operation.GetGroupFileUrl(+peer, fileUUID); @@ -80,7 +80,7 @@ export class NTQQFileApi { } async getPttUrl(peer: string, fileUUID?: string) { - if (this.core.apis.PacketApi.available && fileUUID) { + if (this.core.apis.PacketApi.packetStatus && fileUUID) { let appid = new NapProtoMsg(FileId).decode(Buffer.from(fileUUID.replaceAll('-', '+').replaceAll('_', '/'), 'base64')).appid; try { if (appid && appid === 1403) { @@ -108,7 +108,7 @@ export class NTQQFileApi { } async getVideoUrlPacket(peer: string, fileUUID?: string) { - if (this.core.apis.PacketApi.available && fileUUID) { + if (this.core.apis.PacketApi.packetStatus && fileUUID) { let appid = new NapProtoMsg(FileId).decode(Buffer.from(fileUUID.replaceAll('-', '+').replaceAll('_', '/'), 'base64')).appid; try { if (appid && appid === 1415) { @@ -502,7 +502,7 @@ export class NTQQFileApi { }; try { - if (this.core.apis.PacketApi.available) { + if (this.core.apis.PacketApi.packetStatus) { const rkey_expired_private = !this.packetRkey || (this.packetRkey[0] && this.packetRkey[0].time + Number(this.packetRkey[0].ttl) < Date.now() / 1000); const rkey_expired_group = !this.packetRkey || (this.packetRkey[0] && this.packetRkey[0].time + Number(this.packetRkey[0].ttl) < Date.now() / 1000); if (rkey_expired_private || rkey_expired_group) { diff --git a/src/core/apis/packet.ts b/src/core/apis/packet.ts index 8b573631..99f6a6bd 100644 --- a/src/core/apis/packet.ts +++ b/src/core/apis/packet.ts @@ -21,20 +21,26 @@ export class NTQQPacketApi { qqVersion: string | undefined; pkt!: PacketClientSession; errStack: string[] = []; + packetStatus: boolean = false; constructor(context: InstanceContext, core: NapCatCore) { this.context = context; this.core = core; this.logger = core.context.logger; } + async initApi() { - await this.InitSendPacket(this.context.basicInfoWrapper.getFullQQVesion()) - .then() + this.packetStatus = (await this.InitSendPacket(this.context.basicInfoWrapper.getFullQQVersion()) + .then((result) => { + return result; + }) .catch((err) => { this.logger.logError(err); this.errStack.push(err); - }); + return false; + })) && this.pkt?.available; } + get available(): boolean { return this.pkt?.available ?? false; } @@ -61,6 +67,12 @@ export class NTQQPacketApi { } this.pkt = new PacketClientSession(this.core); await this.pkt.init(process.pid, table.recv, table.send); + try { + await this.pkt.operation.FetchRkey(); + } catch (error) { + this.logger.logError('测试Packet状态异常', error); + return false; + } return true; } } diff --git a/src/onebot/action/msg/SendMsg.ts b/src/onebot/action/msg/SendMsg.ts index 99fd8cb1..60ef8322 100644 --- a/src/onebot/action/msg/SendMsg.ts +++ b/src/onebot/action/msg/SendMsg.ts @@ -130,7 +130,7 @@ export class SendMsgBase extends OneBotAction { ); if (getSpecialMsgNum(payload, OB11MessageDataType.node)) { - const packetMode = this.core.apis.PacketApi.available; + const packetMode = this.core.apis.PacketApi.packetStatus; let returnMsgAndResId: { message: RawMessage | null, res_id?: string } | null; try { returnMsgAndResId = packetMode diff --git a/src/onebot/action/packet/GetPacketStatus.ts b/src/onebot/action/packet/GetPacketStatus.ts index 2630163b..bf0bb3c3 100644 --- a/src/onebot/action/packet/GetPacketStatus.ts +++ b/src/onebot/action/packet/GetPacketStatus.ts @@ -4,7 +4,7 @@ import { ActionName, BaseCheckResult } from '@/onebot/action/router'; export abstract class GetPacketStatusDepends extends OneBotAction { protected override async check(payload: PT): Promise{ - if (!this.core.apis.PacketApi.available) { + if (!this.core.apis.PacketApi.packetStatus) { return { valid: false, message: 'packetBackend不可用,请参照文档 https://napneko.github.io/config/advanced 和启动日志检查packetBackend状态或进行配置!' + diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 00eed17a..888cd53a 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -147,7 +147,7 @@ export class OneBotMsgApi { }; FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, element.fileUuid); FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, element.fileName); - if (this.core.apis.PacketApi.available) { + if (this.core.apis.PacketApi.packetStatus) { let url; try { url = await this.core.apis.FileApi.getFileUrl(msg.chatType, msg.peerUid, element.fileUuid, element.file10MMd5) @@ -391,7 +391,7 @@ export class OneBotMsgApi { //开始兜底 if (!videoDownUrl) { - if (this.core.apis.PacketApi.available) { + if (this.core.apis.PacketApi.packetStatus) { try { videoDownUrl = await this.core.apis.FileApi.getVideoUrlPacket(msg.peerUid, element.fileUuid); } catch (e) { @@ -422,7 +422,7 @@ export class OneBotMsgApi { }; const fileCode = FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, '', element.fileName); let pttUrl = ''; - if (this.core.apis.PacketApi.available) { + if (this.core.apis.PacketApi.packetStatus) { try { pttUrl = await this.core.apis.FileApi.getPttUrl(msg.peerUid, element.fileUuid); } catch (e) { diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 4d6eb932..b529a3de 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -170,7 +170,7 @@ export class NapCatOneBot11Adapter { this.initBuddyListener(); this.initGroupListener(); - WebUiDataRuntime.setQQVersion(this.core.context.basicInfoWrapper.getFullQQVesion()); + WebUiDataRuntime.setQQVersion(this.core.context.basicInfoWrapper.getFullQQVersion()); WebUiDataRuntime.setQQLoginInfo(selfInfo); WebUiDataRuntime.setQQLoginStatus(true); WebUiDataRuntime.setOnOB11ConfigChanged(async (newConfig) => { diff --git a/src/plugin/index.ts b/src/plugin/index.ts index c0c67d3e..8456073e 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -1,8 +1,17 @@ import { NapCatOneBot11Adapter, OB11Message } from '@/onebot'; -import { NapCatCore } from '@/core'; +import { ChatType, NapCatCore } from '@/core'; import { ActionMap } from '@/onebot/action'; import { OB11PluginAdapter } from '@/onebot/network/plugin'; export const plugin_onmessage = async (adapter: string, _core: NapCatCore, _obCtx: NapCatOneBot11Adapter, message: OB11Message, action: ActionMap, instance: OB11PluginAdapter) => { - -}; + const uid = await _core.apis.UserApi.getUidByUinV2(message.sender.user_id.toString()); + + const msgs = (await _core.apis.MsgApi.queryFirstMsgBySender({ + peerUid: message.group_id ? String(message.group_id) : String(uid), + chatType: ChatType.KCHATTYPEGROUP, + }, [uid])).msgList; + console.log('parse message ', message.sender.user_id, msgs.length); + for (const msg of msgs) { + await _obCtx.apis.MsgApi.parseMessageV2(msg) + } +} \ No newline at end of file