Add At.getDisplay

This commit is contained in:
Him188 2020-12-18 19:56:39 +08:00
parent 6efb2ae525
commit 669b04b287
3 changed files with 21 additions and 11 deletions

View File

@ -16,8 +16,10 @@ package net.mamoe.mirai.message.data
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import net.mamoe.mirai.LowLevelApi import net.mamoe.mirai.LowLevelApi
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.UserOrBot import net.mamoe.mirai.contact.UserOrBot
import net.mamoe.mirai.contact.nameCardOrNick
import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.utils.PlannedRemoval import net.mamoe.mirai.utils.PlannedRemoval
@ -38,13 +40,21 @@ public data class At(
public override fun contentToString(): String = "@$target" public override fun contentToString(): String = "@$target"
@Suppress("DeprecatedCallableAddReplaceWith") @Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated( @Deprecated("Use getDisplay", ReplaceWith("this.getDisplay()"), DeprecationLevel.ERROR)
"At.display is no longer supported. Please get Member.nameCard by your self.", level = DeprecationLevel.ERROR
)
@PlannedRemoval("2.0-M2") @PlannedRemoval("2.0-M2")
val display: Nothing val display: Nothing
get() = error("At.display is no longer supported") 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 { public companion object {
/** /**
* 构造一个 [At], 仅供内部使用, 否则可能造成消息无法发出的问题. * 构造一个 [At], 仅供内部使用, 否则可能造成消息无法发出的问题.

View File

@ -298,7 +298,7 @@ internal class GroupImpl(
throw EventCancelledException("exception thrown when broadcasting GroupMessagePreSendEvent", it) throw EventCancelledException("exception thrown when broadcasting GroupMessagePreSendEvent", it)
}.message.asMessageChain() }.message.asMessageChain()
val length = chain.estimateLength(703) // 阈值为700左右限制到3的倍数 val length = chain.estimateLength(this, 703) // 阈值为700左右限制到3的倍数
var imageCnt = 0 // 通过下方逻辑短路延迟计算 var imageCnt = 0 // 通过下方逻辑短路延迟计算
if (length > 5000 || chain.count { it is Image }.apply { imageCnt = this } > 50) { if (length > 5000 || chain.count { it is Image }.apply { imageCnt = this } > 50) {

View File

@ -12,10 +12,10 @@
package net.mamoe.mirai.internal.utils package net.mamoe.mirai.internal.utils
import net.mamoe.mirai.contact.ContactOrBot
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.AtAll.display import net.mamoe.mirai.message.data.AtAll.display
import kotlin.jvm.JvmMultifileClass import net.mamoe.mirai.utils.safeCast
import kotlin.jvm.JvmName
internal fun Int.toIpV4AddressString(): String { 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 -> 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) { 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 Image -> 260 // Magic number
is PlainText -> content.chineseLength(upTo) is PlainText -> content.chineseLength(upTo)
is At -> display.chineseLength(upTo) is At -> this.getDisplay(target.safeCast()).chineseLength(upTo)
is AtAll -> display.chineseLength(upTo) is AtAll -> display.chineseLength(upTo)
else -> this.toString().chineseLength(upTo) else -> this.toString().chineseLength(upTo)
} }