mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-09 01:30:17 +08:00
Fix message length estimate, close #195
This commit is contained in:
parent
f74e5d87ba
commit
220e6d42f5
@ -402,7 +402,12 @@ internal abstract class QQAndroidBotBase constructor(
|
||||
val resId: String
|
||||
when (response) {
|
||||
is MultiMsg.ApplyUp.Response.MessageTooLarge ->
|
||||
error("message is too large")
|
||||
error(
|
||||
"Internal error: message is too large, but this should be handled before sending. Message content:" +
|
||||
message.joinToString {
|
||||
"${it::class.simpleName}(l=${it.toString().length})"
|
||||
}
|
||||
)
|
||||
is MultiMsg.ApplyUp.Response.RequireUpload -> {
|
||||
resId = response.proto.msgResid
|
||||
|
||||
|
@ -30,7 +30,6 @@ import net.mamoe.mirai.qqandroid.network.highway.HighwayHelper
|
||||
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.TroopManagement
|
||||
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.ImgStore
|
||||
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc
|
||||
import net.mamoe.mirai.qqandroid.utils.estimateLength
|
||||
import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString
|
||||
import net.mamoe.mirai.utils.*
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
@ -285,7 +284,7 @@ internal class GroupImpl(
|
||||
throw EventCancelledException("cancelled by GroupMessageSendEvent")
|
||||
}
|
||||
|
||||
val length = event.message.estimateLength(5001)
|
||||
val length = event.message.toString().length
|
||||
if (!(length <= 5000 && event.message.count { it is Image } <= 50)) {
|
||||
throw MessageTooLargeException(
|
||||
this,
|
||||
@ -294,10 +293,17 @@ internal class GroupImpl(
|
||||
"message(${event.message.joinToString(
|
||||
"",
|
||||
limit = 10
|
||||
)}) is too large. Allow up to 5000 in weight (Chinese char=4, English char=1, Quote=700, Image=800, others are estimated in String length.)"
|
||||
)}) is too large. Allow up to 50 images or 5000 chars"
|
||||
)
|
||||
}
|
||||
if (length >= 800) {
|
||||
|
||||
val imageCount = event.message.count { it is Image }
|
||||
|
||||
if (length >= 800
|
||||
|| imageCount >= 4
|
||||
|| (event.message.any<QuoteReply>()
|
||||
&& (imageCount != 0 || length > 100))
|
||||
) {
|
||||
return bot.lowLevelSendLongGroupMessage(this.id, event.message)
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ internal fun MessageChain.calculateValidationDataForGroup(
|
||||
groupCode: Long,
|
||||
botMemberNameCard: String
|
||||
): MessageValidationData {
|
||||
val richTextElems = this.toRichTextElems(true, false)
|
||||
val richTextElems = this.toRichTextElems(forGroup = true, withGeneralFlags = false)
|
||||
|
||||
val msgTransmit = MsgTransmit.PbMultiMsgTransmit(
|
||||
msg = listOf(
|
||||
|
@ -33,7 +33,7 @@ internal fun Int.toIpV4AddressString(): String {
|
||||
}
|
||||
|
||||
internal fun String.chineseLength(upTo: Int): Int {
|
||||
return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 4 else 1 }
|
||||
return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 3 else 1 }
|
||||
}
|
||||
|
||||
internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int =
|
||||
@ -47,7 +47,7 @@ internal fun SingleMessage.estimateLength(upTo: Int = Int.MAX_VALUE): Int {
|
||||
is QuoteReply -> {
|
||||
700 + source.originalMessage.estimateLength(upTo)
|
||||
}
|
||||
is Image -> 300
|
||||
// is Image -> 300
|
||||
is PlainText -> stringValue.chineseLength(upTo)
|
||||
is At -> display.chineseLength(upTo)
|
||||
is AtAll -> display.chineseLength(upTo)
|
||||
|
Loading…
Reference in New Issue
Block a user