fix: send private msg

This commit is contained in:
linyuchen 2023-12-08 16:52:31 +08:00
parent 3d077550cd
commit dc38275660
3 changed files with 31 additions and 12 deletions

View File

@ -4,7 +4,7 @@
"name": "LLOneBot", "name": "LLOneBot",
"slug": "LLOneBot", "slug": "LLOneBot",
"description": "LiteLoaderQQNT的OneBotApi", "description": "LiteLoaderQQNT的OneBotApi",
"version": "1.2.2", "version": "1.2.3",
"thumbnail": "./icon.png", "thumbnail": "./icon.png",
"author": { "author": {
"name": "linyuchen", "name": "linyuchen",

View File

@ -53,6 +53,7 @@ function onLoad(plugin: any) {
let buffer = await blob.arrayBuffer(); let buffer = await blob.arrayBuffer();
fs.writeFileSync(arg.localFilePath, Buffer.from(buffer)); fs.writeFileSync(arg.localFilePath, Buffer.from(buffer));
} }
// todo: 需要识别gif格式
return arg.localFilePath; return arg.localFilePath;
}) })
ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => { ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => {

View File

@ -20,8 +20,25 @@ async function getUserInfo(uid: string): Promise<User> {
return user return user
} }
function getFriend(qq: string) { async function getFriends(){
return friends.find(friend => friend.uid == qq) let _friends = await window.LLAPI.getFriendsList(false)
for (let friend of _friends) {
let existFriend = friends.find(f => f.uin == friend.uin)
if (!existFriend) {
friends.push(friend)
}
}
window.llonebot.updateFriends(friends)
return friends
}
async function getFriend(qq: string) {
let friend = friends.find(friend => friend.uin == qq)
if (!friend) {
await getFriends();
friend = friends.find(friend => friend.uin == qq);
}
return friend;
} }
async function getGroup(qq: string) { async function getGroup(qq: string) {
@ -39,11 +56,11 @@ async function getGroups() {
group.members = []; group.members = [];
let existGroup = groups.find(g => g.uid == group.uid) let existGroup = groups.find(g => g.uid == group.uid)
if (!existGroup) { if (!existGroup) {
console.log("更新群列表", groups) // console.log("更新群列表", groups)
groups.push(group) groups.push(group)
window.llonebot.updateGroups(groups)
} }
} }
window.llonebot.updateGroups(groups)
return groups return groups
} }
@ -115,7 +132,7 @@ async function handleNewMessage(messages: MessageElement[]) {
console.log("收到群消息", onebot_message_data) console.log("收到群消息", onebot_message_data)
} else if (message.peer.chatType == "private") { } else if (message.peer.chatType == "private") {
onebot_message_data["user_id"] = message.peer.uid onebot_message_data["user_id"] = message.peer.uid
let friend = getFriend(message.sender.uid) let friend = await getFriend(message.sender.uid)
onebot_message_data.sender = { onebot_message_data.sender = {
user_id: friend!.uin, user_id: friend!.uin,
nickname: friend!.nickName nickname: friend!.nickName
@ -171,12 +188,12 @@ async function listenSendMessage(postData: PostDataSendMsg) {
} }
} }
if (postData.action == "send_private_msg") { if (postData.action == "send_private_msg") {
let friend = getFriend(postData.params.user_id) let friend = await getFriend(postData.params.user_id)
if (friend) { if (friend) {
peer = { peer = {
chatType: "private", chatType: "private",
name: friend.nickName, name: friend.nickName,
uid: friend.uin uid: friend.uid
} }
} }
} else if (postData.action == "send_group_msg") { } else if (postData.action == "send_group_msg") {
@ -212,6 +229,7 @@ async function listenSendMessage(postData: PostDataSendMsg) {
let uri = new URL(url); let uri = new URL(url);
let ext: string; let ext: string;
if (message.type == "image") { if (message.type == "image") {
// todo: 需要识别gif格式
ext = ".png" ext = ".png"
} }
if (message.type == "voice") { if (message.type == "voice") {
@ -233,7 +251,7 @@ async function listenSendMessage(postData: PostDataSendMsg) {
} }
// 发送完之后要删除下载的文件 // 发送完之后要删除下载的文件
console.log("发送消息", postData) console.log("发送消息", postData)
window.LLAPI.sendMessage(peer, postData.params.message).then(res => console.log("消息发送成功:", res), window.LLAPI.sendMessage(peer, postData.params.message).then(res => console.log("消息发送成功:", peer, postData.params.message),
err => console.log("消息发送失败", postData, err)) err => console.log("消息发送失败", postData, err))
} }
} }
@ -249,8 +267,8 @@ let chatListEle: HTMLCollectionOf<Element>
function onLoad() { function onLoad() {
window.llonebot.listenSendMessage((postData: PostDataSendMsg) => { window.llonebot.listenSendMessage((postData: PostDataSendMsg) => {
listenSendMessage(postData).then() listenSendMessage(postData).then().catch(err => console.log("listenSendMessage err", err))
}); })
window.llonebot.listenRecallMessage((arg: { message_id: string }) => { window.llonebot.listenRecallMessage((arg: { message_id: string }) => {
recallMessage(arg.message_id) recallMessage(arg.message_id)
}) })
@ -287,7 +305,7 @@ function onLoad() {
}) })
console.log("chatListEle", chatListEle) console.log("chatListEle", chatListEle)
} }
getFriends().then();
getGroups().then(() => { getGroups().then(() => {
getGroupsMembers(groups).then(() => { getGroupsMembers(groups).then(() => {
window.LLAPI.on("new-messages", onNewMessages); window.LLAPI.on("new-messages", onNewMessages);