mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-23 14:20:24 +08:00
Support long message in general sendMessage
This commit is contained in:
parent
e6c55e0955
commit
a6079f3eaf
@ -31,6 +31,7 @@ import net.mamoe.mirai.contact.*
|
|||||||
import net.mamoe.mirai.data.*
|
import net.mamoe.mirai.data.*
|
||||||
import net.mamoe.mirai.event.broadcast
|
import net.mamoe.mirai.event.broadcast
|
||||||
import net.mamoe.mirai.event.events.MessageRecallEvent
|
import net.mamoe.mirai.event.events.MessageRecallEvent
|
||||||
|
import net.mamoe.mirai.message.MessageReceipt
|
||||||
import net.mamoe.mirai.message.data.*
|
import net.mamoe.mirai.message.data.*
|
||||||
import net.mamoe.mirai.qqandroid.contact.MemberInfoImpl
|
import net.mamoe.mirai.qqandroid.contact.MemberInfoImpl
|
||||||
import net.mamoe.mirai.qqandroid.contact.QQImpl
|
import net.mamoe.mirai.qqandroid.contact.QQImpl
|
||||||
@ -370,13 +371,11 @@ internal abstract class QQAndroidBotBase constructor(
|
|||||||
|
|
||||||
@LowLevelAPI
|
@LowLevelAPI
|
||||||
@MiraiExperimentalAPI
|
@MiraiExperimentalAPI
|
||||||
override suspend fun _lowLevelSendLongMessage(groupCode: Long, message: Message) {
|
override suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: Message): MessageReceipt<Group> {
|
||||||
val chain = message.asMessageChain()
|
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" }
|
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)
|
||||||
|
|
||||||
// TODO: 2020/3/26 util 方法来添加单例元素
|
|
||||||
|
|
||||||
val time = currentTimeSeconds
|
val time = currentTimeSeconds
|
||||||
|
|
||||||
network.run {
|
network.run {
|
||||||
@ -432,14 +431,12 @@ internal abstract class QQAndroidBotBase constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
group.sendMessage(
|
return group.sendMessage(
|
||||||
RichMessage.longMessage(
|
RichMessage.longMessage(
|
||||||
brief = chain.joinToString(limit = 30) {
|
brief = chain.toString().let { // already cached
|
||||||
when (it) {
|
if (it.length > 27) {
|
||||||
is PlainText -> it.stringValue
|
it.take(27) + "..."
|
||||||
is At -> it.toString()
|
} else it
|
||||||
else -> ""
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
resId = resId,
|
resId = resId,
|
||||||
timeSeconds = time
|
timeSeconds = time
|
||||||
|
@ -23,9 +23,7 @@ import net.mamoe.mirai.event.broadcast
|
|||||||
import net.mamoe.mirai.event.events.*
|
import net.mamoe.mirai.event.events.*
|
||||||
import net.mamoe.mirai.event.events.MessageSendEvent.GroupMessageSendEvent
|
import net.mamoe.mirai.event.events.MessageSendEvent.GroupMessageSendEvent
|
||||||
import net.mamoe.mirai.message.MessageReceipt
|
import net.mamoe.mirai.message.MessageReceipt
|
||||||
import net.mamoe.mirai.message.data.Message
|
import net.mamoe.mirai.message.data.*
|
||||||
import net.mamoe.mirai.message.data.OfflineGroupImage
|
|
||||||
import net.mamoe.mirai.message.data.asMessageChain
|
|
||||||
import net.mamoe.mirai.qqandroid.QQAndroidBot
|
import net.mamoe.mirai.qqandroid.QQAndroidBot
|
||||||
import net.mamoe.mirai.qqandroid.message.MessageSourceFromSendGroup
|
import net.mamoe.mirai.qqandroid.message.MessageSourceFromSendGroup
|
||||||
import net.mamoe.mirai.qqandroid.network.highway.HighwayHelper
|
import net.mamoe.mirai.qqandroid.network.highway.HighwayHelper
|
||||||
@ -273,6 +271,7 @@ internal class GroupImpl(
|
|||||||
return members.delegate.filteringGetOrNull { it.id == id }
|
return members.delegate.filteringGetOrNull { it.id == id }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OptIn(MiraiExperimentalAPI::class)
|
||||||
@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" }
|
||||||
@ -280,6 +279,17 @@ internal class GroupImpl(
|
|||||||
if (event.isCancelled) {
|
if (event.isCancelled) {
|
||||||
throw EventCancelledException("cancelled by GroupMessageSendEvent")
|
throw EventCancelledException("cancelled by GroupMessageSendEvent")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message !is LongMessage) {
|
||||||
|
val length = event.message.toString().length
|
||||||
|
if (length > 4000
|
||||||
|
|| event.message.count { it is Image } > 3
|
||||||
|
|| (event.message.any<QuoteReply>() && (event.message.any<Image>() || length > 100))
|
||||||
|
) {
|
||||||
|
return bot._lowLevelSendLongGroupMessage(this.id, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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(
|
||||||
|
@ -315,17 +315,12 @@ private fun MessageChain.cleanupRubbishMessageElements(): MessageChain {
|
|||||||
var last: SingleMessage? = null
|
var last: SingleMessage? = null
|
||||||
return buildMessageChain(initialSize = this.count()) {
|
return buildMessageChain(initialSize = this.count()) {
|
||||||
this@cleanupRubbishMessageElements.forEach { element ->
|
this@cleanupRubbishMessageElements.forEach { element ->
|
||||||
if (last == null) {
|
|
||||||
last = element
|
|
||||||
return@forEach
|
|
||||||
} else {
|
|
||||||
if (last is LongMessage && element is PlainText) {
|
if (last is LongMessage && element is PlainText) {
|
||||||
if (element == UNSUPPORTED_MERGED_MESSAGE_PLAIN) {
|
if (element == UNSUPPORTED_MERGED_MESSAGE_PLAIN) {
|
||||||
last = element
|
last = element
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
add(element)
|
add(element)
|
||||||
last = element
|
last = element
|
||||||
|
@ -13,6 +13,7 @@ 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.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
|
||||||
@ -148,7 +149,7 @@ interface LowLevelBotAPIAccessor {
|
|||||||
@SinceMirai("0.31.0")
|
@SinceMirai("0.31.0")
|
||||||
@LowLevelAPI
|
@LowLevelAPI
|
||||||
@MiraiExperimentalAPI
|
@MiraiExperimentalAPI
|
||||||
suspend fun _lowLevelSendLongMessage(groupCode: Long, message: Message)
|
suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: Message): MessageReceipt<Group>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@file:Suppress("EXPERIMENTAL_UNSIGNED_LITERALS", "EXPERIMENTAL_API_USAGE")
|
@file:Suppress("EXPERIMENTAL_UNSIGNED_LITERALS", "EXPERIMENTAL_API_USAGE", "unused")
|
||||||
|
|
||||||
package net.mamoe.mirai.message
|
package net.mamoe.mirai.message
|
||||||
|
|
||||||
@ -153,6 +153,7 @@ abstract class MessagePacketBase<TSender : QQ, TSubject : Contact> : Packet, Bot
|
|||||||
/**
|
/**
|
||||||
* 引用这个消息
|
* 引用这个消息
|
||||||
*/
|
*/
|
||||||
|
@ExperimentalMessageSource
|
||||||
inline fun MessageChain.quote(): QuoteReplyToSend = this.quote(sender)
|
inline fun MessageChain.quote(): QuoteReplyToSend = this.quote(sender)
|
||||||
|
|
||||||
operator fun <M : Message> get(at: Message.Key<M>): M {
|
operator fun <M : Message> get(at: Message.Key<M>): M {
|
||||||
|
@ -54,7 +54,8 @@ sealed class QuoteReplyToSend
|
|||||||
* 引用这条消息.
|
* 引用这条消息.
|
||||||
* @see sender 消息发送人.
|
* @see sender 消息发送人.
|
||||||
*/
|
*/
|
||||||
@OptIn(MiraiInternalAPI::class, ExperimentalMessageSource::class)
|
@ExperimentalMessageSource
|
||||||
|
@OptIn(MiraiInternalAPI::class)
|
||||||
fun MessageChain.quote(sender: QQ?): QuoteReplyToSend {
|
fun MessageChain.quote(sender: QQ?): QuoteReplyToSend {
|
||||||
this.firstOrNull<MessageSource>()?.let {
|
this.firstOrNull<MessageSource>()?.let {
|
||||||
return it.quote(sender)
|
return it.quote(sender)
|
||||||
|
Loading…
Reference in New Issue
Block a user