fix: report self

This commit is contained in:
linyuchen 2024-02-06 18:29:03 +08:00
parent e545d8d1cd
commit 9411993d8a
10 changed files with 77 additions and 47 deletions

View File

@ -1,6 +1,6 @@
# LLOneBot API
将NTQQLiteLoaderAPI封装成OneBot11/12标准的API, V12没有完整测试
将NTQQLiteLoaderAPI封装成OneBot11标准的API
*注意:本文档对应的是 LiteLoader 1.0.0及以上版本如果你使用的是旧版本请切换到本项目v1分支查看文档*
@ -51,6 +51,7 @@
- [x] get_group_member_list
- [x] get_group_member_info
- [x] get_friend_list
- [x] get_msg
**自己发送成功的消息也会上报可以用于获取需要撤回消息的id**
@ -90,13 +91,6 @@
</details>
<br/>
<details>
<summary>onebot 12对接不了</summary>
<br/>
onebot 12只写了部分兼容没有完整测试不保证能用慎用
</details>
<br/>
<details>
<summary>QQ变得很卡</summary>
<br/>

View File

@ -11,4 +11,5 @@ export const CHANNEL_SET_SELF_INFO= "llonebot_set_self_info"
export const CHANNEL_DOWNLOAD_FILE= "llonebot_download_file"
export const CHANNEL_DELETE_FILE= "llonebot_delete_file"
export const CHANNEL_GET_RUNNING_STATUS= "llonebot_get_running_status"
export const CHANNEL_FILE2BASE64= "llonebot_file2base64"
export const CHANNEL_FILE2BASE64= "llonebot_file2base64"
export const CHANNEL_GET_HISTORY_MSG= "llonebot_get_history_msg"

View File

@ -2,6 +2,7 @@ import {Group, MessageElement, RawMessage, SelfInfo, User} from "./types";
export let groups: Group[] = []
export let friends: User[] = []
export let msgHistory: Record<string, RawMessage> = {} // msgId: RawMessage
export function getFriend(qq: string): User | undefined {
return friends.find(friend => friend.uin === qq)
@ -23,7 +24,6 @@ export let selfInfo: SelfInfo = {
nickname: ""
}
export let msgHistory: Record<string, RawMessage> = {}
export function getHistoryMsgBySeq(seq: string) {
return Object.values(msgHistory).find(msg => msg.msgSeq === seq)

7
src/global.d.ts vendored
View File

@ -4,13 +4,13 @@ import {
GroupMemberInfo,
MessageElement,
Peer,
PostDataSendMsg, PttElement,
PostDataSendMsg, PttElement, RawMessage,
SelfInfo,
User
} from "./common/types";
import {OB11Return, OB11MessageData} from "./onebot11/types";
import {OB11Return, OB11MessageData, OB11SendMsgReturn} from "./onebot11/types";
declare var LLAPI: {
@ -49,8 +49,9 @@ declare var llonebot: {
downloadFile(arg: {uri: string, fileName: string}):Promise<{errMsg: string, path: string}>;
deleteFile(path: string[]):Promise<void>;
getRunningStatus(): Promise<boolean>;
sendSendMsgResult(sessionId: string, msgResult: OB11Return): void;
sendSendMsgResult(sessionId: string, msgResult: OB11SendMsgReturn): void;
file2base64(path: string): Promise<{err: string, data: string}>;
getHistoryMsg(msgId: string): Promise<RawMessage>;
};
declare global {

View File

@ -14,7 +14,7 @@ import {
CHANNEL_SET_CONFIG,
CHANNEL_START_HTTP_SERVER,
CHANNEL_UPDATE_FRIENDS,
CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE, CHANNEL_GET_RUNNING_STATUS, CHANNEL_FILE2BASE64
CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE, CHANNEL_GET_RUNNING_STATUS, CHANNEL_FILE2BASE64, CHANNEL_GET_HISTORY_MSG
} from "../common/channels";
import {ConfigUtil} from "../common/config";
import {postMsg, startExpress} from "../server/httpserver";
@ -167,12 +167,44 @@ function onLoad() {
return await file2base64(path);
})
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
for (const message of payload.msgList) {
ipcMain.handle(CHANNEL_GET_HISTORY_MSG, (event: any, arg: string): RawMessage | undefined => {
return msgHistory[arg] || null;
})
function postRawMsg(msgList:RawMessage[]) {
const {debug, reportSelfMessage} = getConfigUtil().getConfig();
for (const message of msgList) {
OB11Construct.constructMessage(message).then((msg) => {
if (debug) {
msg.raw = message;
}
if (msg.user_id == selfInfo.user_id && !reportSelfMessage) {
return
}
postMsg(msg);
}).catch(e=>log("constructMessage error: ", e.toString()));
}
}
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
try {
postRawMsg(payload.msgList);
} catch (e) {
log("report message error: ", e.toString())
}
})
registerReceiveHook<{ msgRecord: RawMessage }>(ReceiveCmd.SELF_SEND_MSG, (payload) => {
const {reportSelfMessage} = getConfigUtil().getConfig()
if (!reportSelfMessage) {
return
}
log("reportSelfMessage", payload)
try {
postRawMsg([payload.msgRecord]);
} catch (e) {
log("report self message error: ", e.toString())
}
})
}

View File

@ -1,12 +1,13 @@
import {BrowserWindow} from 'electron';
import {log} from "../common/utils";
import {getConfigUtil, log} from "../common/utils";
import {NTQQApiClass} from "./ntcall";
import {RawMessage} from "../common/types";
import {msgHistory} from "../common/data";
export enum ReceiveCmd {
UPDATE_MSG = "nodeIKernelMsgListener/onMsgInfoListUpdate",
NEW_MSG = "nodeIKernelMsgListener/onRecvMsg"
NEW_MSG = "nodeIKernelMsgListener/onRecvMsg",
SELF_SEND_MSG = "nodeIKernelMsgListener/onAddSendMsg"
}
interface NTQQApiReturnData extends Array<any> {
@ -66,4 +67,5 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload
log("收到新消息push到历史记录", message)
msgHistory[message.msgId] = message;
}
})
})

