mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-10 02:27:01 +08:00
Find nick from source; fix #1137
This commit is contained in:
parent
1dce29a154
commit
83fa78b50d
@ -14,12 +14,40 @@ import kotlinx.io.core.readBytes
|
||||
import net.mamoe.mirai.contact.Group
|
||||
import net.mamoe.mirai.contact.nameCardOrNick
|
||||
import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody
|
||||
import net.mamoe.mirai.message.data.At
|
||||
import net.mamoe.mirai.message.data.AtAll
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import net.mamoe.mirai.utils.safeCast
|
||||
|
||||
|
||||
internal fun At.toJceData(group: Group?): ImMsgBody.Text {
|
||||
val text = "@${group?.members?.get(this.target)?.nameCardOrNick ?: target}"
|
||||
internal fun At.toJceData(
|
||||
group: Group?,
|
||||
source: MessageSource?,
|
||||
isForward: Boolean,
|
||||
): ImMsgBody.Text {
|
||||
fun findFromGroup(g: Group?): String? {
|
||||
return g?.members?.get(this.target)?.nameCardOrNick
|
||||
}
|
||||
|
||||
fun findFromSource(): String? {
|
||||
return when (source) {
|
||||
is OnlineMessageSource -> {
|
||||
return findFromGroup(source.target.safeCast())
|
||||
}
|
||||
is OfflineMessageSource -> {
|
||||
if (source.kind == MessageSourceKind.GROUP) {
|
||||
return findFromGroup(group?.bot?.getGroup(source.targetId))
|
||||
} else null
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
val text = "@${
|
||||
if (isForward) {
|
||||
findFromSource() ?: findFromGroup(group)
|
||||
} else {
|
||||
findFromGroup(group) ?: findFromSource()
|
||||
} ?: target
|
||||
}"
|
||||
return ImMsgBody.Text(
|
||||
str = text,
|
||||
attr6Buf = buildPacket {
|
||||
|
@ -34,7 +34,8 @@ internal val UNSUPPORTED_VOICE_MESSAGE_PLAIN = PlainText("收到语音消息,
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
internal fun MessageChain.toRichTextElems(
|
||||
messageTarget: ContactOrBot?,
|
||||
withGeneralFlags: Boolean
|
||||
withGeneralFlags: Boolean,
|
||||
isForward: Boolean = false,
|
||||
): MutableList<ImMsgBody.Elem> {
|
||||
val forGroup = messageTarget is Group
|
||||
val elements = ArrayList<ImMsgBody.Elem>(this.size)
|
||||
@ -114,7 +115,15 @@ internal fun MessageChain.toRichTextElems(
|
||||
)
|
||||
}
|
||||
is At -> {
|
||||
elements.add(ImMsgBody.Elem(text = currentMessage.toJceData(messageTarget.safeCast())))
|
||||
elements.add(
|
||||
ImMsgBody.Elem(
|
||||
text = currentMessage.toJceData(
|
||||
messageTarget.safeCast(),
|
||||
this[MessageSource],
|
||||
isForward,
|
||||
)
|
||||
)
|
||||
)
|
||||
// elements.add(ImMsgBody.Elem(text = ImMsgBody.Text(str = " ")))
|
||||
// removed by https://github.com/mamoe/mirai/issues/524
|
||||
// 发送 QuoteReply 消息时无可避免的产生多余空格 #524
|
||||
|
@ -86,7 +86,11 @@ internal fun Collection<ForwardMessage.INode>.calculateValidationData(
|
||||
msgBody = ImMsgBody.MsgBody(
|
||||
richText = ImMsgBody.RichText(
|
||||
elems = chain.messageChain.toMessageChain()
|
||||
.toRichTextElems(handler.contact, withGeneralFlags = false).toMutableList()
|
||||
.toRichTextElems(
|
||||
handler.contact,
|
||||
withGeneralFlags = false,
|
||||
isForward = true,
|
||||
).toMutableList()
|
||||
)
|
||||
)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user