mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0a3ae76b89 | ||
![]() |
ddd60a6a79 | ||
![]() |
e4d8c5e72e | ||
![]() |
6d33fb8b14 | ||
![]() |
2350e4dc75 | ||
![]() |
600addbf82 | ||
![]() |
f07f0111cd | ||
![]() |
923f72e5d3 | ||
![]() |
5b4001e411 | ||
![]() |
b950f01d51 | ||
![]() |
dc38275660 | ||
![]() |
3d077550cd | ||
![]() |
44fe01f94b | ||
![]() |
5f9679dfbf | ||
![]() |
bffa2d9a31 | ||
![]() |
0c131ff555 | ||
![]() |
a802e95d89 | ||
![]() |
5ae65e1cf8 | ||
![]() |
a826232e96 | ||
![]() |
1bd51d8d7b | ||
![]() |
3d3974d0c7 | ||
![]() |
9a07c8bba1 |
26
README.md
26
README.md
@@ -1,6 +1,6 @@
|
|||||||
# LLOneBot API
|
# LLOneBot API
|
||||||
|
|
||||||
将NTQQLiteLoaderAPI封装成OneBot11/12标准的API
|
将NTQQLiteLoaderAPI封装成OneBot11/12标准的API, V12没有完整测试
|
||||||
|
|
||||||
## 安装方法
|
## 安装方法
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
2.安装修改后的[LiteLoaderQQNT-Plugin-LLAPI](https://github.com/linyuchen/LiteLoaderQQNT-Plugin-LLAPI),原版的功能有缺陷
|
2.安装修改后的[LiteLoaderQQNT-Plugin-LLAPI](https://github.com/linyuchen/LiteLoaderQQNT-Plugin-LLAPI),原版的功能有缺陷
|
||||||
|
|
||||||
3.安装本项目插件[OneBotApi](https://github.com/linyuchen/LiteLoaderQQNT-OneBotApi)
|
3.安装本项目插件[OneBotApi](https://github.com/linyuchen/LiteLoaderQQNT-OneBotApi/releases/)
|
||||||
|
|
||||||
*关于插件的安装方法: 上述的两个插件都没有上架NTQQLiteLoader插件市场,需要自己下载源码复制到插件目录*
|
*关于插件的安装方法: 上述的两个插件都没有上架NTQQLiteLoader插件市场,需要自己下载源码复制到插件目录*
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
## 支持的API
|
## 支持的API
|
||||||
|
|
||||||
目前只支持http协议,不支持websocket,事件上报也是http协议
|
目前只支持http协议POST方法,不支持websocket,事件上报也是http协议
|
||||||
|
|
||||||
- [x] 获取群列表
|
- [x] 获取群列表
|
||||||
- [x] 获取群成员列表
|
- [x] 获取群成员列表
|
||||||
@@ -38,4 +38,24 @@
|
|||||||
- [ ] 转发消息记录
|
- [ ] 转发消息记录
|
||||||
- [ ] xml
|
- [ ] xml
|
||||||
|
|
||||||
|
支持的api:
|
||||||
|
- [x] get_login_info
|
||||||
|
- [x] send_msg
|
||||||
|
- [x] send_group_msg
|
||||||
|
- [x] send_private_msg
|
||||||
|
- [x] delete_msg
|
||||||
|
- [x] get_group_list
|
||||||
|
- [x] get_group_member_list
|
||||||
|
- [x] get_group_member_info
|
||||||
|
- [x] get_friend_list
|
||||||
|
|
||||||
**自己发送成功的消息也会上报,可以用于获取需要撤回消息的id**
|
**自己发送成功的消息也会上报,可以用于获取需要撤回消息的id**
|
||||||
|
|
||||||
|
## 示例
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
*暂时不支持`"message": "hello"`这种message为字符串的形式*
|
||||||
|
|
||||||
|
## onebot11文档
|
||||||
|
<https://11.onebot.dev/>
|
||||||
|
BIN
doc/image/example.jpg
Normal file
BIN
doc/image/example.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 102 KiB |
@@ -4,7 +4,7 @@
|
|||||||
"name": "LLOneBot",
|
"name": "LLOneBot",
|
||||||
"slug": "LLOneBot",
|
"slug": "LLOneBot",
|
||||||
"description": "LiteLoaderQQNT的OneBotApi",
|
"description": "LiteLoaderQQNT的OneBotApi",
|
||||||
"version": "0.1.0",
|
"version": "1.2.7",
|
||||||
"thumbnail": "./icon.png",
|
"thumbnail": "./icon.png",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "linyuchen",
|
"name": "linyuchen",
|
||||||
|
@@ -9,9 +9,9 @@
|
|||||||
"build-main": "webpack --config webpack.main.config.js",
|
"build-main": "webpack --config webpack.main.config.js",
|
||||||
"build-preload": "webpack --config webpack.preload.config.js",
|
"build-preload": "webpack --config webpack.preload.config.js",
|
||||||
"build-renderer": "webpack --config webpack.renderer.config.js",
|
"build-renderer": "webpack --config webpack.renderer.config.js",
|
||||||
"build-mac": "npm run build && npm run deploy-mac",
|
"build-mac": "npm run build && cp manifest.json dist/ && npm run deploy-mac",
|
||||||
"deploy-mac": "cp dist/* ~/Library/Containers/com.tencent.qq/Data/Documents/LiteLoaderQQNT/plugins/LLOnebot/",
|
"deploy-mac": "cp dist/* ~/Library/Containers/com.tencent.qq/Data/Documents/LiteLoaderQQNT/plugins/LLOnebot/",
|
||||||
"build-win": "npm run build && npm run deploy-win",
|
"build-win": "npm run build && cp manifest.json dist/ && npm run deploy-win",
|
||||||
"deploy-win": "cmd /c \"copy dist\\* %USERPROFILE%\\documents\\LiteLoaderQQNT\\plugins\\LLOnebot\\\""
|
"deploy-win": "cmd /c \"copy dist\\* %USERPROFILE%\\documents\\LiteLoaderQQNT\\plugins\\LLOnebot\\\""
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
|
@@ -7,3 +7,7 @@ export const CHANNEL_UPDATE_GROUPS = "llonebot_update_groups"
|
|||||||
export const CHANNEL_UPDATE_FRIENDS = "llonebot_update_friends"
|
export const CHANNEL_UPDATE_FRIENDS = "llonebot_update_friends"
|
||||||
export const CHANNEL_LOG = "llonebot_log"
|
export const CHANNEL_LOG = "llonebot_log"
|
||||||
export const CHANNEL_POST_ONEBOT_DATA = "llonebot_post_onebot_data"
|
export const CHANNEL_POST_ONEBOT_DATA = "llonebot_post_onebot_data"
|
||||||
|
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"
|
@@ -17,6 +17,18 @@ export type GroupMemberInfo = {
|
|||||||
uin: string; // QQ号
|
uin: string; // QQ号
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const OnebotGroupMemberRole = {
|
||||||
|
4: 'owner',
|
||||||
|
3: 'admin',
|
||||||
|
2: 'member'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export type SelfInfo = {
|
||||||
|
user_id: string;
|
||||||
|
nickname: string;
|
||||||
|
}
|
||||||
|
|
||||||
export type User = {
|
export type User = {
|
||||||
avatarUrl?: string;
|
avatarUrl?: string;
|
||||||
bio?: string; // 签名
|
bio?: string; // 签名
|
||||||
@@ -102,7 +114,7 @@ export type SendMessage = {
|
|||||||
text: string, // 纯文本
|
text: string, // 纯文本
|
||||||
}
|
}
|
||||||
} | {
|
} | {
|
||||||
type: "image" | "voice",
|
type: "image" | "voice" | "record",
|
||||||
file: string, // 本地路径
|
file: string, // 本地路径
|
||||||
data?: {
|
data?: {
|
||||||
file: string // 本地路径
|
file: string // 本地路径
|
||||||
@@ -126,8 +138,11 @@ 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"
|
||||||
|
|
||||||
export type PostDataSendMsg = {
|
export type PostDataSendMsg = {
|
||||||
action: "send_private_msg" | "send_group_msg" | "get_group_list" | "get_friend_list" | "delete_msg",
|
action: PostDataAction
|
||||||
message_type?: "private" | "group"
|
message_type?: "private" | "group"
|
||||||
params?: {
|
params?: {
|
||||||
user_id: string,
|
user_id: string,
|
||||||
@@ -141,5 +156,5 @@ export type PostDataSendMsg = {
|
|||||||
|
|
||||||
export type Config = {
|
export type Config = {
|
||||||
port: number,
|
port: number,
|
||||||
host: string,
|
hosts: string[],
|
||||||
}
|
}
|
||||||
|
23
src/global.d.ts
vendored
23
src/global.d.ts
vendored
@@ -1,16 +1,25 @@
|
|||||||
import {Config, Group, GroupMemberInfo, MessageElement, Peer, PostDataSendMsg, SendMessage, User} from "./common/types";
|
import {
|
||||||
|
Config,
|
||||||
|
Group,
|
||||||
|
GroupMemberInfo,
|
||||||
|
MessageElement,
|
||||||
|
Peer,
|
||||||
|
PostDataSendMsg,
|
||||||
|
SelfInfo,
|
||||||
|
SendMessage,
|
||||||
|
User
|
||||||
|
} from "./common/types";
|
||||||
|
|
||||||
|
|
||||||
declare var LLAPI: {
|
declare var LLAPI: {
|
||||||
on(event: "new-messages" | "new-send-messages", callback: (data: MessageElement[]) => void): void;
|
on(event: "new-messages" | "new-send-messages", callback: (data: MessageElement[]) => void): void;
|
||||||
on(event: "context-msg-menu", callback: (event: any, target: any, msgIds:any) => void): void;
|
on(event: "context-msg-menu", callback: (event: any, target: any, msgIds:any) => void): void;
|
||||||
getAccountInfo(): Promise<{
|
getAccountInfo(): Promise<{
|
||||||
uid: string // qq
|
uid: string // 一串加密的字符串
|
||||||
uin: string // 一串加密的字符串
|
uin: string // qq
|
||||||
}>
|
}>
|
||||||
|
|
||||||
// uid是一串加密的字符串
|
getUserInfo(uid: string): Promise<User>; // uid是一串加密的字符串
|
||||||
getUserInfo(uid: string): Promise<User>;
|
|
||||||
sendMessage(peer: Peer, message: SendMessage[]): Promise<any>;
|
sendMessage(peer: Peer, message: SendMessage[]): Promise<any>;
|
||||||
recallMessage(peer: Peer, msgIds: string[]): Promise<void>;
|
recallMessage(peer: Peer, msgIds: string[]): Promise<void>;
|
||||||
getGroupsList(forced: boolean): Promise<Group[]>
|
getGroupsList(forced: boolean): Promise<Group[]>
|
||||||
@@ -33,6 +42,10 @@ declare var llonebot: {
|
|||||||
log(data: any): void,
|
log(data: any): void,
|
||||||
setConfig(config: Config):void;
|
setConfig(config: Config):void;
|
||||||
getConfig():Promise<Config>;
|
getConfig():Promise<Config>;
|
||||||
|
setSelfInfo(selfInfo: SelfInfo):void;
|
||||||
|
downloadFile(arg: {uri: string, localFilePath: string}):Promise<string>;
|
||||||
|
deleteFile(path: string[]):Promise<void>;
|
||||||
|
getRunningStatus(): Promise<boolean>;
|
||||||
};
|
};
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
@@ -1,23 +1,85 @@
|
|||||||
import {sendIPCRecallQQMsg, sendIPCSendQQMsg} from "./IPCSend";
|
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
import {PostDataSendMsg} from "../common/types";
|
const bodyParser = require('body-parser');
|
||||||
import {friends, groups} from "./data";
|
import {sendIPCRecallQQMsg, sendIPCSendQQMsg} from "./IPCSend";
|
||||||
|
import {OnebotGroupMemberRole, PostDataAction, PostDataSendMsg, SendMessage} from "../common/types";
|
||||||
|
import {friends, groups, selfInfo} from "./data";
|
||||||
|
|
||||||
|
// @SiberianHusky 2021-08-15
|
||||||
|
function checkSendMessage(sendMsgList: SendMessage[]) {
|
||||||
|
function checkUri(uri: string): boolean {
|
||||||
|
const pattern = /^(file:\/\/|http:\/\/|https:\/\/|base64:\/\/)/;
|
||||||
|
return pattern.test(uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let msg of sendMsgList) {
|
||||||
|
if (msg["type"] && msg["data"]) {
|
||||||
|
let type = msg["type"];
|
||||||
|
let data = msg["data"];
|
||||||
|
if (type === "text" && !data["text"]) {
|
||||||
|
return 400;
|
||||||
|
} else if (["image", "voice"].includes(type)) {
|
||||||
|
if (!data["file"]) {
|
||||||
|
return 400;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if (checkUri(data["file"])) {
|
||||||
|
return 200;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return 400;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (type === "at" && !data["qq"]) {
|
||||||
|
return 400;
|
||||||
|
} else if (type === "reply" && !data["id"]) {
|
||||||
|
return 400;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return 400
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 200;
|
||||||
|
}
|
||||||
|
// ==end==
|
||||||
|
|
||||||
function handlePost(jsonData: any) {
|
function handlePost(jsonData: any) {
|
||||||
|
if (!jsonData.params) {
|
||||||
|
jsonData.params = jsonData
|
||||||
|
|
||||||
|
}
|
||||||
let resData = {
|
let resData = {
|
||||||
status: 0,
|
status: 0,
|
||||||
retcode: 0,
|
retcode: 0,
|
||||||
data: {},
|
data: {},
|
||||||
message: ''
|
message: ''
|
||||||
}
|
}
|
||||||
if (jsonData.action == "send_private_msg" || jsonData.action == "send_group_msg") {
|
if (jsonData.action == "get_login_info") {
|
||||||
sendIPCSendQQMsg(jsonData);
|
resData["data"] = selfInfo
|
||||||
|
} else if (jsonData.action == "send_private_msg" || jsonData.action == "send_group_msg") {
|
||||||
|
if (jsonData.action == "send_private_msg") {
|
||||||
|
jsonData.message_type = "private"
|
||||||
|
} else {
|
||||||
|
jsonData.message_type = "group"
|
||||||
|
}
|
||||||
|
// @SiberianHuskY 2021-10-20 22:00:00
|
||||||
|
resData.status = checkSendMessage(jsonData.message);
|
||||||
|
if (resData.status == 200) {
|
||||||
|
resData.message = "发送成功";
|
||||||
|
resData.data = jsonData.message;
|
||||||
|
sendIPCSendQQMsg(jsonData);
|
||||||
|
} else {
|
||||||
|
resData.message = "发送失败, 请检查消息格式";
|
||||||
|
resData.data = jsonData.message;
|
||||||
|
}
|
||||||
|
// == end ==
|
||||||
} else if (jsonData.action == "get_group_list") {
|
} else if (jsonData.action == "get_group_list") {
|
||||||
resData["data"] = groups.map(group => {
|
resData["data"] = groups.map(group => {
|
||||||
return {
|
return {
|
||||||
group_id: group.uid,
|
group_id: group.uid,
|
||||||
group_name: group.name,
|
group_name: group.name,
|
||||||
|
member_count: group.members.length,
|
||||||
group_members: group.members.map(member => {
|
group_members: group.members.map(member => {
|
||||||
return {
|
return {
|
||||||
user_id: member.uin,
|
user_id: member.uin,
|
||||||
@@ -27,33 +89,36 @@ function handlePost(jsonData: any) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} else if (jsonData.action == "get_group_info") {
|
||||||
|
let group = groups.find(group => group.uid == jsonData.params.group_id)
|
||||||
|
if (group) {
|
||||||
|
resData["data"] = {
|
||||||
|
group_id: group.uid,
|
||||||
|
group_name: group.name,
|
||||||
|
member_count: group.members.length,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (jsonData.action == "get_group_member_info") {
|
||||||
|
let member = groups.find(group => group.uid == jsonData.params.group_id)?.members?.find(member => member.uin == jsonData.params.user_id)
|
||||||
|
resData["data"] = {
|
||||||
|
user_id: member.uin,
|
||||||
|
user_name: member.nick,
|
||||||
|
user_display_name: member.cardName || member.nick,
|
||||||
|
nickname: member.nick,
|
||||||
|
card: member.cardName,
|
||||||
|
role: OnebotGroupMemberRole[member.role],
|
||||||
|
}
|
||||||
} else if (jsonData.action == "get_group_member_list") {
|
} else if (jsonData.action == "get_group_member_list") {
|
||||||
let group = groups.find(group => group.uid == jsonData.params.group_id)
|
let group = groups.find(group => group.uid == jsonData.params.group_id)
|
||||||
if (group) {
|
if (group) {
|
||||||
resData["data"] = group?.members?.map(member => {
|
resData["data"] = group?.members?.map(member => {
|
||||||
let role = "member"
|
|
||||||
switch (member.role) {
|
|
||||||
case 4: {
|
|
||||||
role = "owner"
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 3: {
|
|
||||||
role = "admin"
|
|
||||||
break
|
|
||||||
}
|
|
||||||
case 2: {
|
|
||||||
role = "member"
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
user_id: member.uin,
|
user_id: member.uin,
|
||||||
user_name: member.nick,
|
user_name: member.nick,
|
||||||
user_display_name: member.cardName || member.nick,
|
user_display_name: member.cardName || member.nick,
|
||||||
nickname: member.nick,
|
nickname: member.nick,
|
||||||
card: member.cardName,
|
card: member.cardName,
|
||||||
role
|
role: OnebotGroupMemberRole[member.role],
|
||||||
}
|
}
|
||||||
|
|
||||||
}) || []
|
}) || []
|
||||||
@@ -76,11 +141,27 @@ function handlePost(jsonData: any) {
|
|||||||
|
|
||||||
export function startExpress(port: number) {
|
export function startExpress(port: number) {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
// 中间件,用于解析POST请求的请求体
|
// 中间件,用于解析POST请求的请求体
|
||||||
app.use(express.urlencoded({extended: true}));
|
app.use(express.urlencoded({extended: true, limit: "500mb"}));
|
||||||
|
app.use(bodyParser({limit: '500mb'}))
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
function parseToOnebot12(action: PostDataAction) {
|
||||||
|
app.post('/' + action, (req: any, res: any) => {
|
||||||
|
let jsonData: PostDataSendMsg = req.body;
|
||||||
|
jsonData.action = action
|
||||||
|
let resData = handlePost(jsonData)
|
||||||
|
res.send(resData)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const actionList: PostDataAction[] = ["get_login_info", "send_private_msg", "send_group_msg",
|
||||||
|
"get_group_list", "get_friend_list", "delete_msg", "get_group_member_list", "get_group_member_info"]
|
||||||
|
|
||||||
|
for (const action of actionList) {
|
||||||
|
parseToOnebot12(action as PostDataAction)
|
||||||
|
}
|
||||||
|
|
||||||
app.get('/', (req: any, res: any) => {
|
app.get('/', (req: any, res: any) => {
|
||||||
res.send('llonebot已启动');
|
res.send('llonebot已启动');
|
||||||
})
|
})
|
||||||
@@ -92,18 +173,6 @@ export function startExpress(port: number) {
|
|||||||
let resData = handlePost(jsonData)
|
let resData = handlePost(jsonData)
|
||||||
res.send(resData)
|
res.send(resData)
|
||||||
});
|
});
|
||||||
app.post('/send_private_msg', (req: any, res: any) => {
|
|
||||||
let jsonData: PostDataSendMsg = req.body;
|
|
||||||
jsonData.action = "send_private_msg"
|
|
||||||
let resData = handlePost(jsonData)
|
|
||||||
res.send(resData)
|
|
||||||
})
|
|
||||||
app.post('/send_group_msg', (req: any, res: any) => {
|
|
||||||
let jsonData: PostDataSendMsg = req.body;
|
|
||||||
jsonData.action = "send_group_msg"
|
|
||||||
let resData = handlePost(jsonData)
|
|
||||||
res.send(resData)
|
|
||||||
})
|
|
||||||
app.post('/send_msg', (req: any, res: any) => {
|
app.post('/send_msg', (req: any, res: any) => {
|
||||||
let jsonData: PostDataSendMsg = req.body;
|
let jsonData: PostDataSendMsg = req.body;
|
||||||
if (jsonData.message_type == "private") {
|
if (jsonData.message_type == "private") {
|
||||||
@@ -120,12 +189,7 @@ export function startExpress(port: number) {
|
|||||||
let resData = handlePost(jsonData)
|
let resData = handlePost(jsonData)
|
||||||
res.send(resData)
|
res.send(resData)
|
||||||
})
|
})
|
||||||
app.post('/delete_msg', (req: any, res: any) => {
|
|
||||||
let jsonData: PostDataSendMsg = req.body;
|
|
||||||
jsonData.action = "delete_msg"
|
|
||||||
let resData = handlePost(jsonData)
|
|
||||||
res.send(resData)
|
|
||||||
})
|
|
||||||
app.listen(port, "0.0.0.0", () => {
|
app.listen(port, "0.0.0.0", () => {
|
||||||
console.log(`服务器已启动,监听端口 ${port}`);
|
console.log(`服务器已启动,监听端口 ${port}`);
|
||||||
});
|
});
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import {webContents} from 'electron';
|
import {ipcMain, webContents} from 'electron';
|
||||||
import {PostDataSendMsg} from "../common/types";
|
import {PostDataSendMsg} from "../common/types";
|
||||||
import {CHANNEL_RECALL_MSG, CHANNEL_SEND_MSG} from "../common/IPCChannel";
|
import {CHANNEL_RECALL_MSG, CHANNEL_SEND_MSG} from "../common/IPCChannel";
|
||||||
|
|
||||||
|
@@ -11,10 +11,18 @@ export class ConfigUtil{
|
|||||||
|
|
||||||
getConfig(): Config{
|
getConfig(): Config{
|
||||||
if (!fs.existsSync(this.configPath)) {
|
if (!fs.existsSync(this.configPath)) {
|
||||||
return {"port":3000, "host": "http://localhost:5000/"}
|
return {port:3000, hosts: ["http://192.168.1.2:5000/"]}
|
||||||
} else {
|
} else {
|
||||||
const data = fs.readFileSync(this.configPath, "utf-8");
|
const data = fs.readFileSync(this.configPath, "utf-8");
|
||||||
return JSON.parse(data);
|
let jsonData =JSON.parse(data);
|
||||||
|
if (!jsonData.hosts){
|
||||||
|
jsonData.hosts = []
|
||||||
|
}
|
||||||
|
return jsonData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setConfig(config: Config){
|
||||||
|
fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), "utf-8")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,9 @@
|
|||||||
import {Group, User} from "../common/types";
|
import {Group, SelfInfo, User} from "../common/types";
|
||||||
|
|
||||||
export let groups: Group[] = []
|
export let groups: Group[] = []
|
||||||
export let friends: User[] = []
|
export let friends: User[] = []
|
||||||
|
|
||||||
|
export let selfInfo: SelfInfo = {
|
||||||
|
user_id: "",
|
||||||
|
nickname: ""
|
||||||
|
}
|
103
src/main/main.ts
103
src/main/main.ts
@@ -1,42 +1,72 @@
|
|||||||
// 运行在 Electron 主进程 下的插件入口
|
// 运行在 Electron 主进程 下的插件入口
|
||||||
|
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
|
||||||
const fs = require('fs');
|
|
||||||
import {ipcMain} from 'electron';
|
import {ipcMain} from 'electron';
|
||||||
|
|
||||||
import {Config, Group, User} from "../common/types";
|
import {Config, Group, SelfInfo, User} from "../common/types";
|
||||||
import {
|
import {
|
||||||
CHANNEL_GET_CONFIG, CHANNEL_LOG, CHANNEL_POST_ONEBOT_DATA,
|
CHANNEL_DOWNLOAD_FILE,
|
||||||
|
CHANNEL_GET_CONFIG,
|
||||||
|
CHANNEL_SET_SELF_INFO,
|
||||||
|
CHANNEL_LOG,
|
||||||
|
CHANNEL_POST_ONEBOT_DATA,
|
||||||
CHANNEL_SET_CONFIG,
|
CHANNEL_SET_CONFIG,
|
||||||
CHANNEL_START_HTTP_SERVER, CHANNEL_UPDATE_FRIENDS,
|
CHANNEL_START_HTTP_SERVER,
|
||||||
CHANNEL_UPDATE_GROUPS
|
CHANNEL_UPDATE_FRIENDS,
|
||||||
|
CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE, CHANNEL_GET_RUNNING_STATUS
|
||||||
} from "../common/IPCChannel";
|
} from "../common/IPCChannel";
|
||||||
import {ConfigUtil} from "./config";
|
import {ConfigUtil} from "./config";
|
||||||
import {startExpress} from "./HttpServer";
|
import {startExpress} from "./HttpServer";
|
||||||
import {log} from "./utils";
|
import {isGIF, log} from "./utils";
|
||||||
import {friends, groups} from "./data";
|
import {friends, groups, selfInfo} from "./data";
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
let running = false;
|
||||||
|
|
||||||
|
|
||||||
// 加载插件时触发
|
// 加载插件时触发
|
||||||
function onLoad(plugin: any) {
|
function onLoad(plugin: any) {
|
||||||
|
log("main onLoaded");
|
||||||
const configFilePath = path.join(plugin.path.data, "config.json")
|
function getConfigUtil() {
|
||||||
let configUtil = new ConfigUtil(configFilePath)
|
const configFilePath = path.join(plugin.path.data, `config_${selfInfo.user_id}.json`)
|
||||||
|
return new ConfigUtil(configFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(plugin.path.data)) {
|
if (!fs.existsSync(plugin.path.data)) {
|
||||||
fs.mkdirSync(plugin.path.data, {recursive: true});
|
fs.mkdirSync(plugin.path.data, {recursive: true});
|
||||||
}
|
}
|
||||||
ipcMain.handle(CHANNEL_GET_CONFIG, (event: any, arg: any) => {
|
ipcMain.handle(CHANNEL_GET_CONFIG, (event: any, arg: any) => {
|
||||||
return configUtil.getConfig()
|
return getConfigUtil().getConfig()
|
||||||
|
})
|
||||||
|
ipcMain.handle(CHANNEL_DOWNLOAD_FILE, async (event: any, arg: {uri: string, localFilePath: string}) => {
|
||||||
|
let url = new URL(arg.uri);
|
||||||
|
if (url.protocol == "base64:"){
|
||||||
|
// base64转成文件
|
||||||
|
let base64Data = arg.uri.split("base64://")[1]
|
||||||
|
const buffer = Buffer.from(base64Data, 'base64');
|
||||||
|
|
||||||
|
fs.writeFileSync(arg.localFilePath, buffer);
|
||||||
|
}
|
||||||
|
else if (url.protocol == "http:" || url.protocol == "https:") {
|
||||||
|
// 下载文件
|
||||||
|
let res = await fetch(url)
|
||||||
|
let blob = await res.blob();
|
||||||
|
let buffer = await blob.arrayBuffer();
|
||||||
|
fs.writeFileSync(arg.localFilePath, Buffer.from(buffer));
|
||||||
|
}
|
||||||
|
if (isGIF(arg.localFilePath)) {
|
||||||
|
fs.renameSync(arg.localFilePath, arg.localFilePath + ".gif");
|
||||||
|
arg.localFilePath += ".gif";
|
||||||
|
}
|
||||||
|
return arg.localFilePath;
|
||||||
})
|
})
|
||||||
ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => {
|
ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => {
|
||||||
fs.writeFileSync(configFilePath, JSON.stringify(arg, null, 2), "utf-8")
|
getConfigUtil().setConfig(arg)
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on(CHANNEL_START_HTTP_SERVER, (event: any, arg: any) => {
|
ipcMain.on(CHANNEL_START_HTTP_SERVER, (event: any, arg: any) => {
|
||||||
startExpress(configUtil.getConfig().port)
|
startExpress(getConfigUtil().getConfig().port)
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on(CHANNEL_UPDATE_GROUPS, (event: any, arg: Group[]) => {
|
ipcMain.on(CHANNEL_UPDATE_GROUPS, (event: any, arg: Group[]) => {
|
||||||
@@ -70,20 +100,23 @@ function onLoad(plugin: any) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on(CHANNEL_POST_ONEBOT_DATA, (event: any, arg: any) => {
|
ipcMain.on(CHANNEL_POST_ONEBOT_DATA, (event: any, arg: any) => {
|
||||||
try {
|
for(const host of getConfigUtil().getConfig().hosts) {
|
||||||
fetch(configUtil.getConfig().host, {
|
try {
|
||||||
method: "POST",
|
fetch(host, {
|
||||||
headers: {
|
method: "POST",
|
||||||
"Content-Type": "application/json"
|
headers: {
|
||||||
},
|
"Content-Type": "application/json",
|
||||||
body: JSON.stringify(arg)
|
"x-self-id": selfInfo.user_id
|
||||||
}).then((res: any) => {
|
},
|
||||||
log("新消息事件上传");
|
body: JSON.stringify(arg)
|
||||||
}, (err: any) => {
|
}).then((res: any) => {
|
||||||
log("新消息事件上传失败:" + err + JSON.stringify(arg));
|
log("新消息事件上传");
|
||||||
});
|
}, (err: any) => {
|
||||||
} catch (e: any) {
|
log("新消息事件上传失败:" + err + JSON.stringify(arg));
|
||||||
log(e.toString())
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
log(e.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -91,7 +124,21 @@ function onLoad(plugin: any) {
|
|||||||
log(arg)
|
log(arg)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ipcMain.handle(CHANNEL_SET_SELF_INFO, (event: any, arg: SelfInfo) => {
|
||||||
|
selfInfo.user_id = arg.user_id;
|
||||||
|
selfInfo.nickname = arg.nickname;
|
||||||
|
running = true;
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.on(CHANNEL_DELETE_FILE, (event: any, arg: string[]) => {
|
||||||
|
for (const path of arg) {
|
||||||
|
fs.unlinkSync(path);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.handle(CHANNEL_GET_RUNNING_STATUS, (event: any, arg: any) => {
|
||||||
|
return running;
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -6,3 +6,11 @@ export function log(msg: any) {
|
|||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isGIF(path: string) {
|
||||||
|
const buffer = Buffer.alloc(4);
|
||||||
|
const fd = fs.openSync(path, 'r');
|
||||||
|
fs.readSync(fd, buffer, 0, 4, 0);
|
||||||
|
fs.closeSync(fd);
|
||||||
|
return buffer.toString() === 'GIF8'
|
||||||
|
}
|
@@ -1,11 +1,20 @@
|
|||||||
// Electron 主进程 与 渲染进程 交互的桥梁
|
// Electron 主进程 与 渲染进程 交互的桥梁
|
||||||
|
|
||||||
import {Config, Group, PostDataSendMsg, User} from "./common/types";
|
import {Config, Group, PostDataSendMsg, SelfInfo, User} from "./common/types";
|
||||||
import {
|
import {
|
||||||
CHANNEL_GET_CONFIG, CHANNEL_LOG, CHANNEL_POST_ONEBOT_DATA,
|
CHANNEL_DOWNLOAD_FILE,
|
||||||
CHANNEL_RECALL_MSG, CHANNEL_SEND_MSG,
|
CHANNEL_GET_CONFIG,
|
||||||
|
CHANNEL_SET_SELF_INFO,
|
||||||
|
CHANNEL_LOG,
|
||||||
|
CHANNEL_POST_ONEBOT_DATA,
|
||||||
|
CHANNEL_RECALL_MSG,
|
||||||
|
CHANNEL_SEND_MSG,
|
||||||
CHANNEL_SET_CONFIG,
|
CHANNEL_SET_CONFIG,
|
||||||
CHANNEL_START_HTTP_SERVER, CHANNEL_UPDATE_FRIENDS, CHANNEL_UPDATE_GROUPS
|
CHANNEL_START_HTTP_SERVER,
|
||||||
|
CHANNEL_UPDATE_FRIENDS,
|
||||||
|
CHANNEL_UPDATE_GROUPS,
|
||||||
|
CHANNEL_DELETE_FILE,
|
||||||
|
CHANNEL_GET_RUNNING_STATUS
|
||||||
} from "./common/IPCChannel";
|
} from "./common/IPCChannel";
|
||||||
|
|
||||||
|
|
||||||
@@ -45,6 +54,18 @@ contextBridge.exposeInMainWorld("llonebot", {
|
|||||||
},
|
},
|
||||||
getConfig: async () => {
|
getConfig: async () => {
|
||||||
return ipcRenderer.invoke(CHANNEL_GET_CONFIG);
|
return ipcRenderer.invoke(CHANNEL_GET_CONFIG);
|
||||||
|
},
|
||||||
|
setSelfInfo(selfInfo: SelfInfo){
|
||||||
|
ipcRenderer.invoke(CHANNEL_SET_SELF_INFO, selfInfo)
|
||||||
|
},
|
||||||
|
downloadFile: (arg: {uri: string, localFilePath: string}) => {
|
||||||
|
return ipcRenderer.invoke(CHANNEL_DOWNLOAD_FILE, arg);
|
||||||
|
},
|
||||||
|
deleteFile: async (localFilePath: string[]) => {
|
||||||
|
ipcRenderer.send(CHANNEL_DELETE_FILE, localFilePath);
|
||||||
|
},
|
||||||
|
getRunningStatus: () => {
|
||||||
|
return ipcRenderer.invoke(CHANNEL_GET_RUNNING_STATUS);
|
||||||
}
|
}
|
||||||
// startExpress,
|
// startExpress,
|
||||||
});
|
});
|
224
src/renderer.ts
224
src/renderer.ts
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
// import express from "express";
|
// import express from "express";
|
||||||
// const { ipcRenderer } = require('electron');
|
// const { ipcRenderer } = require('electron');
|
||||||
import {AtType, Group, MessageElement, Peer, PostDataSendMsg, User} from "./common/types";
|
import {AtType, Group, MessageElement, OnebotGroupMemberRole, Peer, PostDataSendMsg, User} from "./common/types";
|
||||||
|
import * as stream from "stream";
|
||||||
|
import {raw} from "express";
|
||||||
|
|
||||||
let self_qq: string = ""
|
let self_qq: string = ""
|
||||||
let groups: Group[] = []
|
let groups: Group[] = []
|
||||||
@@ -19,8 +21,27 @@ 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) {
|
||||||
@@ -38,11 +59,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,16 +120,26 @@ async function handleNewMessage(messages: MessageElement[]) {
|
|||||||
sub_type: "",
|
sub_type: "",
|
||||||
message: []
|
message: []
|
||||||
}
|
}
|
||||||
// let group: Group
|
|
||||||
if (message.peer.chatType == "group") {
|
if (message.peer.chatType == "group") {
|
||||||
let group_id = message.peer.uid
|
let group_id = message.peer.uid
|
||||||
let group = (await getGroup(group_id))!
|
let group = (await getGroup(group_id))!
|
||||||
onebot_message_data["group_id"] = message.peer.uid
|
onebot_message_data["group_id"] = message.peer.uid
|
||||||
let groupMember = await getGroupMember(group_id, message.sender.uid)
|
let groupMember = await getGroupMember(group_id, message.sender.uid)
|
||||||
onebot_message_data["user_id"] = groupMember!.uin
|
onebot_message_data["user_id"] = groupMember!.uin
|
||||||
|
onebot_message_data.sender = {
|
||||||
|
user_id: groupMember!.uin,
|
||||||
|
nickname: groupMember!.nick,
|
||||||
|
card: groupMember!.cardName,
|
||||||
|
role: OnebotGroupMemberRole[groupMember!.role]
|
||||||
|
}
|
||||||
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 = await getFriend(message.sender.uid)
|
||||||
|
onebot_message_data.sender = {
|
||||||
|
user_id: friend!.uin,
|
||||||
|
nickname: friend!.nickName
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (let element of message.raw.elements) {
|
for (let element of message.raw.elements) {
|
||||||
let message_data: any = {
|
let message_data: any = {
|
||||||
@@ -132,7 +163,11 @@ async function handleNewMessage(messages: MessageElement[]) {
|
|||||||
message_data["type"] = "image"
|
message_data["type"] = "image"
|
||||||
message_data["data"]["file_id"] = element.picElement.fileUuid
|
message_data["data"]["file_id"] = element.picElement.fileUuid
|
||||||
message_data["data"]["path"] = element.picElement.sourcePath
|
message_data["data"]["path"] = element.picElement.sourcePath
|
||||||
message_data["data"]["file"] = element.picElement.sourcePath
|
let startS = "file://"
|
||||||
|
if (!element.picElement.sourcePath.startsWith("/")) {
|
||||||
|
startS += "/"
|
||||||
|
}
|
||||||
|
message_data["data"]["file"] = startS + element.picElement.sourcePath
|
||||||
} else if (element.replyElement) {
|
} else if (element.replyElement) {
|
||||||
message_data["type"] = "reply"
|
message_data["type"] = "reply"
|
||||||
message_data["data"]["id"] = msgHistory.find(msg => msg.raw.msgSeq == element.replyElement.replayMsgSeq)?.raw.msgId
|
message_data["data"]["id"] = msgHistory.find(msg => msg.raw.msgSeq == element.replyElement.replayMsgSeq)?.raw.msgId
|
||||||
@@ -148,7 +183,7 @@ async function handleNewMessage(messages: MessageElement[]) {
|
|||||||
async function listenSendMessage(postData: PostDataSendMsg) {
|
async function listenSendMessage(postData: PostDataSendMsg) {
|
||||||
if (postData.action == "send_private_msg" || postData.action == "send_group_msg") {
|
if (postData.action == "send_private_msg" || postData.action == "send_group_msg") {
|
||||||
let peer: Peer | null = null;
|
let peer: Peer | null = null;
|
||||||
if (!postData.params){
|
if (!postData.params) {
|
||||||
postData.params = {
|
postData.params = {
|
||||||
message: postData.message,
|
message: postData.message,
|
||||||
user_id: postData.user_id,
|
user_id: postData.user_id,
|
||||||
@@ -156,12 +191,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") {
|
||||||
@@ -178,8 +213,9 @@ async function listenSendMessage(postData: PostDataSendMsg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (peer) {
|
if (peer) {
|
||||||
for (let message of postData.params.message){
|
let sendFiles: string[] = [];
|
||||||
if (message.type == "at"){
|
for (let message of postData.params.message) {
|
||||||
|
if (message.type == "at") {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
message.type = "text"
|
message.type = "text"
|
||||||
message.atType = AtType.atUser
|
message.atType = AtType.atUser
|
||||||
@@ -189,14 +225,29 @@ async function listenSendMessage(postData: PostDataSendMsg) {
|
|||||||
message.atNtUid = atMember.uid
|
message.atNtUid = atMember.uid
|
||||||
message.atUid = atUid
|
message.atUid = atUid
|
||||||
message.content = `@${atMember.cardName || atMember.nick}`
|
message.content = `@${atMember.cardName || atMember.nick}`
|
||||||
}
|
} else if (message.type == "text") {
|
||||||
else if (message.type == "text"){
|
|
||||||
message.content = message.data?.text || message.content
|
message.content = message.data?.text || message.content
|
||||||
}
|
} else if (message.type == "image" || message.type == "voice" || message.type == "record") {
|
||||||
else if (message.type == "image" || message.type == "voice"){
|
// todo: 收到的应该是uri格式的,需要转成本地的, uri格式有三种,http, file, base64
|
||||||
message.file = message.data?.file || message.file
|
let url = message.data?.file || message.file
|
||||||
}
|
let uri = new URL(url);
|
||||||
else if (message.type == "reply"){
|
let ext: string;
|
||||||
|
if (message.type == "image") {
|
||||||
|
ext = ".png"
|
||||||
|
}
|
||||||
|
if (message.type == "voice" || message.type == "record") {
|
||||||
|
message.type = "voice"
|
||||||
|
ext = ".amr"
|
||||||
|
}
|
||||||
|
let localFilePath = `${Date.now()}${ext}`
|
||||||
|
if (uri.protocol == "file:") {
|
||||||
|
localFilePath = url.split("file://")[1]
|
||||||
|
} else {
|
||||||
|
localFilePath = await window.llonebot.downloadFile({uri: url, localFilePath: localFilePath})
|
||||||
|
}
|
||||||
|
message.file = localFilePath
|
||||||
|
sendFiles.push(localFilePath);
|
||||||
|
} else if (message.type == "reply") {
|
||||||
let msgId = message.data?.id || message.msgId
|
let msgId = message.data?.id || message.msgId
|
||||||
let replyMessage = msgHistory.find(msg => msg.raw.msgId == msgId)
|
let replyMessage = msgHistory.find(msg => msg.raw.msgId == msgId)
|
||||||
message.msgId = msgId
|
message.msgId = msgId
|
||||||
@@ -204,7 +255,12 @@ 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)
|
||||||
|
if (sendFiles.length) {
|
||||||
|
window.llonebot.deleteFile(sendFiles);
|
||||||
|
}
|
||||||
|
},
|
||||||
err => console.log("消息发送失败", postData, err))
|
err => console.log("消息发送失败", postData, err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,14 +273,11 @@ function recallMessage(msgId: string) {
|
|||||||
|
|
||||||
let chatListEle: HTMLCollectionOf<Element>
|
let chatListEle: HTMLCollectionOf<Element>
|
||||||
|
|
||||||
function onLoad() {
|
async function onLoad(arg: any) {
|
||||||
window.llonebot.startExpress();
|
let runningStatus = await window.llonebot.getRunningStatus();
|
||||||
window.llonebot.listenSendMessage((postData: PostDataSendMsg) => {
|
if (runningStatus) {
|
||||||
listenSendMessage(postData).then()
|
return;
|
||||||
});
|
}
|
||||||
window.llonebot.listenRecallMessage((arg: { message_id: string }) => {
|
|
||||||
recallMessage(arg.message_id)
|
|
||||||
})
|
|
||||||
|
|
||||||
async function getGroupsMembers(groupsArg: Group[]) {
|
async function getGroupsMembers(groupsArg: Group[]) {
|
||||||
// 批量获取群成员列表
|
// 批量获取群成员列表
|
||||||
@@ -240,11 +293,13 @@ function onLoad() {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
getGroupsMembers(failedGroups).then()
|
getGroupsMembers(failedGroups).then()
|
||||||
}, 1000)
|
}, 1000)
|
||||||
}
|
} else {
|
||||||
else{
|
window.llonebot.log("全部群成员获取完毕")
|
||||||
console.log("全部群成员获取完毕", groups)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
await getFriends();
|
||||||
|
await getGroups();
|
||||||
|
await getGroupsMembers(groups);
|
||||||
|
|
||||||
function onNewMessages(messages: MessageElement[]) {
|
function onNewMessages(messages: MessageElement[]) {
|
||||||
async function func(messages: MessageElement[]) {
|
async function func(messages: MessageElement[]) {
|
||||||
@@ -257,15 +312,31 @@ function onLoad() {
|
|||||||
|
|
||||||
func(messages).then(() => {
|
func(messages).then(() => {
|
||||||
})
|
})
|
||||||
console.log("chatListEle", chatListEle)
|
// console.log("chatListEle", chatListEle)
|
||||||
}
|
}
|
||||||
|
window.LLAPI.on("new-messages", onNewMessages);
|
||||||
|
window.LLAPI.on("new-send-messages", onNewMessages);
|
||||||
|
|
||||||
getGroups().then(()=>{
|
let accountInfo = await window.LLAPI.getAccountInfo();
|
||||||
getGroupsMembers(groups).then(()=>{
|
window.llonebot.log("getAccountInfo " + JSON.stringify(accountInfo));
|
||||||
window.LLAPI.on("new-messages", onNewMessages);
|
if (!accountInfo.uid) {
|
||||||
window.LLAPI.on("new-send-messages", onNewMessages);
|
return;
|
||||||
})
|
}
|
||||||
|
let selfInfo = await window.LLAPI.getUserInfo(accountInfo.uid);
|
||||||
|
window.llonebot.setSelfInfo({
|
||||||
|
user_id: accountInfo.uin,
|
||||||
|
nickname: selfInfo.nickName
|
||||||
|
});
|
||||||
|
window.llonebot.log("selfInfo " + JSON.stringify(selfInfo))
|
||||||
|
window.llonebot.startExpress();
|
||||||
|
|
||||||
|
window.llonebot.listenSendMessage((postData: PostDataSendMsg) => {
|
||||||
|
listenSendMessage(postData).then().catch(err => console.log("listenSendMessage err", err))
|
||||||
})
|
})
|
||||||
|
window.llonebot.listenRecallMessage((arg: { message_id: string }) => {
|
||||||
|
recallMessage(arg.message_id)
|
||||||
|
})
|
||||||
|
window.llonebot.log("llonebot loaded");
|
||||||
|
|
||||||
window.LLAPI.add_qmenu((qContextMenu: Node) => {
|
window.LLAPI.add_qmenu((qContextMenu: Node) => {
|
||||||
let btn = document.createElement("a")
|
let btn = document.createElement("a")
|
||||||
@@ -273,7 +344,7 @@ function onLoad() {
|
|||||||
btn.setAttribute("aria-disabled", "false")
|
btn.setAttribute("aria-disabled", "false")
|
||||||
btn.setAttribute("role", "menuitem")
|
btn.setAttribute("role", "menuitem")
|
||||||
btn.setAttribute("tabindex", "-1")
|
btn.setAttribute("tabindex", "-1")
|
||||||
btn.onclick = ()=>{
|
btn.onclick = () => {
|
||||||
// window.LLAPI.getPeer().then(peer => {
|
// window.LLAPI.getPeer().then(peer => {
|
||||||
// // console.log("current peer", peer)
|
// // console.log("current peer", peer)
|
||||||
// if (peer && peer.chatType == "group") {
|
// if (peer && peer.chatType == "group") {
|
||||||
@@ -288,7 +359,8 @@ function onLoad() {
|
|||||||
await getGroupMembers(group.uid, true)
|
await getGroupMembers(group.uid, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func().then(()=> {
|
|
||||||
|
func().then(() => {
|
||||||
console.log("获取群成员列表结果", groups);
|
console.log("获取群成员列表结果", groups);
|
||||||
// 找到members数量为空的群
|
// 找到members数量为空的群
|
||||||
groups.map(group => {
|
groups.map(group => {
|
||||||
@@ -301,7 +373,7 @@ function onLoad() {
|
|||||||
}
|
}
|
||||||
btn.innerText = "获取群成员列表"
|
btn.innerText = "获取群成员列表"
|
||||||
console.log(qContextMenu)
|
console.log(qContextMenu)
|
||||||
qContextMenu.appendChild(btn)
|
// qContextMenu.appendChild(btn)
|
||||||
})
|
})
|
||||||
|
|
||||||
window.LLAPI.on("context-msg-menu", (event, target, msgIds) => {
|
window.LLAPI.on("context-msg-menu", (event, target, msgIds) => {
|
||||||
@@ -326,7 +398,7 @@ function onLoad() {
|
|||||||
// 获得当前聊天窗口
|
// 获得当前聊天窗口
|
||||||
window.LLAPI.getPeer().then(peer => {
|
window.LLAPI.getPeer().then(peer => {
|
||||||
// console.log("current peer", peer)
|
// console.log("current peer", peer)
|
||||||
if (peer && peer.chatType == "group"){
|
if (peer && peer.chatType == "group") {
|
||||||
getGroupMembers(peer.uid, false).then()
|
getGroupMembers(peer.uid, false).then()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -339,7 +411,7 @@ function onLoad() {
|
|||||||
// 传入目标节点和观察选项
|
// 传入目标节点和观察选项
|
||||||
observer.observe(targetNode, config);
|
observer.observe(targetNode, config);
|
||||||
|
|
||||||
}catch (e) {
|
} catch (e) {
|
||||||
window.llonebot.log(e)
|
window.llonebot.log(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -350,41 +422,77 @@ function onLoad() {
|
|||||||
|
|
||||||
// 打开设置界面时触发
|
// 打开设置界面时触发
|
||||||
async function onConfigView(view: any) {
|
async function onConfigView(view: any) {
|
||||||
// 插件本体的路径
|
const {port, hosts} = await window.llonebot.getConfig()
|
||||||
// const plugin_path = (window as any).LiteLoader.plugins.LLOneBot.path;
|
|
||||||
const {port, host} = await window.llonebot.getConfig()
|
function creatHostEleStr(host: string) {
|
||||||
|
let eleStr = `
|
||||||
|
<div class="hostItem vertical-list-item">
|
||||||
|
<h2>事件上报地址(http)</h2>
|
||||||
|
<input class="host" type="text" value="${host}"
|
||||||
|
style="width:60%;padding: 5px"
|
||||||
|
placeholder="不支持localhost,如果是本机请填写局域网ip"/>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
return eleStr
|
||||||
|
}
|
||||||
|
|
||||||
|
let hostsEleStr = ""
|
||||||
|
for (const host of hosts) {
|
||||||
|
hostsEleStr += creatHostEleStr(host);
|
||||||
|
}
|
||||||
const html = `
|
const html = `
|
||||||
<section>
|
<section class="wrap">
|
||||||
<div>
|
<div class="vertical-list-item">
|
||||||
<label>监听端口</label>
|
<h2>监听端口</h2>
|
||||||
<input id="port" type="number" value="${port}"/>
|
<input id="port" type="number" value="${port}"/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label>事件上报地址(http)</label>
|
<button id="addHost" class="q-button">添加上报地址</button>
|
||||||
<input id="host" type="text" value="${host}"/>
|
|
||||||
</div>
|
</div>
|
||||||
<button id="save">保存(重启QQ后生效)</button>
|
<div id="hostItems">
|
||||||
|
${hostsEleStr}
|
||||||
|
</div>
|
||||||
|
<button id="save" class="q-button">保存(监听端口重启QQ后生效)</button>
|
||||||
</section>
|
</section>
|
||||||
`
|
`
|
||||||
|
|
||||||
const parser = new DOMParser();
|
const parser = new DOMParser();
|
||||||
const doc = parser.parseFromString(html, "text/html");
|
const doc = parser.parseFromString(html, "text/html");
|
||||||
|
|
||||||
|
|
||||||
|
function addHostEle(initValue: string = "") {
|
||||||
|
let addressDoc = parser.parseFromString(creatHostEleStr(initValue), "text/html");
|
||||||
|
let addressEle = addressDoc.querySelector("div")
|
||||||
|
let hostItemsEle = document.getElementById("hostItems");
|
||||||
|
hostItemsEle.appendChild(addressEle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
doc.getElementById("addHost").addEventListener("click", () => addHostEle())
|
||||||
|
|
||||||
doc.getElementById("save")?.addEventListener("click",
|
doc.getElementById("save")?.addEventListener("click",
|
||||||
() => {
|
() => {
|
||||||
const portEle: HTMLInputElement = document.getElementById("port") as HTMLInputElement
|
const portEle: HTMLInputElement = document.getElementById("port") as HTMLInputElement
|
||||||
const hostEle: HTMLInputElement = document.getElementById("host") as HTMLInputElement
|
const hostEles: HTMLCollectionOf<HTMLInputElement> = document.getElementsByClassName("host") as HTMLCollectionOf<HTMLInputElement>;
|
||||||
// const port = doc.querySelector("input[type=number]")?.value
|
// const port = doc.querySelector("input[type=number]")?.value
|
||||||
// const host = doc.querySelector("input[type=text]")?.value
|
// const host = doc.querySelector("input[type=text]")?.value
|
||||||
// 获取端口和host
|
// 获取端口和host
|
||||||
const port = portEle.value
|
const port = portEle.value
|
||||||
const host = hostEle.value
|
let hosts: string[] = [];
|
||||||
if (port && host) {
|
for (const hostEle of hostEles) {
|
||||||
window.llonebot.setConfig({
|
if (hostEle.value) {
|
||||||
port: parseInt(port),
|
hosts.push(hostEle.value);
|
||||||
host: host
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
window.llonebot.setConfig({
|
||||||
|
port: parseInt(port),
|
||||||
|
hosts: hosts
|
||||||
|
})
|
||||||
|
alert("保存成功");
|
||||||
})
|
})
|
||||||
doc.querySelectorAll("section").forEach((node) => view.appendChild(node));
|
doc.querySelectorAll("section").forEach((node) => view.appendChild(node));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
Reference in New Issue
Block a user