diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt index bdcd93995..b32e7a4c4 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt @@ -209,8 +209,10 @@ public sealed interface MessageChain : * * @see MessageChain.getOrFail 在找不到此类型的元素时抛出 [NoSuchElementException] */ - public operator fun <M : SingleMessage> get(key: MessageKey<M>): M? = - asSequence().mapNotNull { key.safeCast.invoke(it) }.firstOrNull() + public operator fun <M : SingleMessage> get(key: MessageKey<M>): M? { + @Suppress("UNCHECKED_CAST") + return firstOrNull { key.safeCast.invoke(it) != null } as M? + } /** * 当存在 [ConstrainSingle.key] 为 [key] 的 [SingleMessage] 实例时返回 `true`. @@ -239,7 +241,7 @@ public sealed interface MessageChain : * @see MessageChain.getOrFail 在找不到此类型的元素时抛出 [NoSuchElementException] */ public operator fun <M : SingleMessage> contains(key: MessageKey<M>): Boolean = - asSequence().any { key.safeCast.invoke(it) != null } + any { key.safeCast.invoke(it) != null } @MiraiExperimentalApi override fun appendMiraiCodeTo(builder: StringBuilder) { diff --git a/mirai-core-api/src/commonTest/kotlin/message.data/MessageKeyTest.kt b/mirai-core-api/src/commonTest/kotlin/message.data/MessageKeyTest.kt index 0cb907cf6..b90a284d9 100644 --- a/mirai-core-api/src/commonTest/kotlin/message.data/MessageKeyTest.kt +++ b/mirai-core-api/src/commonTest/kotlin/message.data/MessageKeyTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -12,6 +12,7 @@ package net.mamoe.mirai.message.data import net.mamoe.mirai.utils.safeCast import org.junit.jupiter.api.Test import kotlin.test.assertEquals +import kotlin.test.assertSame private open class TestStandaloneConstrainSingleMessage : ConstrainSingle, MessageContent { @@ -98,4 +99,26 @@ internal class MessageKeyTest { assertEquals<Any?>(constrainSingle, chain[TestStandaloneConstrainSingleMessage]) assertEquals(1, chain.size) } + + @Test + fun `can get from MessageChain`() { + val cons = TestStandaloneConstrainSingleMessage() + assertSame(cons, messageChainOf(cons)[TestStandaloneConstrainSingleMessage]) + } + + @Test + fun `can get from MessageChain2`() { + val cons = TestStandaloneConstrainSingleMessage() + assertSame(cons, messageChainOf(PlainText("test"), AtAll, cons)[TestStandaloneConstrainSingleMessage]) + } + + @Test + fun `get can be null`() { + assertEquals(null, messageChainOf()[TestStandaloneConstrainSingleMessage]) + } + + @Test + fun `get can be null2`() { + assertEquals(null, messageChainOf(PlainText("test"), AtAll)[TestStandaloneConstrainSingleMessage]) + } } \ No newline at end of file