feat: QuickHandle From LLOB

This commit is contained in:
手瓜一十雪 2024-05-18 14:35:24 +08:00
parent 076c9cfed7
commit b3c6e2a0f3
4 changed files with 92 additions and 79 deletions

View File

@ -0,0 +1,17 @@
import { log } from '@/common/utils/log';
import BaseAction from '../BaseAction'
import { ActionName } from '../types'
import { QuickAction, QuickActionEvent, handleQuickOperation } from '@/onebot11/server/postOB11Event';
interface Payload{
context: QuickActionEvent,
operation: QuickAction
}
export class GoCQHTTHandleQuickOperation extends BaseAction<Payload, null>{
actionName = ActionName.GoCQHTTP_HandleQuickOperation
protected async _handle(payload: Payload): Promise<null> {
handleQuickOperation(payload.context, payload.operation).then().catch(log);
return null
}
}

View File

@ -42,13 +42,13 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de
try { try {
for (const fileElement of sendElements) { for (const fileElement of sendElements) {
if (fileElement.elementType === ElementType.PTT) { if (fileElement.elementType === ElementType.PTT) {
totalSize += fs.statSync(fileElement.pttElement.filePath).size totalSize += fs.statSync(fileElement.pttElement.filePath).size;
} }
if (fileElement.elementType === ElementType.FILE) { if (fileElement.elementType === ElementType.FILE) {
totalSize += fs.statSync(fileElement.fileElement.filePath).size totalSize += fs.statSync(fileElement.fileElement.filePath).size;
} }
if (fileElement.elementType === ElementType.VIDEO) { if (fileElement.elementType === ElementType.VIDEO) {
totalSize += fs.statSync(fileElement.videoElement.filePath).size totalSize += fs.statSync(fileElement.videoElement.filePath).size;
} }
if (fileElement.elementType === ElementType.PIC) { if (fileElement.elementType === ElementType.PIC) {
totalSize += fs.statSync(fileElement.picElement.sourcePath).size totalSize += fs.statSync(fileElement.picElement.sourcePath).size
@ -57,7 +57,7 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de
//且 PredictTime ((totalSize / 1024 / 512) * 1000)不等于Nan //且 PredictTime ((totalSize / 1024 / 512) * 1000)不等于Nan
let PredictTime = totalSize / 1024 / 512 * 1000; let PredictTime = totalSize / 1024 / 512 * 1000;
if (!Number.isNaN(PredictTime)) { if (!Number.isNaN(PredictTime)) {
timeout += PredictTime// 10S Basic Timeout + PredictTime( For File 512kb/s ) timeout += PredictTime// 5S Basic Timeout + PredictTime( For File 512kb/s )
} }
} catch (e) { } catch (e) {
logError("发送消息计算预计时间异常", e); logError("发送消息计算预计时间异常", e);

View File

@ -60,6 +60,7 @@ export enum ActionName {
CleanCache = 'clean_cache', CleanCache = 'clean_cache',
GetCookies = 'get_cookies', GetCookies = 'get_cookies',
// 以下为go-cqhttp api // 以下为go-cqhttp api
GoCQHTTP_HandleQuickOperation = ".handle_quick_operation",
GetGroupHonorInfo = 'get_group_honor_info', GetGroupHonorInfo = 'get_group_honor_info',
GoCQHTTP_GetEssenceMsg = 'get_essence_msg_list', GoCQHTTP_GetEssenceMsg = 'get_essence_msg_list',
GoCQHTTP_SendGroupNotice = '_send_group_notice', GoCQHTTP_SendGroupNotice = '_send_group_notice',

View File

@ -16,7 +16,7 @@ import { friendRequests, getGroup, getUidByUin, groupNotifies, selfInfo } from '
import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi } from '@/core/apis'; import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi } from '@/core/apis';
import createSendElements from '../action/msg/SendMsg/create-send-elements'; import createSendElements from '../action/msg/SendMsg/create-send-elements';
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent export type QuickActionEvent = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
interface QuickActionPrivateMessage { interface QuickActionPrivateMessage {
reply?: string; reply?: string;
@ -43,7 +43,7 @@ interface QuickActionGroupRequest {
reason?: string; reason?: string;
} }
type QuickAction = export type QuickAction =
QuickActionPrivateMessage QuickActionPrivateMessage
& QuickActionGroupMessage & QuickActionGroupMessage
& QuickActionFriendRequest & QuickActionFriendRequest
@ -62,7 +62,7 @@ export function unregisterWsEventSender(ws: WebSocketClass) {
} }
} }
export function postWsEvent(event: PostEventType) { export function postWsEvent(event: QuickActionEvent) {
for (const ws of eventWSList) { for (const ws of eventWSList) {
new Promise(() => { new Promise(() => {
wsReply(ws, event); wsReply(ws, event);
@ -70,7 +70,7 @@ export function postWsEvent(event: PostEventType) {
} }
} }
export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = true) { export function postOB11Event(msg: QuickActionEvent, reportSelf = false, postWs = true) {
const config = ob11Config; const config = ob11Config;
// 判断msg是否是event // 判断msg是否是event
@ -108,11 +108,24 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = t
return; return;
} }
try { try {
if (msg.post_type === 'message') { handleQuickOperation(msg as QuickActionEvent, resJson).then().catch(logError);
} catch (e: any) {
logError('新消息事件HTTP上报返回快速操作失败', e);
}
}, (err: any) => {
logError(`新消息事件HTTP上报失败: ${host} `, err, msg);
});
}
}
if (postWs) {
postWsEvent(msg);
}
}
async function handleMsg(msg: OB11Message, quickAction: QuickAction) {
msg = msg as OB11Message; msg = msg as OB11Message;
const rawMessage = await dbUtil.getMsgByShortId(msg.message_id); const rawMessage = await dbUtil.getMsgByShortId(msg.message_id);
resJson = resJson as QuickActionPrivateMessage | QuickActionGroupMessage; const reply = quickAction.reply;
const reply = resJson.reply;
const peer: Peer = { const peer: Peer = {
chatType: ChatType.friend, chatType: ChatType.friend,
peerUid: getUidByUin(msg.user_id.toString()) as string peerUid: getUidByUin(msg.user_id.toString()) as string
@ -131,7 +144,7 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = t
if (msg.message_type == 'group') { if (msg.message_type == 'group') {
group = await getGroup(msg.group_id!.toString()); group = await getGroup(msg.group_id!.toString());
if ((resJson as QuickActionGroupMessage).at_sender) { if ((quickAction as QuickActionGroupMessage).at_sender) {
replyMessage.push({ replyMessage.push({
type: 'at', type: 'at',
data: { data: {
@ -140,55 +153,37 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = t
} as OB11MessageAt); } as OB11MessageAt);
} }
} }
replyMessage = replyMessage.concat(normalize(reply, resJson.auto_escape)); replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape));
const { sendElements, deleteAfterSentFiles } = await createSendElements(replyMessage, group); const { sendElements, deleteAfterSentFiles } = await createSendElements(replyMessage, group);
sendMsg(peer, sendElements, deleteAfterSentFiles, false).then().catch(logError); sendMsg(peer, sendElements, deleteAfterSentFiles, false).then().catch(logError);
} else if (resJson.delete) { }
NTQQMsgApi.recallMsg(peer, [rawMessage!.msgId]).then().catch(logError); }
} else if (resJson.kick) { async function handleGroupRequest(request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
NTQQGroupApi.kickMember(peer.peerUid, [rawMessage!.senderUid]).then().catch(logError); if (!isNull(quickAction.approve)) {
} else if (resJson.ban) { NTQQGroupApi.handleGroupRequest(
NTQQGroupApi.banMember(peer.peerUid, [{ groupNotifies[request.flag],
uid: rawMessage!.senderUid, quickAction.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject,
timeStamp: resJson.ban_duration || 60 * 30 quickAction.reason,
}],).then().catch(logError); ).then().catch(logError)
} }
}
} else if (msg.post_type === 'request') { async function handleFriendRequest(request: OB11FriendRequestEvent, quickAction: QuickActionFriendRequest) {
if ((msg as OB11FriendRequestEvent).request_type === 'friend') { if (!isNull(quickAction.approve)) {
resJson = resJson as QuickActionFriendRequest; NTQQFriendApi.handleFriendRequest(friendRequests[request.flag], !!quickAction.approve).then().catch(logError)
if (!isNull(resJson.approve)) { }
// todo: set remark }
const flag = (msg as OB11FriendRequestEvent).flag; export async function handleQuickOperation(context: QuickActionEvent, quickAction: QuickAction) {
// const [friendUid, seq] = flag.split('|'); if (context.post_type === 'message') {
const request = friendRequests[flag]; handleMsg(context as OB11Message, quickAction).then().catch(logError)
NTQQFriendApi.handleFriendRequest( }
request, if (context.post_type === 'request') {
!!resJson.approve, const friendRequest = context as OB11FriendRequestEvent;
).then().catch(logError); const groupRequest = context as OB11GroupRequestEvent;
} if ((friendRequest).request_type === 'friend') {
} else if ((msg as OB11GroupRequestEvent).request_type === 'group') { handleFriendRequest(friendRequest, quickAction).then().catch(logError)
resJson = resJson as QuickActionGroupRequest; }
if (!isNull(resJson.approve)) { else if (groupRequest.request_type === 'group') {
const flag = (msg as OB11GroupRequestEvent).flag; handleGroupRequest(groupRequest, quickAction).then().catch(logError)
const request = groupNotifies[flag]; }
// const [groupCode, seq] = flag.split('|');
NTQQGroupApi.handleGroupRequest(request,
resJson.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject
).then().catch(logError);
}
}
}
} catch (e: any) {
logError('新消息事件HTTP上报返回快速操作失败', e);
}
}, (err: any) => {
logError(`新消息事件HTTP上报失败: ${host} `, err, msg);
});
}
}
if (postWs) {
postWsEvent(msg);
} }
} }