Remove _lowLevelSendLongGroupMessage, fix length counting

This commit is contained in:
Him188 2020-03-30 16:01:30 +08:00
parent 9f8d75f895
commit 8df45b4072
4 changed files with 41 additions and 35 deletions

View File

@ -371,15 +371,13 @@ internal abstract class QQAndroidBotBase constructor(
@LowLevelAPI
@MiraiExperimentalAPI
override suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: Message): MessageReceipt<Group> {
val chain = message.asMessageChain()
check(chain.toString().length <= 4500 && chain.count { it is Image } <= 50) { "message is too large. Allow up to 4500 chars or 50 images" }
internal suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: MessageChain): MessageReceipt<Group> {
val group = getGroup(groupCode)
val time = currentTimeSeconds
network.run {
val data = chain.calculateValidationDataForGroup(
val data = message.calculateValidationDataForGroup(
sequenceId = client.atomicNextMessageSequenceId(),
time = time.toInt(),
random = Random.nextInt().absoluteValue.toUInt(),
@ -433,10 +431,15 @@ internal abstract class QQAndroidBotBase constructor(
return group.sendMessage(
RichMessage.longMessage(
brief = chain.toString().let { // already cached
if (it.length > 27) {
it.take(27) + "..."
} else it
brief = message.joinToString(limit = 27){
when(it){
is PlainText -> it.stringValue
is At -> it.display
is AtAll -> it.display
is Image -> "[图片]"
is Face -> "[表情]"
else -> ""
}
},
resId = resId,
timeSeconds = time

View File

@ -30,8 +30,7 @@ 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.chineseLength
import net.mamoe.mirai.qqandroid.utils.sumUpTo
import net.mamoe.mirai.qqandroid.utils.estimateLength
import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString
import net.mamoe.mirai.utils.*
import kotlin.contracts.ExperimentalContracts
@ -277,30 +276,30 @@ internal class GroupImpl(
@JvmSynthetic
override suspend fun sendMessage(message: Message): MessageReceipt<Group> {
check(!isBotMuted) { "bot is muted. Remaining seconds=$botMuteRemaining" }
val event = GroupMessageSendEvent(this, message.asMessageChain()).broadcast()
if (event.isCancelled) {
throw EventCancelledException("cancelled by GroupMessageSendEvent")
}
val msg: MessageChain
if (message !is LongMessage) {
if (event.message.sumUpTo(800) { it, upTo ->
when (it) {
is QuoteReply -> 700
is Image -> 300
is PlainText -> it.stringValue.chineseLength(upTo)
else -> it.toString().length
}
} >= 800) {
return bot._lowLevelSendLongGroupMessage(this.id, message)
val event = GroupMessageSendEvent(this, message.asMessageChain()).broadcast()
if (event.isCancelled) {
throw EventCancelledException("cancelled by GroupMessageSendEvent")
}
}
val length = event.message.estimateLength(4501)
check(length <= 4500 && event.message.count { it is Image } <= 50) { "message is too large. Allow up to 4500 chars or 50 images" }
if (length >= 800) {
return bot._lowLevelSendLongGroupMessage(this.id, event.message)
}
msg = event.message
} else msg = message.asMessageChain()
lateinit var source: MessageSourceFromSendGroup
bot.network.run {
val response: MessageSvc.PbSendMsg.Response = MessageSvc.PbSendMsg.ToGroup(
bot.client,
id,
event.message
msg
) {
source = it
source.startWaitingSequenceId(this)

View File

@ -12,6 +12,10 @@
package net.mamoe.mirai.qqandroid.utils
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.message.data.QuoteReply
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
@ -34,6 +38,16 @@ internal fun String.chineseLength(upTo: Int): Int {
return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 3 else 1 }
}
internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int =
sumUpTo(upTo) { it, up ->
when (it) {
is QuoteReply -> 700
is Image -> 300
is PlainText -> it.stringValue.chineseLength(up)
else -> it.toString().chineseLength(up)
}
}
internal inline fun <T> Iterable<T>.sumUpTo(upTo: Int, selector: (T, remaining: Int) -> Int): Int {
var sum = 0
for (element in this) {

View File

@ -13,8 +13,6 @@ import kotlinx.coroutines.Job
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.data.*
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import net.mamoe.mirai.utils.MiraiInternalAPI
@ -142,14 +140,6 @@ interface LowLevelBotAPIAccessor {
@LowLevelAPI
@MiraiExperimentalAPI
suspend fun _lowLevelGetGroupActiveData(groupId: Long): GroupActiveData
/**
* 发送长消息
*/
@SinceMirai("0.31.0")
@LowLevelAPI
@MiraiExperimentalAPI
suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: Message): MessageReceipt<Group>
}
/**