From 72ca12a347301130b88a47852f0bd0ca7f1c64d3 Mon Sep 17 00:00:00 2001 From: sandtechnology <20417547+sandtechnology@users.noreply.github.com> Date: Wed, 13 Jan 2021 21:45:35 +0800 Subject: [PATCH] Fix long message error again (#858) * Fix #195 again, also fix #539 * Let AtAll consistent with At Co-authored-by: Him188 * Change Member to NormalMember for capable events * Make StrangerRelationChangeEvent sealed * 2.0-RC * Fix dokka * Fix #195 again * Revert Image length to 260 and increase limit to 15000 * Add fallback measure to group message * Remove unnecessary log Co-authored-by: Him188 Co-authored-by: Him188 --- .../commonMain/kotlin/contact/GroupImpl.kt | 20 +++++++++++++++++-- .../src/commonMain/kotlin/contact/util.kt | 4 ++-- .../chat/receive/MessageSvc.PbSendMsg.kt | 13 ++++++++---- .../src/commonMain/kotlin/utils/type.kt | 6 +++--- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt index 24ed7075d..1af75e985 100644 --- a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt @@ -140,7 +140,8 @@ internal class GroupImpl( return MiraiImpl.lowLevelSendGroupLongOrForwardMessage(bot, this.id, message.nodeList, false, message) } - val msg: MessageChain = if (message !is LongMessage && message !is ForwardMessageInternal) { + val isLongOrForward = message is LongMessage || message is ForwardMessageInternal + val msg: MessageChain = if (!isLongOrForward) { val chain = kotlin.runCatching { GroupMessagePreSendEvent(this, message).broadcast() }.onSuccess { @@ -194,7 +195,7 @@ internal class GroupImpl( } } - val result = bot.network.runCatching { + val result = bot.network.runCatching sendMsg@{ val source: OnlineMessageSourceToGroupImpl MessageSvcPbSendMsg.createToGroup( bot.client, @@ -204,6 +205,21 @@ internal class GroupImpl( ) { source = it }.sendAndExpect().let { + if (!isLongOrForward && it is MessageSvcPbSendMsg.Response.MessageTooLarge) { + return@sendMsg MiraiImpl.lowLevelSendGroupLongOrForwardMessage( + bot, + this@GroupImpl.id, + listOf( + ForwardMessage.Node( + senderId = bot.id, + time = currentTimeSeconds().toInt(), + messageChain = msg, + senderName = bot.nick + ) + ), + true, null + ) + } check(it is MessageSvcPbSendMsg.Response.SUCCESS) { "Send group message failed: $it" } diff --git a/mirai-core/src/commonMain/kotlin/contact/util.kt b/mirai-core/src/commonMain/kotlin/contact/util.kt index 3a8e893a8..a583ce536 100644 --- a/mirai-core/src/commonMain/kotlin/contact/util.kt +++ b/mirai-core/src/commonMain/kotlin/contact/util.kt @@ -154,9 +154,9 @@ internal inline fun MessageChain.verityLength( } val chain = this - val length = estimateLength(target, 5001) + val length = estimateLength(target, 15001) lengthCallback(length) - if (length > 5000 || count { it is Image }.apply { imageCntCallback(this) } > 50) { + if (length > 15000 || count { it is Image }.apply { imageCntCallback(this) } > 50) { throw MessageTooLargeException( target, message, this, "message(${ diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbSendMsg.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbSendMsg.kt index 628ed11e7..13dc4af7c 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbSendMsg.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbSendMsg.kt @@ -46,6 +46,10 @@ internal object MessageSvcPbSendMsg : OutgoingPacketFactory Response.SUCCESS + 10 -> Response.MessageTooLarge + else -> Response.Failed( response.result, response.errtype, response.errmsg ) + } } } diff --git a/mirai-core/src/commonMain/kotlin/utils/type.kt b/mirai-core/src/commonMain/kotlin/utils/type.kt index fcec4e708..90b1a4f3f 100644 --- a/mirai-core/src/commonMain/kotlin/utils/type.kt +++ b/mirai-core/src/commonMain/kotlin/utils/type.kt @@ -46,10 +46,10 @@ internal fun MessageChain.estimateLength(target: ContactOrBot, upTo: Int): Int = internal fun SingleMessage.estimateLength(target: ContactOrBot, upTo: Int): Int { return when (this) { is QuoteReply -> 444 + this.source.originalMessage.estimateLength(target, upTo) // Magic number - is Image -> 40 //magic number + is Image -> 260 //Magic number is PlainText -> content.chineseLength(upTo) - is At -> 60 //magic number - is AtAll -> 60 //magic number + is At -> 60 //Magic number + is AtAll -> 60 //Magic number else -> this.toString().chineseLength(upTo) } }