View File

@ -6,7 +6,7 @@ import {file2base64, getConfigUtil} from "../common/utils";
export class OB11Construct {
static async constructMessage(msg: RawMessage): Promise<OB11Message> {
const {debug, enableBase64} = getConfigUtil().getConfig()
const {enableBase64} = getConfigUtil().getConfig()
const message_type = msg.chatType == ChatType.group ? "group" : "private";
const resMsg: OB11Message = {
self_id: selfInfo.user_id,
@ -42,11 +42,6 @@ export class OB11Construct {
resMsg.sub_type = "group"
}
if (debug) {
resMsg.raw = msg
}
for (let element of msg.elements) {
let message_data: any = {
data: {},

View File

@ -1,6 +1,6 @@
// Electron 主进程 与 渲染进程 交互的桥梁
import {Config, Group, PostDataSendMsg, SelfInfo, User} from "./common/types";
import {Config, Group, PostDataSendMsg, RawMessage, SelfInfo, User} from "./common/types";
import {
CHANNEL_DOWNLOAD_FILE,
CHANNEL_GET_CONFIG,
@ -14,7 +14,7 @@ import {
CHANNEL_UPDATE_FRIENDS,
CHANNEL_UPDATE_GROUPS,
CHANNEL_DELETE_FILE,
CHANNEL_GET_RUNNING_STATUS, CHANNEL_FILE2BASE64
CHANNEL_GET_RUNNING_STATUS, CHANNEL_FILE2BASE64, CHANNEL_GET_HISTORY_MSG
} from "./common/channels";
@ -74,8 +74,8 @@ contextBridge.exposeInMainWorld("llonebot", {
getRunningStatus: () => {
return ipcRenderer.invoke(CHANNEL_GET_RUNNING_STATUS);
},
file2base64: (localFilePath: string) => {
return ipcRenderer.invoke(CHANNEL_FILE2BASE64, localFilePath);
getHistoryMsg: async (msgId: string):Promise<RawMessage> => {
return await ipcRenderer.invoke(CHANNEL_GET_HISTORY_MSG, msgId)
}
// startExpress,
});

View File

@ -1,21 +1,14 @@
/// <reference path="./global.d.ts" />
import {
AtType,
ChatType,
Group,
MessageElement, Peer,
PostDataSendMsg,
User
} from "./common/types";
import {AtType, ChatType, Group, MessageElement, Peer, PostDataSendMsg, RawMessage, User} from "./common/types";
import {OB11Return, OB11SendMsgReturn, OB11MessageDataType} from "./onebot11/types";
import {OB11SendMsgReturn} from "./onebot11/types";
import {ipcRenderer} from "electron";
import {CHANNEL_GET_HISTORY_MSG} from "./common/channels";
let self_qq: string = ""
let groups: Group[] = []
let friends: User[] = []
let msgHistory: MessageElement[] = []
let uid_maps: Record<string, User> = {} // 一串加密的字符串 -> qq号
function getStrangerByUin(uin: string) {
@ -26,6 +19,10 @@ function getStrangerByUin(uin: string) {
}
}
async function getHistoryMsg(msgId: string) {
return await window.llonebot.getHistoryMsg(msgId);
}
async function getUserInfo(uid: string): Promise<User> {
let user = uid_maps[uid]
if (!user) {
@ -236,9 +233,11 @@ async function listenSendMessage(postData: PostDataSendMsg) {
message.file = localFilePath
} else if (message.type == "reply") {
let msgId = message.data?.id || message.msgId
let replyMessage = msgHistory.find(msg => msg.raw.msgId == msgId)
message.msgId = msgId
message.msgSeq = replyMessage?.raw.msgSeq || ""
const rawMsg: RawMessage = await getHistoryMsg(msgId)
if (rawMsg){
message.msgId = msgId
message.msgSeq = rawMsg.msgSeq
}
}
}
console.log("发送消息", postData)
@ -270,8 +269,15 @@ async function listenSendMessage(postData: PostDataSendMsg) {
}
function recallMessage(msgId: string) {
let msg = msgHistory.find(msg => msg.raw.msgId == msgId)
window.LLAPI.recallMessage(msg.peer, [msgId]).then()
getHistoryMsg(msgId).then((msg: RawMessage)=>{
const peer: Peer ={
chatType: msg.chatType,
name: "",
uid: msg.peerUin
}
window.LLAPI.recallMessage(peer, [msgId]).then()
})
}
let chatListEle: HTMLCollectionOf<Element>

View File

@ -153,7 +153,7 @@ function handlePost(jsonData: any, handleSendResult: (data: OB11Return<any>) =>
}
})
} else if (jsonData.action == "delete_msg") {
sendIPCRecallQQMsg(String(jsonData.message_id))
sendIPCRecallQQMsg(jsonData.message_id)
}
return resData
}
@ -287,5 +287,4 @@ export function postMsg(msg: OB11Message) {
log(`新消息事件上报失败: ${host} ` + err + JSON.stringify(msg));
});
}
}