From 7a217f6f65be01411e305cfe25701a2a3891859d Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 22 Feb 2020 14:32:52 +0800 Subject: [PATCH] Add `MessageChain.messageUid`, `MessageChain.sequenceId` --- .../kotlin/net.mamoe.mirai/contact/Contact.kt | 2 +- .../net.mamoe.mirai/message/data/MessageChain.kt | 2 +- .../net.mamoe.mirai/message/data/MessageSource.kt | 12 +++++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt index 68224bdd0..fb1fc8942 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt @@ -48,7 +48,7 @@ interface Contact : CoroutineScope { val id: Long /** - * 向这个对象发送消息. 发送成功后 [message] 中会添加 [MessageSource], 此后可以 [引用回复][MessageReceipt.quote](仅群聊)或 [撤回][MessageReceipt.recall] 这条消息. + * 向这个对象发送消息. * * @see FriendMessageSendEvent 发送好友信息事件, cancellable * @see GroupMessageSendEvent 发送群消息事件. cancellable diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt index 9a4c9ad4e..f66376bb3 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt @@ -209,7 +209,7 @@ fun MessageChain.firstOrNull(key: Message.Key): M? = when (key) * @throws [NoSuchElementException] 如果找不到该类型的实例 */ @Suppress("UNCHECKED_CAST") -fun MessageChain.first(key: Message.Key): M = firstOrNull(key) ?: error("unknown key: $key") +fun MessageChain.first(key: Message.Key): M = firstOrNull(key) ?: throw NoSuchElementException("no such element: $key") /** * 获取第一个 [M] 类型的 [Message] 实例 diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt index c0ecb35cd..d7a047a30 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt @@ -62,4 +62,14 @@ interface MessageSource : Message { * 固定返回空字符串 ("") */ override fun toString(): String -} \ No newline at end of file +} + +/** + * 消息唯一标识符. 实际上是个随机数, 但服务器确实是用它当做 uid + */ +val MessageChain.messageUid get() = this[MessageSource].messageUid + +/** + * 消息序列号, 可能来自服务器也可以发送时赋值, 不唯一. + */ +val MessageChain.sequenceId get() = this[MessageSource].sequenceId \ No newline at end of file