Fix long message error again (#858)

* Fix #195 again, also fix #539

* Let AtAll consistent with At

Co-authored-by: Him188 <Him188@mamoe.net>

* 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 <Him188@mamoe.net>

Co-authored-by: Him188 <Him188@mamoe.net>
This commit is contained in:
sandtechnology 2021-01-13 21:45:35 +08:00 committed by GitHub
parent 837bae41fc
commit 72ca12a347
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 11 deletions

View File

@ -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<MessageSvcPbSendMsg.Response>().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"
}

View File

@ -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(${

View File

@ -46,6 +46,10 @@ internal object MessageSvcPbSendMsg : OutgoingPacketFactory<MessageSvcPbSendMsg.
override fun toString(): String = "MessageSvcPbSendMsg.Response.SUCCESS"
}
object MessageTooLarge : Response() {
override fun toString(): String = "MessageSvcPbSendMsg.Response.MessageTooLarge"
}
/**
* 121: 被限制? 个别号才不能发
*/
@ -338,14 +342,15 @@ internal object MessageSvcPbSendMsg : OutgoingPacketFactory<MessageSvcPbSendMsg.
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
val response = readProtoBuf(MsgSvc.PbSendMsgResp.serializer())
return if (response.result == 0) {
Response.SUCCESS
} else {
Response.Failed(
return when (response.result) {
0 -> Response.SUCCESS
10 -> Response.MessageTooLarge
else -> Response.Failed(
response.result,
response.errtype,
response.errmsg
)
}
}
}

View File

@ -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)
}
}