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 @LowLevelAPI
@MiraiExperimentalAPI @MiraiExperimentalAPI
override suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: Message): MessageReceipt<Group> { internal suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: MessageChain): 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" }
val group = getGroup(groupCode) val group = getGroup(groupCode)
val time = currentTimeSeconds val time = currentTimeSeconds
network.run { network.run {
val data = chain.calculateValidationDataForGroup( val data = message.calculateValidationDataForGroup(
sequenceId = client.atomicNextMessageSequenceId(), sequenceId = client.atomicNextMessageSequenceId(),
time = time.toInt(), time = time.toInt(),
random = Random.nextInt().absoluteValue.toUInt(), random = Random.nextInt().absoluteValue.toUInt(),
@ -433,10 +431,15 @@ internal abstract class QQAndroidBotBase constructor(
return group.sendMessage( return group.sendMessage(
RichMessage.longMessage( RichMessage.longMessage(
brief = chain.toString().let { // already cached brief = message.joinToString(limit = 27){
if (it.length > 27) { when(it){
it.take(27) + "..." is PlainText -> it.stringValue
} else it is At -> it.display
is AtAll -> it.display
is Image -> "[图片]"
is Face -> "[表情]"
else -> ""
}
}, },
resId = resId, resId = resId,
timeSeconds = time 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.TroopManagement
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.ImgStore 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.network.protocol.packet.chat.receive.MessageSvc
import net.mamoe.mirai.qqandroid.utils.chineseLength import net.mamoe.mirai.qqandroid.utils.estimateLength
import net.mamoe.mirai.qqandroid.utils.sumUpTo
import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import kotlin.contracts.ExperimentalContracts import kotlin.contracts.ExperimentalContracts
@ -277,30 +276,30 @@ internal class GroupImpl(
@JvmSynthetic @JvmSynthetic
override suspend fun sendMessage(message: Message): MessageReceipt<Group> { override suspend fun sendMessage(message: Message): MessageReceipt<Group> {
check(!isBotMuted) { "bot is muted. Remaining seconds=$botMuteRemaining" } check(!isBotMuted) { "bot is muted. Remaining seconds=$botMuteRemaining" }
val event = GroupMessageSendEvent(this, message.asMessageChain()).broadcast()
if (event.isCancelled) { val msg: MessageChain
throw EventCancelledException("cancelled by GroupMessageSendEvent")
}
if (message !is LongMessage) { if (message !is LongMessage) {
if (event.message.sumUpTo(800) { it, upTo -> val event = GroupMessageSendEvent(this, message.asMessageChain()).broadcast()
when (it) { if (event.isCancelled) {
is QuoteReply -> 700 throw EventCancelledException("cancelled by GroupMessageSendEvent")
is Image -> 300
is PlainText -> it.stringValue.chineseLength(upTo)
else -> it.toString().length
}
} >= 800) {
return bot._lowLevelSendLongGroupMessage(this.id, message)
} }
}
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 lateinit var source: MessageSourceFromSendGroup
bot.network.run { bot.network.run {
val response: MessageSvc.PbSendMsg.Response = MessageSvc.PbSendMsg.ToGroup( val response: MessageSvc.PbSendMsg.Response = MessageSvc.PbSendMsg.ToGroup(
bot.client, bot.client,
id, id,
event.message msg
) { ) {
source = it source = it
source.startWaitingSequenceId(this) source.startWaitingSequenceId(this)

View File

@ -12,6 +12,10 @@
package net.mamoe.mirai.qqandroid.utils 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.JvmMultifileClass
import kotlin.jvm.JvmName 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 } 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 { internal inline fun <T> Iterable<T>.sumUpTo(upTo: Int, selector: (T, remaining: Int) -> Int): Int {
var sum = 0 var sum = 0
for (element in this) { 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.Group
import net.mamoe.mirai.contact.QQ import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.data.* 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.message.data.MessageSource
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
import net.mamoe.mirai.utils.MiraiInternalAPI import net.mamoe.mirai.utils.MiraiInternalAPI
@ -142,14 +140,6 @@ interface LowLevelBotAPIAccessor {
@LowLevelAPI @LowLevelAPI
@MiraiExperimentalAPI @MiraiExperimentalAPI
suspend fun _lowLevelGetGroupActiveData(groupId: Long): GroupActiveData suspend fun _lowLevelGetGroupActiveData(groupId: Long): GroupActiveData
/**
* 发送长消息
*/
@SinceMirai("0.31.0")
@LowLevelAPI
@MiraiExperimentalAPI
suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: Message): MessageReceipt<Group>
} }
/** /**