From 076c9cfed782559ba6269c1e9e4c56cbfdad41c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sat, 18 May 2024 14:09:57 +0800 Subject: [PATCH] feat: send timeout predict --- src/onebot11/action/msg/SendMsg/index.ts | 29 ++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/onebot11/action/msg/SendMsg/index.ts b/src/onebot11/action/msg/SendMsg/index.ts index 465d0857..2a664bed 100644 --- a/src/onebot11/action/msg/SendMsg/index.ts +++ b/src/onebot11/action/msg/SendMsg/index.ts @@ -9,9 +9,9 @@ import { import { ActionName, BaseCheckResult } from '@/onebot11/action/types'; import { getFriend, getGroup, getUidByUin } from '@/core/data'; import { dbUtil } from '@/core/utils/db'; -import { ChatType, Group, NTQQMsgApi, Peer, SendMessageElement, } from '@/core'; +import { ChatType, ElementType, Group, NTQQMsgApi, Peer, SendMessageElement, } from '@/core'; import fs from 'node:fs'; -import { logDebug } from '@/common/utils/log'; +import { logDebug, logError } from '@/common/utils/log'; import { decodeCQCode } from '@/onebot11/cqcode'; import createSendElements from './create-send-elements'; import { handleForwardNode } from '@/onebot11/action/msg/SendMsg/handle-forward-node'; @@ -37,6 +37,31 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de if (!sendElements.length) { throw ('消息体无法解析, 请检查是否发送了不支持的消息类型'); } + let totalSize = 0; + let timeout = 5000; + try { + for (const fileElement of sendElements) { + if (fileElement.elementType === ElementType.PTT) { + totalSize += fs.statSync(fileElement.pttElement.filePath).size + } + if (fileElement.elementType === ElementType.FILE) { + totalSize += fs.statSync(fileElement.fileElement.filePath).size + } + if (fileElement.elementType === ElementType.VIDEO) { + totalSize += fs.statSync(fileElement.videoElement.filePath).size + } + if (fileElement.elementType === ElementType.PIC) { + totalSize += fs.statSync(fileElement.picElement.sourcePath).size + } + } + //且 PredictTime ((totalSize / 1024 / 512) * 1000)不等于Nan + let PredictTime = totalSize / 1024 / 512 * 1000; + if (!Number.isNaN(PredictTime)) { + timeout += PredictTime// 10S Basic Timeout + PredictTime( For File 512kb/s ) + } + } catch (e) { + logError("发送消息计算预计时间异常", e); + } const returnMsg = await NTQQMsgApi.sendMsg(peer, sendElements, waitComplete, 20000); try { returnMsg.id = await dbUtil.addMsg(returnMsg, false);