From 05aa3c80266e0e8b6b9017e07ec582134856ef49 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 30 Apr 2022 19:01:28 +0100 Subject: [PATCH] Add ContactOrBot.inferMessageSourceKind --- .../kotlin/contact/SendMessageHandler.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt b/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt index c0c753c3c..4a6f58c9f 100644 --- a/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt +++ b/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt @@ -13,6 +13,7 @@ import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Deferred import kotlinx.coroutines.async import kotlinx.coroutines.withTimeoutOrNull +import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.* import net.mamoe.mirai.event.EventPriority import net.mamoe.mirai.event.GlobalEventChannel @@ -51,6 +52,17 @@ import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.castOrNull import net.mamoe.mirai.utils.currentTimeSeconds +internal fun ContactOrBot.inferMessageSourceKind(): MessageSourceKind { + return when (this) { + is Group -> MessageSourceKind.GROUP + is Friend -> MessageSourceKind.FRIEND + is Member -> MessageSourceKind.TEMP + is Stranger -> MessageSourceKind.STRANGER + is Bot -> MessageSourceKind.FRIEND + else -> error("Unsupported contact: $this") + } +} + /** * 处理 mirai 消息系统 `Message` 到协议数据结构的转换. * @@ -62,13 +74,7 @@ internal abstract class SendMessageHandler { val messageSourceKind: MessageSourceKind get() { - return when (contact) { - is Group -> MessageSourceKind.GROUP - is Friend -> MessageSourceKind.FRIEND - is Member -> MessageSourceKind.TEMP - is Stranger -> MessageSourceKind.STRANGER - else -> error("Unsupported contact: $contact") - } + return contact.inferMessageSourceKind() } val bot get() = contact.bot.asQQAndroidBot()