mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
ver: 2.1.0
This commit is contained in:
@@ -3,6 +3,12 @@ export enum AtType {
|
||||
atUser = 2
|
||||
}
|
||||
|
||||
export enum ChatType {
|
||||
friend = 1,
|
||||
group = 2,
|
||||
temp = 100
|
||||
}
|
||||
|
||||
export type GroupMemberInfo = {
|
||||
avatarPath: string;
|
||||
cardName: string;
|
||||
@@ -44,7 +50,7 @@ export type Group = {
|
||||
}
|
||||
|
||||
export type Peer = {
|
||||
chatType: "private" | "group" | "friend"
|
||||
chatType: ChatType
|
||||
name: string
|
||||
uid: string // qq号
|
||||
}
|
||||
@@ -52,8 +58,10 @@ export type Peer = {
|
||||
export type MessageElement = {
|
||||
raw: {
|
||||
msgId: string,
|
||||
msgTime: string,
|
||||
msgSeq: string,
|
||||
senderUin: string; // 发送者QQ号
|
||||
chatType: ChatType,
|
||||
elements: {
|
||||
replyElement: {
|
||||
senderUid: string, // 原消息发送者QQ号
|
||||
@@ -139,7 +147,7 @@ export type SendMessage = {
|
||||
}
|
||||
|
||||
export type PostDataAction = "send_private_msg" | "send_group_msg" | "get_group_list"
|
||||
| "get_friend_list" | "delete_msg" | "get_login_info" | "get_group_member_list" | "get_group_member_info"
|
||||
| "get_friend_list" | "delete_msg" | "get_login_info" | "get_group_member_list" | "get_group_member_info"
|
||||
|
||||
export type PostDataSendMsg = {
|
||||
action: PostDataAction
|
||||
@@ -152,9 +160,17 @@ export type PostDataSendMsg = {
|
||||
user_id: string,
|
||||
group_id: string,
|
||||
message: SendMessage[];
|
||||
ipc_uuid?: string
|
||||
}
|
||||
|
||||
export type Config = {
|
||||
port: number,
|
||||
hosts: string[],
|
||||
}
|
||||
|
||||
export type SendMsgResult = {
|
||||
status: number,
|
||||
retcode: number,
|
||||
data: any,
|
||||
message: string,
|
||||
}
|
5
src/global.d.ts
vendored
5
src/global.d.ts
vendored
@@ -6,7 +6,7 @@ import {
|
||||
Peer,
|
||||
PostDataSendMsg,
|
||||
SelfInfo,
|
||||
SendMessage,
|
||||
SendMessage, SendMsgResult,
|
||||
User
|
||||
} from "./common/types";
|
||||
|
||||
@@ -43,9 +43,10 @@ declare var llonebot: {
|
||||
setConfig(config: Config):void;
|
||||
getConfig():Promise<Config>;
|
||||
setSelfInfo(selfInfo: SelfInfo):void;
|
||||
downloadFile(arg: {uri: string, fileName: string}):Promise<string>;
|
||||
downloadFile(arg: {uri: string, fileName: string}):Promise<{errMsg: string, path: string}>;
|
||||
deleteFile(path: string[]):Promise<void>;
|
||||
getRunningStatus(): Promise<boolean>;
|
||||
sendSendMsgResult(sessionId: string, msgResult: SendMsgResult): void;
|
||||
};
|
||||
|
||||
declare global {
|
||||
|
@@ -3,7 +3,7 @@ import {log} from "./utils";
|
||||
const express = require("express");
|
||||
const bodyParser = require('body-parser');
|
||||
import {sendIPCRecallQQMsg, sendIPCSendQQMsg} from "./IPCSend";
|
||||
import {OnebotGroupMemberRole, PostDataAction, PostDataSendMsg, SendMessage} from "../common/types";
|
||||
import {OnebotGroupMemberRole, PostDataAction, PostDataSendMsg, SendMessage, SendMsgResult} from "../common/types";
|
||||
import {friends, groups, selfInfo} from "./data";
|
||||
|
||||
// @SiberianHusky 2021-08-15
|
||||
@@ -19,7 +19,7 @@ function checkSendMessage(sendMsgList: SendMessage[]) {
|
||||
let data = msg["data"];
|
||||
if (type === "text" && !data["text"]) {
|
||||
return 400;
|
||||
} else if (["image", "voice"].includes(type)) {
|
||||
} else if (["image", "voice", "record"].includes(type)) {
|
||||
if (!data["file"]) {
|
||||
return 400;
|
||||
}
|
||||
@@ -46,7 +46,7 @@ function checkSendMessage(sendMsgList: SendMessage[]) {
|
||||
}
|
||||
// ==end==
|
||||
|
||||
function handlePost(jsonData: any) {
|
||||
function handlePost(jsonData: any, handleSendResult: (data: SendMsgResult)=>void) {
|
||||
log("API receive post:" + JSON.stringify(jsonData))
|
||||
if (!jsonData.params) {
|
||||
jsonData.params = JSON.parse(JSON.stringify(jsonData));
|
||||
@@ -58,6 +58,7 @@ function handlePost(jsonData: any) {
|
||||
data: {},
|
||||
message: ''
|
||||
}
|
||||
|
||||
if (jsonData.action == "get_login_info") {
|
||||
resData["data"] = selfInfo
|
||||
} else if (jsonData.action == "send_private_msg" || jsonData.action == "send_group_msg") {
|
||||
@@ -71,7 +72,8 @@ function handlePost(jsonData: any) {
|
||||
if (resData.status == 200) {
|
||||
resData.message = "发送成功";
|
||||
resData.data = jsonData.message;
|
||||
sendIPCSendQQMsg(jsonData);
|
||||
sendIPCSendQQMsg(jsonData, handleSendResult);
|
||||
return;
|
||||
} else {
|
||||
resData.message = "发送失败, 请检查消息格式";
|
||||
resData.data = jsonData.message;
|
||||
@@ -153,8 +155,10 @@ export function startExpress(port: number) {
|
||||
app.post('/' + action, (req: any, res: any) => {
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
jsonData.action = action
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
let resData = handlePost(jsonData, (data:SendMsgResult)=>{res.send(data)})
|
||||
if (resData){
|
||||
res.send(resData)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -173,8 +177,10 @@ export function startExpress(port: number) {
|
||||
// 处理POST请求的路由
|
||||
app.post('/', (req: any, res: any) => {
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
let resData = handlePost(jsonData, (data:SendMsgResult)=>{res.send(data)})
|
||||
if (resData){
|
||||
res.send(resData)
|
||||
}
|
||||
});
|
||||
app.post('/send_msg', (req: any, res: any) => {
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
@@ -189,11 +195,13 @@ export function startExpress(port: number) {
|
||||
jsonData.action = "send_private_msg"
|
||||
}
|
||||
}
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
let resData = handlePost(jsonData, (data:SendMsgResult)=>{res.send(data)})
|
||||
if (resData){
|
||||
res.send(resData)
|
||||
}
|
||||
})
|
||||
|
||||
app.listen(port, "0.0.0.0", () => {
|
||||
console.log(`服务器已启动,监听端口 ${port}`);
|
||||
console.log(`llonebot started 0.0.0.0:${port}`);
|
||||
});
|
||||
}
|
@@ -1,6 +1,8 @@
|
||||
import {ipcMain, webContents} from 'electron';
|
||||
import {PostDataSendMsg} from "../common/types";
|
||||
import {PostDataSendMsg, SendMsgResult} from "../common/types";
|
||||
import {CHANNEL_RECALL_MSG, CHANNEL_SEND_MSG} from "../common/IPCChannel";
|
||||
import {v4 as uuid4} from "uuid";
|
||||
import {log} from "./utils";
|
||||
|
||||
function sendIPCMsg(channel: string, data: any) {
|
||||
let contents = webContents.getAllWebContents();
|
||||
@@ -14,7 +16,17 @@ function sendIPCMsg(channel: string, data: any) {
|
||||
}
|
||||
|
||||
|
||||
export function sendIPCSendQQMsg(postData: PostDataSendMsg) {
|
||||
export function sendIPCSendQQMsg(postData: PostDataSendMsg, handleSendResult: (data: SendMsgResult) => void) {
|
||||
const onceSessionId = "llonebot_send_msg_" + uuid4();
|
||||
postData.ipc_uuid = onceSessionId;
|
||||
ipcMain.once(onceSessionId, (event: any, sendResult: SendMsgResult) => {
|
||||
// log("llonebot send msg ipcMain.once:" + JSON.stringify(sendResult));
|
||||
try {
|
||||
handleSendResult(sendResult)
|
||||
} catch (e) {
|
||||
log("llonebot send msg ipcMain.once error:" + JSON.stringify(e))
|
||||
}
|
||||
})
|
||||
sendIPCMsg(CHANNEL_SEND_MSG, postData);
|
||||
}
|
||||
|
||||
|
@@ -29,6 +29,7 @@ let running = false;
|
||||
// 加载插件时触发
|
||||
function onLoad() {
|
||||
log("main onLoaded");
|
||||
|
||||
// const config_dir = browserWindow.LiteLoader.plugins["LLOneBot"].path.data;
|
||||
function getConfigUtil() {
|
||||
const configFilePath = path.join(CONFIG_DIR, `config_${selfInfo.user_id}.json`)
|
||||
@@ -41,28 +42,65 @@ function onLoad() {
|
||||
ipcMain.handle(CHANNEL_GET_CONFIG, (event: any, arg: any) => {
|
||||
return getConfigUtil().getConfig()
|
||||
})
|
||||
ipcMain.handle(CHANNEL_DOWNLOAD_FILE, async (event: any, arg: {uri: string, fileName: string}) => {
|
||||
ipcMain.handle(CHANNEL_DOWNLOAD_FILE, async (event: any, arg: { uri: string, fileName: string }): Promise<{
|
||||
success: boolean,
|
||||
errMsg: string,
|
||||
path: string
|
||||
}> => {
|
||||
let filePath = path.join(CONFIG_DIR, arg.fileName)
|
||||
let url = new URL(arg.uri);
|
||||
if (url.protocol == "base64:"){
|
||||
if (url.protocol == "base64:") {
|
||||
// base64转成文件
|
||||
let base64Data = arg.uri.split("base64://")[1]
|
||||
const buffer = Buffer.from(base64Data, 'base64');
|
||||
try {
|
||||
const buffer = Buffer.from(base64Data, 'base64');
|
||||
|
||||
fs.writeFileSync(filePath, buffer);
|
||||
}
|
||||
else if (url.protocol == "http:" || url.protocol == "https:") {
|
||||
fs.writeFileSync(filePath, buffer);
|
||||
} catch (e: any) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `base64文件下载失败,` + e.toString(),
|
||||
path: ""
|
||||
}
|
||||
}
|
||||
} else if (url.protocol == "http:" || url.protocol == "https:") {
|
||||
// 下载文件
|
||||
let res = await fetch(url)
|
||||
if (!res.ok) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `${url}下载失败,` + res.statusText,
|
||||
path: ""
|
||||
}
|
||||
}
|
||||
let blob = await res.blob();
|
||||
let buffer = await blob.arrayBuffer();
|
||||
fs.writeFileSync(filePath, Buffer.from(buffer));
|
||||
try {
|
||||
fs.writeFileSync(filePath, Buffer.from(buffer));
|
||||
} catch (e: any) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `${url}下载失败,` + e.toString(),
|
||||
path: ""
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `不支持的file协议,` + url.protocol,
|
||||
path: ""
|
||||
}
|
||||
}
|
||||
if (isGIF(filePath)) {
|
||||
fs.renameSync(filePath, filePath + ".gif");
|
||||
filePath += ".gif";
|
||||
}
|
||||
return filePath;
|
||||
return {
|
||||
success: true,
|
||||
errMsg: "",
|
||||
path: filePath
|
||||
};
|
||||
})
|
||||
ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => {
|
||||
getConfigUtil().setConfig(arg)
|
||||
@@ -103,7 +141,7 @@ function onLoad() {
|
||||
})
|
||||
|
||||
ipcMain.on(CHANNEL_POST_ONEBOT_DATA, (event: any, arg: any) => {
|
||||
for(const host of getConfigUtil().getConfig().hosts) {
|
||||
for (const host of getConfigUtil().getConfig().hosts) {
|
||||
try {
|
||||
fetch(host, {
|
||||
method: "POST",
|
||||
@@ -113,9 +151,9 @@ function onLoad() {
|
||||
},
|
||||
body: JSON.stringify(arg)
|
||||
}).then((res: any) => {
|
||||
log("新消息事件上传成功:" + JSON.stringify(arg));
|
||||
log(`新消息事件上传成功: ${host} ` + JSON.stringify(arg));
|
||||
}, (err: any) => {
|
||||
log("新消息事件上传失败:" + err + JSON.stringify(arg));
|
||||
log(`新消息事件上传失败: ${host} ` + err + JSON.stringify(arg));
|
||||
});
|
||||
} catch (e: any) {
|
||||
log(e.toString())
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import * as path from "path";
|
||||
import {json} from "express";
|
||||
import {selfInfo} from "./data";
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
@@ -11,7 +12,8 @@ export function log(msg: any) {
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
const currentDate = `${year}-${month}-${day}`;
|
||||
fs.appendFile(path.join(CONFIG_DIR , `llonebot-${currentDate}.log`), currentDateTime + ":" + JSON.stringify(msg) + "\n", (err: any) => {
|
||||
const userInfo = selfInfo.user_id ? `${selfInfo.nickname}(${selfInfo.user_id})` : ""
|
||||
fs.appendFile(path.join(CONFIG_DIR , `llonebot-${currentDate}.log`), currentDateTime + ` ${userInfo}:` + JSON.stringify(msg) + "\n", (err: any) => {
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
// Electron 主进程 与 渲染进程 交互的桥梁
|
||||
|
||||
import {Config, Group, PostDataSendMsg, SelfInfo, User} from "./common/types";
|
||||
import {Config, Group, PostDataSendMsg, SelfInfo, SendMsgResult, User} from "./common/types";
|
||||
import {
|
||||
CHANNEL_DOWNLOAD_FILE,
|
||||
CHANNEL_GET_CONFIG,
|
||||
@@ -33,6 +33,9 @@ contextBridge.exposeInMainWorld("llonebot", {
|
||||
updateFriends: (friends: User[]) => {
|
||||
ipcRenderer.send(CHANNEL_UPDATE_FRIENDS, friends);
|
||||
},
|
||||
sendSendMsgResult: (sessionId: string, msgResult: SendMsgResult)=>{
|
||||
ipcRenderer.send(sessionId, msgResult);
|
||||
},
|
||||
listenSendMessage: (handle: (jsonData: PostDataSendMsg) => void) => {
|
||||
ipcRenderer.send(CHANNEL_LOG, "发送消息API已注册");
|
||||
ipcRenderer.on(CHANNEL_SEND_MSG, (event: any, args: PostDataSendMsg) => {
|
||||
|
291
src/renderer.ts
291
src/renderer.ts
@@ -2,15 +2,31 @@
|
||||
|
||||
// import express from "express";
|
||||
// const { ipcRenderer } = require('electron');
|
||||
import {AtType, Group, MessageElement, OnebotGroupMemberRole, Peer, PostDataSendMsg, User} from "./common/types";
|
||||
import * as stream from "stream";
|
||||
import {raw} from "express";
|
||||
import {
|
||||
AtType,
|
||||
ChatType,
|
||||
Group,
|
||||
MessageElement,
|
||||
OnebotGroupMemberRole,
|
||||
Peer,
|
||||
PostDataSendMsg, SendMsgResult,
|
||||
User
|
||||
} from "./common/types";
|
||||
|
||||
let self_qq: string = ""
|
||||
let groups: Group[] = []
|
||||
let friends: User[] = []
|
||||
let msgHistory: MessageElement[] = []
|
||||
let uid_maps: Record<string, User> = {} // 一串加密的字符串 -> qq号
|
||||
|
||||
function getStrangerByUin(uin: string) {
|
||||
for (const key in uid_maps) {
|
||||
if (uid_maps[key].uin === uin) {
|
||||
return uid_maps[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function getUserInfo(uid: string): Promise<User> {
|
||||
let user = uid_maps[uid]
|
||||
if (!user) {
|
||||
@@ -80,6 +96,11 @@ async function getGroupMembers(group_qq: string, forced: boolean = false) {
|
||||
if (!group!.members!.find(m => m.uid == member.uid)) {
|
||||
group!.members!.push(member)
|
||||
}
|
||||
uid_maps[member.uid] = {
|
||||
uin: member.uin,
|
||||
uid: member.uid,
|
||||
nickName: member.nick
|
||||
};
|
||||
}
|
||||
window.llonebot.updateGroups(groups)
|
||||
console.log(`更新群${group.name}成员列表`, group)
|
||||
@@ -100,9 +121,8 @@ async function getGroupMember(group_qq: string, member_uid: string) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleNewMessage(messages: MessageElement[]) {
|
||||
// console.log("llonebot 收到消息:", messages);
|
||||
console.log("llonebot 收到消息:", messages);
|
||||
for (let message of messages) {
|
||||
let onebot_message_data: any = {
|
||||
self: {
|
||||
@@ -110,7 +130,7 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
user_id: self_qq
|
||||
},
|
||||
self_id: self_qq,
|
||||
time: 0,
|
||||
time: parseInt(message.raw.msgTime || "0"),
|
||||
type: "message",
|
||||
post_type: "message",
|
||||
message_type: message.peer.chatType,
|
||||
@@ -121,9 +141,10 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
raw_message: "",
|
||||
font: 14
|
||||
}
|
||||
if (message.peer.chatType == "group") {
|
||||
if (message.raw.chatType == ChatType.group) {
|
||||
let group_id = message.peer.uid
|
||||
let group = (await getGroup(group_id))!
|
||||
onebot_message_data.message_type = onebot_message_data.sub_type = "group"
|
||||
onebot_message_data["group_id"] = message.peer.uid
|
||||
let groupMember = await getGroupMember(group_id, message.sender.uid)
|
||||
onebot_message_data["user_id"] = groupMember!.uin
|
||||
@@ -134,13 +155,25 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
role: OnebotGroupMemberRole[groupMember!.role]
|
||||
}
|
||||
// console.log("收到群消息", onebot_message_data)
|
||||
} else if (message.peer.chatType == "private" || message.peer.chatType == "friend") {
|
||||
} else if (message.raw.chatType == ChatType.friend) {
|
||||
onebot_message_data["user_id"] = message.raw.senderUin;
|
||||
let friend = await getFriend(message.raw.senderUin)
|
||||
onebot_message_data.message_type = onebot_message_data.sub_type = "friend"
|
||||
let friend = await getFriend(message.raw.senderUin);
|
||||
onebot_message_data.sender = {
|
||||
user_id: friend!.uin,
|
||||
nickname: friend!.nickName
|
||||
}
|
||||
} else if (message.raw.chatType == ChatType.temp) {
|
||||
let senderQQ = message.raw.senderUin;
|
||||
let senderUid = message.sender.uid;
|
||||
let sender = await getUserInfo(senderUid);
|
||||
onebot_message_data["user_id"] = senderQQ;
|
||||
onebot_message_data.message_type = "friend"
|
||||
onebot_message_data.sub_type = "group";
|
||||
onebot_message_data.sender = {
|
||||
user_id: senderQQ,
|
||||
nickname: sender.nickName
|
||||
}
|
||||
}
|
||||
for (let element of message.raw.elements) {
|
||||
let message_data: any = {
|
||||
@@ -168,6 +201,7 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
if (!element.picElement.sourcePath.startsWith("/")) {
|
||||
startS += "/"
|
||||
}
|
||||
// todo: 转成base64
|
||||
message_data["data"]["file"] = startS + element.picElement.sourcePath
|
||||
} else if (element.replyElement) {
|
||||
message_data["type"] = "reply"
|
||||
@@ -175,6 +209,9 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
}
|
||||
onebot_message_data.message.push(message_data)
|
||||
}
|
||||
if (msgHistory.length > 10000) {
|
||||
msgHistory.splice(0, 100)
|
||||
}
|
||||
msgHistory.push(message)
|
||||
console.log("发送上传消息给ipc main", onebot_message_data)
|
||||
window.llonebot.postData(onebot_message_data);
|
||||
@@ -183,6 +220,12 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
|
||||
async function listenSendMessage(postData: PostDataSendMsg) {
|
||||
console.log("收到发送消息请求", postData);
|
||||
let sendMsgResult: SendMsgResult = {
|
||||
retcode: 0,
|
||||
status: 0,
|
||||
data: {},
|
||||
message: "发送成功"
|
||||
}
|
||||
if (postData.action == "send_private_msg" || postData.action == "send_group_msg") {
|
||||
let peer: Peer | null = null;
|
||||
if (!postData.params) {
|
||||
@@ -195,22 +238,41 @@ async function listenSendMessage(postData: PostDataSendMsg) {
|
||||
if (postData.action == "send_private_msg") {
|
||||
let friend = await getFriend(postData.params.user_id)
|
||||
if (friend) {
|
||||
console.log("好友消息", postData)
|
||||
peer = {
|
||||
chatType: "private",
|
||||
chatType: ChatType.friend,
|
||||
name: friend.nickName,
|
||||
uid: friend.uid
|
||||
}
|
||||
} else {
|
||||
// 临时消息
|
||||
console.log("发送临时消息", postData)
|
||||
let receiver = getStrangerByUin(postData.params.user_id);
|
||||
if (receiver) {
|
||||
peer = {
|
||||
chatType: ChatType.temp,
|
||||
name: receiver.nickName,
|
||||
uid: receiver.uid
|
||||
}
|
||||
} else {
|
||||
sendMsgResult.status = -1;
|
||||
sendMsgResult.retcode = -1;
|
||||
sendMsgResult.message = `发送失败,未找到对象${postData.params.user_id},检查他是否为好友或是群友`;
|
||||
}
|
||||
}
|
||||
} else if (postData.action == "send_group_msg") {
|
||||
let group = await getGroup(postData.params.group_id)
|
||||
if (group) {
|
||||
peer = {
|
||||
chatType: "group",
|
||||
chatType: ChatType.group,
|
||||
name: group.name,
|
||||
uid: group.uid
|
||||
}
|
||||
|
||||
} else {
|
||||
sendMsgResult.status = -1;
|
||||
sendMsgResult.retcode = -1;
|
||||
sendMsgResult.message = `发送失败,未找到群${postData.params.group_id}`;
|
||||
console.log("未找到群, 发送群消息失败", postData)
|
||||
}
|
||||
}
|
||||
@@ -245,7 +307,20 @@ async function listenSendMessage(postData: PostDataSendMsg) {
|
||||
if (uri.protocol == "file:") {
|
||||
localFilePath = url.split("file://")[1]
|
||||
} else {
|
||||
localFilePath = await window.llonebot.downloadFile({uri: url, fileName: localFilePath})
|
||||
const {errMsg, path} = await window.llonebot.downloadFile({
|
||||
uri: url,
|
||||
fileName: `${Date.now()}${ext}`
|
||||
})
|
||||
console.log("下载文件结果", errMsg, path)
|
||||
if (errMsg) {
|
||||
console.log("下载文件失败", errMsg);
|
||||
sendMsgResult.status = -1;
|
||||
sendMsgResult.retcode = -1;
|
||||
sendMsgResult.message = `发送失败,下载文件失败,${errMsg}`;
|
||||
break;
|
||||
} else {
|
||||
localFilePath = path;
|
||||
}
|
||||
}
|
||||
message.file = localFilePath
|
||||
sendFiles.push(localFilePath);
|
||||
@@ -257,13 +332,27 @@ async function listenSendMessage(postData: PostDataSendMsg) {
|
||||
}
|
||||
}
|
||||
console.log("发送消息", postData)
|
||||
if (sendMsgResult.status !== 0) {
|
||||
window.llonebot.sendSendMsgResult(postData.ipc_uuid, sendMsgResult)
|
||||
return;
|
||||
}
|
||||
window.LLAPI.sendMessage(peer, postData.params.message).then(res => {
|
||||
console.log("消息发送成功:", peer, postData.params.message)
|
||||
if (sendFiles.length) {
|
||||
window.llonebot.deleteFile(sendFiles);
|
||||
}
|
||||
window.llonebot.sendSendMsgResult(postData.ipc_uuid, sendMsgResult)
|
||||
},
|
||||
err => console.log("消息发送失败", postData, err))
|
||||
err => {
|
||||
sendMsgResult.status = -1;
|
||||
sendMsgResult.retcode = -1;
|
||||
sendMsgResult.message = `发送失败,${err}`;
|
||||
window.llonebot.sendSendMsgResult(postData.ipc_uuid, sendMsgResult)
|
||||
console.log("消息发送失败", postData, err)
|
||||
})
|
||||
} else {
|
||||
console.log(sendMsgResult, postData);
|
||||
window.llonebot.sendSendMsgResult(postData.ipc_uuid, sendMsgResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -308,7 +397,7 @@ function onNewMessages(messages: MessageElement[]) {
|
||||
// console.log("chatListEle", chatListEle)
|
||||
}
|
||||
|
||||
async function initAccountInfo(){
|
||||
async function initAccountInfo() {
|
||||
let accountInfo = await window.LLAPI.getAccountInfo();
|
||||
window.llonebot.log("getAccountInfo " + JSON.stringify(accountInfo));
|
||||
if (!accountInfo.uid) {
|
||||
@@ -325,21 +414,23 @@ async function initAccountInfo(){
|
||||
|
||||
function onLoad() {
|
||||
window.llonebot.log("llonebot render onLoad");
|
||||
window.llonebot.getRunningStatus().then(running=>{
|
||||
window.llonebot.getRunningStatus().then(running => {
|
||||
if (running) {
|
||||
return;
|
||||
}
|
||||
initAccountInfo().then(
|
||||
(initSuccess)=>{
|
||||
(initSuccess) => {
|
||||
if (!initSuccess) {
|
||||
return;
|
||||
}
|
||||
if (friends.length == 0) {
|
||||
getFriends().then(()=>{});
|
||||
getFriends().then(() => {
|
||||
});
|
||||
}
|
||||
if (groups.length == 0) {
|
||||
getGroups().then(()=>{
|
||||
getGroupsMembers(groups).then(()=>{});
|
||||
getGroups().then(() => {
|
||||
getGroupsMembers(groups).then(() => {
|
||||
});
|
||||
});
|
||||
}
|
||||
window.LLAPI.on("new-messages", onNewMessages);
|
||||
@@ -354,93 +445,93 @@ function onLoad() {
|
||||
recallMessage(arg.message_id)
|
||||
})
|
||||
window.llonebot.log("llonebot loaded");
|
||||
// window.LLAPI.add_qmenu((qContextMenu: Node) => {
|
||||
// let btn = document.createElement("a")
|
||||
// btn.className = "q-context-menu-item q-context-menu-item--normal vue-component"
|
||||
// btn.setAttribute("aria-disabled", "false")
|
||||
// btn.setAttribute("role", "menuitem")
|
||||
// btn.setAttribute("tabindex", "-1")
|
||||
// btn.onclick = () => {
|
||||
// // window.LLAPI.getPeer().then(peer => {
|
||||
// // // console.log("current peer", peer)
|
||||
// // if (peer && peer.chatType == "group") {
|
||||
// // getGroupMembers(peer.uid, true).then(()=> {
|
||||
// // console.log("获取群成员列表成功", groups);
|
||||
// // alert("获取群成员列表成功")
|
||||
// // })
|
||||
// // }
|
||||
// // })
|
||||
// async function func() {
|
||||
// for (const group of groups) {
|
||||
// await getGroupMembers(group.uid, true)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func().then(() => {
|
||||
// console.log("获取群成员列表结果", groups);
|
||||
// // 找到members数量为空的群
|
||||
// groups.map(group => {
|
||||
// if (group.members.length == 0) {
|
||||
// console.log(`${group.name}群成员为空`)
|
||||
// }
|
||||
// })
|
||||
// window.llonebot.updateGroups(groups)
|
||||
// })
|
||||
// }
|
||||
// btn.innerText = "获取群成员列表"
|
||||
// console.log(qContextMenu)
|
||||
// // qContextMenu.appendChild(btn)
|
||||
// })
|
||||
//
|
||||
// window.LLAPI.on("context-msg-menu", (event, target, msgIds) => {
|
||||
// console.log("msg menu", event, target, msgIds);
|
||||
// })
|
||||
//
|
||||
// // console.log("getAccountInfo", LLAPI.getAccountInfo());
|
||||
// function getChatListEle() {
|
||||
// chatListEle = document.getElementsByClassName("viewport-list__inner")
|
||||
// console.log("chatListEle", chatListEle)
|
||||
// if (chatListEle.length == 0) {
|
||||
// setTimeout(getChatListEle, 500)
|
||||
// } else {
|
||||
// try {
|
||||
// // 选择要观察的目标节点
|
||||
// const targetNode = chatListEle[0];
|
||||
//
|
||||
// // 创建一个观察器实例并传入回调函数
|
||||
// const observer = new MutationObserver(function (mutations) {
|
||||
// mutations.forEach(function (mutation) {
|
||||
// // console.log("chat list changed", mutation.type); // 输出 mutation 的类型
|
||||
// // 获得当前聊天窗口
|
||||
// window.LLAPI.getPeer().then(peer => {
|
||||
// // console.log("current peer", peer)
|
||||
// if (peer && peer.chatType == "group") {
|
||||
// getGroupMembers(peer.uid, false).then()
|
||||
// }
|
||||
// })
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// // 配置观察选项
|
||||
// const config = {attributes: true, childList: true, subtree: true};
|
||||
//
|
||||
// // 传入目标节点和观察选项
|
||||
// observer.observe(targetNode, config);
|
||||
//
|
||||
// } catch (e) {
|
||||
// window.llonebot.log(e)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // getChatListEle();
|
||||
// window.LLAPI.add_qmenu((qContextMenu: Node) => {
|
||||
// let btn = document.createElement("a")
|
||||
// btn.className = "q-context-menu-item q-context-menu-item--normal vue-component"
|
||||
// btn.setAttribute("aria-disabled", "false")
|
||||
// btn.setAttribute("role", "menuitem")
|
||||
// btn.setAttribute("tabindex", "-1")
|
||||
// btn.onclick = () => {
|
||||
// // window.LLAPI.getPeer().then(peer => {
|
||||
// // // console.log("current peer", peer)
|
||||
// // if (peer && peer.chatType == "group") {
|
||||
// // getGroupMembers(peer.uid, true).then(()=> {
|
||||
// // console.log("获取群成员列表成功", groups);
|
||||
// // alert("获取群成员列表成功")
|
||||
// // })
|
||||
// // }
|
||||
// // })
|
||||
// async function func() {
|
||||
// for (const group of groups) {
|
||||
// await getGroupMembers(group.uid, true)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func().then(() => {
|
||||
// console.log("获取群成员列表结果", groups);
|
||||
// // 找到members数量为空的群
|
||||
// groups.map(group => {
|
||||
// if (group.members.length == 0) {
|
||||
// console.log(`${group.name}群成员为空`)
|
||||
// }
|
||||
// })
|
||||
// window.llonebot.updateGroups(groups)
|
||||
// })
|
||||
// }
|
||||
// btn.innerText = "获取群成员列表"
|
||||
// console.log(qContextMenu)
|
||||
// // qContextMenu.appendChild(btn)
|
||||
// })
|
||||
//
|
||||
// window.LLAPI.on("context-msg-menu", (event, target, msgIds) => {
|
||||
// console.log("msg menu", event, target, msgIds);
|
||||
// })
|
||||
//
|
||||
// // console.log("getAccountInfo", LLAPI.getAccountInfo());
|
||||
// function getChatListEle() {
|
||||
// chatListEle = document.getElementsByClassName("viewport-list__inner")
|
||||
// console.log("chatListEle", chatListEle)
|
||||
// if (chatListEle.length == 0) {
|
||||
// setTimeout(getChatListEle, 500)
|
||||
// } else {
|
||||
// try {
|
||||
// // 选择要观察的目标节点
|
||||
// const targetNode = chatListEle[0];
|
||||
//
|
||||
// // 创建一个观察器实例并传入回调函数
|
||||
// const observer = new MutationObserver(function (mutations) {
|
||||
// mutations.forEach(function (mutation) {
|
||||
// // console.log("chat list changed", mutation.type); // 输出 mutation 的类型
|
||||
// // 获得当前聊天窗口
|
||||
// window.LLAPI.getPeer().then(peer => {
|
||||
// // console.log("current peer", peer)
|
||||
// if (peer && peer.chatType == "group") {
|
||||
// getGroupMembers(peer.uid, false).then()
|
||||
// }
|
||||
// })
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// // 配置观察选项
|
||||
// const config = {attributes: true, childList: true, subtree: true};
|
||||
//
|
||||
// // 传入目标节点和观察选项
|
||||
// observer.observe(targetNode, config);
|
||||
//
|
||||
// } catch (e) {
|
||||
// window.llonebot.log(e)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // getChatListEle();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// 打开设置界面时触发
|
||||
async function onSettingWindowCreated (view: Element) {
|
||||
async function onSettingWindowCreated(view: Element) {
|
||||
window.llonebot.log("setting window created");
|
||||
const {port, hosts} = await window.llonebot.getConfig()
|
||||
|
||||
|
Reference in New Issue
Block a user