fix: send empty forward msg

fix: ignore post history msg before login
fix: quit group not sync to groups of data
feat: support post url params
feat: support port http heart
This commit is contained in:
linyuchen 2024-04-16 23:15:33 +08:00
parent f8890b309b
commit c2b3316603
15 changed files with 555 additions and 458 deletions

View File

@ -39,6 +39,7 @@ export class ConfigUtil {
enableWs: true,
enableWsReverse: false,
messagePostFormat: "array",
enableHttpHeart: false
}
let defaultConfig: Config = {
ob11: ob11Default,

View File

@ -58,6 +58,15 @@ export async function getGroup(qq: string): Promise<Group | undefined> {
return group
}
export function deleteGroup(groupCode: string) {
const groupIndex = groups.findIndex(group => group.groupCode === groupCode.toString())
// log(groups, groupCode, groupIndex);
if (groupIndex !== -1) {
log("删除群", groupCode);
groups.splice(groupIndex, 1)
}
}
export async function getGroupMember(groupQQ: string | number, memberUinOrUid: string | number) {
groupQQ = groupQQ.toString()
memberUinOrUid = memberUinOrUid.toString()

View File

@ -95,6 +95,9 @@ export abstract class HttpServerBase {
if (method == "get") {
payload = req.query
}
else if (req.query){
payload = {...req.query, ...req.body}
}
log("收到http请求", url, payload);
try {
res.send(await handler(res, payload))

View File

@ -9,6 +9,7 @@ export interface OB11Config {
enableWs?: boolean
enableWsReverse?: boolean
messagePostFormat?: 'array' | 'string'
enableHttpHeart?: boolean
}
export interface CheckVersion {
result: boolean,

View File

@ -34,7 +34,7 @@ import {
GroupNotifyTypes,
RawMessage
} from "../ntqqapi/types";
import {ob11HTTPServer} from "../onebot11/server/http";
import {httpHeart, ob11HTTPServer} from "../onebot11/server/http";
import {OB11FriendRecallNoticeEvent} from "../onebot11/event/notice/OB11FriendRecallNoticeEvent";
import {OB11GroupRecallNoticeEvent} from "../onebot11/event/notice/OB11GroupRecallNoticeEvent";
import {postOB11Event} from "../onebot11/server/postOB11Event";
@ -148,7 +148,10 @@ function onLoad() {
async function postReceiveMsg(msgList: RawMessage[]) {
const {debug, reportSelfMessage} = getConfigUtil().getConfig();
for (let message of msgList) {
// 过滤启动之前的消息
if (parseInt(message.msgTime) < startTime / 1000) {
continue;
}
// log("收到新消息", message.msgId, message.msgSeq)
// if (message.senderUin !== selfInfo.uin){
message.msgShortId = await dbUtil.addMsg(message);
@ -379,7 +382,7 @@ function onLoad() {
})
}
let startTime = 0;
let startTime = 0; // 毫秒
async function start() {
log("llonebot pid", process.pid)
@ -402,6 +405,9 @@ function onLoad() {
if (config.ob11.enableWsReverse) {
ob11ReverseWebsockets.start();
}
if (config.ob11.enableHttpHeart){
httpHeart.start();
}
log("LLOneBot start")
}

View File

@ -1,5 +1,5 @@
import {Config} from "../common/types";
import {ob11HTTPServer} from "../onebot11/server/http";
import {httpHeart, ob11HTTPServer} from "../onebot11/server/http";
import {ob11WebsocketServer} from "../onebot11/server/ws/WebsocketServer";
import {ob11ReverseWebsockets} from "../onebot11/server/ws/ReverseWebsocket";
import {llonebotError} from "../common/data";
@ -54,6 +54,14 @@ export async function setConfig(config: Config) {
}
}
}
if (config.ob11.enableHttpHeart){
// 启动http心跳
httpHeart.start();
}
else{
// 关闭http心跳
httpHeart.stop();
}
log("old config", oldConfig)
log("配置已更新", config)
checkFfmpeg(config.ffmpeg).then()

View File

@ -1,7 +1,7 @@
import {ReceiveCmdS} from "../hook";
import {Group, GroupMember, GroupMemberRole, GroupNotifies, GroupNotify, GroupRequestOperateTypes} from "../types";
import {callNTQQApi, GeneralCallResult, NTQQApiClass, NTQQApiMethod} from "../ntcall";
import {uidMaps} from "../../common/data";
import {deleteGroup, uidMaps} from "../../common/data";
import {dbUtil} from "../../common/db";
import {log} from "../../common/utils/log";
import {NTQQWindowApi, NTQQWindows} from "./window";
@ -102,13 +102,17 @@ export class NTQQGroupApi{
});
}
static async quitGroup(groupQQ: string) {
await callNTQQApi<GeneralCallResult>({
const result = await callNTQQApi<GeneralCallResult>({
methodName: NTQQApiMethod.QUIT_GROUP,
args: [
{"groupCode": groupQQ},
null
]
})
if (result.result === 0){
deleteGroup(groupQQ);
}
return result;
}
static async kickMember(groupQQ: string, kickUids: string[], refuseForever: boolean = false, kickReason: string = '') {
return await callNTQQApi<GeneralCallResult>(

View File

@ -2,7 +2,16 @@ import {BrowserWindow} from 'electron';
import {NTQQApiClass, NTQQApiMethod} from "./ntcall";
import {NTQQMsgApi, sendMessagePool} from "./api/msg"
import {ChatType, Group, GroupMember, GroupMemberRole, RawMessage, User} from "./types";
import {friends, getFriend, getGroupMember, groups, selfInfo, tempGroupCodeMap, uidMaps} from "../common/data";
import {
deleteGroup,
friends,
getFriend,
getGroupMember,
groups,
selfInfo,
tempGroupCodeMap,
uidMaps
} from "../common/data";
import {OB11GroupDecreaseEvent} from "../onebot11/event/notice/OB11GroupDecreaseEvent";
import {v4 as uuidv4} from "uuid"
import {postOB11Event} from "../onebot11/server/postOB11Event";
@ -233,6 +242,11 @@ let activatedGroups: string[] = [];
async function updateGroups(_groups: Group[], needUpdate: boolean = true) {
for (let group of _groups) {
log("update group", group);
if (group.privilegeFlag === 0){
deleteGroup(group.groupCode);
continue;
}
log("update group", group)
// if (!activatedGroups.includes(group.groupCode)) {
NTQQMsgApi.activateChat({peerUid: group.groupCode, chatType: ChatType.group}).then((r) => {
@ -294,7 +308,9 @@ async function processGroupEvent(payload: { groupList: Group[] }) {
}
}
}
if (group.privilegeFlag === 0){
deleteGroup(group.groupCode);
}
}
}

View File

@ -6,15 +6,25 @@ import {ActionName} from "../types";
import {NTQQGroupApi} from "../../../ntqqapi/api";
import {log} from "../../../common/utils";
interface Payload {
no_cache: boolean
}
class GetGroupList extends BaseAction<null, OB11Group[]> {
class GetGroupList extends BaseAction<Payload, OB11Group[]> {
actionName = ActionName.GetGroupList
protected async _handle(payload: null) {
// if (groups.length === 0) {
// const groups = await NTQQGroupApi.getGroups(true)
protected async _handle(payload: Payload) {
if (groups.length === 0
// || payload.no_cache === true
) {
try {
const groups = await NTQQGroupApi.getGroups(true)
// log("get groups", groups)
// }
return OB11Constructor.groups(groups);
} catch (e) {
}
}
return OB11Constructor.groups(groups);
}
}

View File

@ -91,6 +91,8 @@ function initActionMap() {
const actionMap = new Map<string, BaseAction<any, any>>();
for (const action of actionHandlers) {
actionMap.set(action.actionName, action);
actionMap.set(action.actionName + '_async', action);
actionMap.set(action.actionName + '_rate_limited', action);
}
return actionMap

View File

@ -74,15 +74,15 @@ export interface ReturnDataType {
export function convertMessage2List(message: OB11MessageMixType, autoEscape = false) {
if (typeof message === "string") {
if (!autoEscape) {
message = decodeCQCode(message.toString())
} else {
if (autoEscape === true) {
message = [{
type: OB11MessageDataType.text,
data: {
text: message
}
}]
} else {
message = decodeCQCode(message.toString())
}
} else if (!Array.isArray(message)) {
message = [message]
@ -329,7 +329,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} else {
throw ("发送消息参数错误, 请指定group_id或user_id")
}
const messages = convertMessage2List(payload.message, !!payload.auto_escape);
const messages = convertMessage2List(payload.message, payload.auto_escape);
if (this.getSpecialMsgNum(payload, OB11MessageDataType.node)) {
try {
const returnMsg = await this.handleForwardNode(peer, messages as OB11MessageNode[], group)
@ -508,6 +508,9 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
// nodeIds.push(nodeMsg.msgId)
// await sleep(500);
// 开发转发
if (nodeMsgIds.length === 0) {
throw Error("转发消息失败,节点为空")
}
try {
log("开发转发", nodeMsgIds)
return await NTQQMsgApi.multiForwardMsg(srcPeer, destPeer, nodeMsgIds)

View File

@ -22,7 +22,7 @@ import {
User,
VideoElement
} from '../ntqqapi/types';
import {getFriend, getGroupMember, selfInfo, tempGroupCodeMap} from '../common/data';
import {deleteGroup, getFriend, getGroupMember, groups, selfInfo, tempGroupCodeMap} from '../common/data';
import {EventType} from "./event/OB11BaseEvent";
import {encodeCQCode} from "./cqcode";
import {dbUtil} from "../common/db";
@ -39,6 +39,7 @@ import {getConfigUtil} from "../common/config";
import {OB11GroupTitleEvent} from "./event/notice/OB11GroupTitleEvent";
import {OB11GroupCardEvent} from "./event/notice/OB11GroupCardEvent";
import {OB11GroupDecreaseEvent} from "./event/notice/OB11GroupDecreaseEvent";
import {NTQQGroupApi} from "../ntqqapi/api";
let lastRKeyUpdateTime = 0;
@ -158,8 +159,7 @@ export class OB11Constructor {
// }
// }
message_data["data"]["url"] = IMAGE_HTTP_HOST + url
}
else{
} else {
// 有可能会碰到appid为1406的这个不能使用新的NT域名并且需要把appid改为1407才可访问
message_data["data"]["url"] = `${IMAGE_HTTP_HOST}/download?appid=1407&fileid=${fileUuid}&rkey=${currentRKey}&spec=0`
}
@ -231,12 +231,10 @@ export class OB11Constructor {
if (faceId === FaceIndex.dice) {
message_data["type"] = OB11MessageDataType.dice
message_data["data"]["result"] = element.faceElement.resultId;
}
else if (faceId === FaceIndex.RPS){
} else if (faceId === FaceIndex.RPS) {
message_data["type"] = OB11MessageDataType.RPS
message_data["data"]["result"] = element.faceElement.resultId;
}
else{
} else {
message_data["type"] = OB11MessageDataType.face;
message_data["data"]["id"] = element.faceElement.faceIndex.toString();
}
@ -317,13 +315,18 @@ export class OB11Constructor {
if (memberUin && adminUin) {
return new OB11GroupBanEvent(parseInt(msg.peerUid), parseInt(memberUin), parseInt(adminUin), duration, sub_type);
}
}
else if (groupElement.type == TipGroupElementType.kicked){
log("收到我被踢出提示", groupElement)
} else if (groupElement.type == TipGroupElementType.kicked) {
log(`收到我被踢出或退群提示, 群${msg.peerUid}`, groupElement)
deleteGroup(msg.peerUid);
NTQQGroupApi.quitGroup(msg.peerUid).then()
try {
const adminUin = (await getGroupMember(msg.peerUid, groupElement.adminUid))?.uin || (await NTQQUserApi.getUserDetailInfo(groupElement.adminUid))?.uin
if (adminUin) {
return new OB11GroupDecreaseEvent(parseInt(msg.peerUid), parseInt(selfInfo.uin), parseInt(adminUin), "kick_me");
}
} catch (e) {
return new OB11GroupDecreaseEvent(parseInt(msg.peerUid), parseInt(selfInfo.uin), 0, "leave");
}
}
} else if (element.fileElement) {
return new OB11GroupUploadNoticeEvent(parseInt(msg.peerUid), parseInt(msg.senderUin), {

View File

@ -1,8 +1,11 @@
import {Response} from "express";
import {OB11Response} from "../action/OB11Response";
import {HttpServerBase} from "../../common/server/http";
import {actionHandlers} from "../action";
import {actionHandlers, actionMap} from "../action";
import {getConfigUtil} from "../../common/config";
import {postOB11Event} from "./postOB11Event";
import {OB11HeartbeatEvent} from "../event/meta/OB11HeartbeatEvent";
import {selfInfo} from "../../common/data";
class OB11HTTPServer extends HttpServerBase {
name = "OneBot V11 server"
@ -21,9 +24,32 @@ class OB11HTTPServer extends HttpServerBase {
export const ob11HTTPServer = new OB11HTTPServer();
setTimeout(() => {
for (const action of actionHandlers) {
for (const [actionName, action] of actionMap) {
for (const method of ["post", "get"]) {
ob11HTTPServer.registerRouter(method, action.actionName, (res, payload) => action.handle(payload))
ob11HTTPServer.registerRouter(method, actionName, (res, payload) => action.handle(payload))
}
}
}, 0)
class HTTPHeart{
intervalId: NodeJS.Timeout | null = null
start(){
const {heartInterval} = getConfigUtil().getConfig();
if (this.intervalId) {
clearInterval(this.intervalId);
}
this.intervalId = setInterval(() => {
// ws的心跳是ws自己维护的
postOB11Event(new OB11HeartbeatEvent(selfInfo.online, true, heartInterval), false, false)
}, heartInterval)
}
stop(){
if (this.intervalId){
clearInterval(this.intervalId)
}
}
}
export const httpHeart = new HTTPHeart();

View File

@ -69,7 +69,7 @@ export function postWsEvent(event: PostEventType) {
}
}
export function postOB11Event(msg: PostEventType, reportSelf = false) {
export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = true) {
const config = getConfigUtil().getConfig();
// 判断msg是否是event
if (!config.reportSelfMessage && !reportSelf) {
@ -172,5 +172,7 @@ export function postOB11Event(msg: PostEventType, reportSelf = false) {
});
}
}
if (postWs){
postWsEvent(msg);
}
}

View File

@ -68,6 +68,9 @@ async function onSettingWindowCreated(view: Element) {
`<div class="q-input"><input class="q-input__inner" data-config-key="ob11.httpPort" type="number" min="1" max="65534" value="${config.ob11.httpPort}" placeholder="${config.ob11.httpPort}" /></div>`,
'config-ob11-httpPort', config.ob11.enableHttp
),
SettingItem('启用 HTTP 心跳', null,
SettingSwitch('ob11.enableHttpHeart', config.ob11.enableHttpHeart, {'control-display-id': 'config-ob11-enableHttpHeart'}),
),
SettingItem('启用 HTTP 事件上报', null,
SettingSwitch('ob11.enableHttpPost', config.ob11.enableHttpPost, {'control-display-id': 'config-ob11-httpHosts'}),
),