diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/At.kt b/mirai-core-api/src/commonMain/kotlin/message/data/At.kt index fb0383451..b44bb75dd 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/At.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/At.kt @@ -16,8 +16,10 @@ package net.mamoe.mirai.message.data import kotlinx.serialization.Serializable import net.mamoe.mirai.LowLevelApi +import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.UserOrBot +import net.mamoe.mirai.contact.nameCardOrNick import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.utils.PlannedRemoval @@ -38,13 +40,21 @@ public data class At( public override fun contentToString(): String = "@$target" @Suppress("DeprecatedCallableAddReplaceWith") - @Deprecated( - "At.display is no longer supported. Please get Member.nameCard by your self.", level = DeprecationLevel.ERROR - ) + @Deprecated("Use getDisplay", ReplaceWith("this.getDisplay()"), DeprecationLevel.ERROR) @PlannedRemoval("2.0-M2") val display: Nothing get() = error("At.display is no longer supported") + /** + * 获取 [At] 发送于指定 [Group] 时会显示的内容. + * + * 若 [group] 非 `null` 且包含成员 [target], 返回 `"@成员名片或昵称"`. 否则返回 `"@123456"` 其中 123456 表示 [target] + */ + public fun getDisplay(group: Group?): String { + val member = group?.get(this.target) ?: return "@$target" + return "@${member.nameCardOrNick}" + } + public companion object { /** * 构造一个 [At], 仅供内部使用, 否则可能造成消息无法发出的问题. diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt index c30fc4185..88b007b2b 100644 --- a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt @@ -298,7 +298,7 @@ internal class GroupImpl( throw EventCancelledException("exception thrown when broadcasting GroupMessagePreSendEvent", it) }.message.asMessageChain() - val length = chain.estimateLength(703) // 阈值为700左右,限制到3的倍数 + val length = chain.estimateLength(this, 703) // 阈值为700左右,限制到3的倍数 var imageCnt = 0 // 通过下方逻辑短路延迟计算 if (length > 5000 || chain.count { it is Image }.apply { imageCnt = this } > 50) { diff --git a/mirai-core/src/commonMain/kotlin/utils/type.kt b/mirai-core/src/commonMain/kotlin/utils/type.kt index adbb7e7ad..6775f0efd 100644 --- a/mirai-core/src/commonMain/kotlin/utils/type.kt +++ b/mirai-core/src/commonMain/kotlin/utils/type.kt @@ -12,10 +12,10 @@ package net.mamoe.mirai.internal.utils +import net.mamoe.mirai.contact.ContactOrBot import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.AtAll.display -import kotlin.jvm.JvmMultifileClass -import kotlin.jvm.JvmName +import net.mamoe.mirai.utils.safeCast internal fun Int.toIpV4AddressString(): String { @@ -43,17 +43,17 @@ internal fun String.chineseLength(upTo: Int): Int { } } -internal fun MessageChain.estimateLength(upTo: Int): Int = +internal fun MessageChain.estimateLength(target: ContactOrBot, upTo: Int): Int = sumUpTo(upTo) { it, up -> - it.estimateLength(up) + it.estimateLength(target, up) } -internal fun SingleMessage.estimateLength(upTo: Int): Int { +internal fun SingleMessage.estimateLength(target: ContactOrBot, upTo: Int): Int { return when (this) { - is QuoteReply -> 444 + this.source.originalMessage.estimateLength(upTo) // Magic number + is QuoteReply -> 444 + this.source.originalMessage.estimateLength(target, upTo) // Magic number is Image -> 260 // Magic number is PlainText -> content.chineseLength(upTo) - is At -> display.chineseLength(upTo) + is At -> this.getDisplay(target.safeCast()).chineseLength(upTo) is AtAll -> display.chineseLength(upTo) else -> this.toString().chineseLength(upTo) }