diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/FriendMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/FriendMessage.kt index b9d0a8138..e0075ddd5 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/FriendMessage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/FriendMessage.kt @@ -13,15 +13,24 @@ import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.QQ import net.mamoe.mirai.event.BroadcastControllable import net.mamoe.mirai.message.data.MessageChain +import net.mamoe.mirai.message.data.MessageSource import net.mamoe.mirai.message.data.OnlineMessageSource import net.mamoe.mirai.message.data.source import net.mamoe.mirai.utils.getValue import net.mamoe.mirai.utils.unsafeWeakRef +/** + * 好友消息 + */ class FriendMessage( sender: QQ, override val message: MessageChain ) : ContactMessage(), BroadcastControllable { + init { + val source = message.getOrNull(MessageSource) ?: error("Cannot find MessageSource from message") + check(source is OnlineMessageSource.Incoming.FromFriend) { "source provided to a FriendMessage must be an instance of OnlineMessageSource.Incoming.FromFriend" } + } + override val sender: QQ by sender.unsafeWeakRef() override val bot: Bot get() = sender.bot override val subject: QQ get() = sender diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/GroupMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/GroupMessage.kt index 883a25caa..fc961d633 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/GroupMessage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/GroupMessage.kt @@ -15,6 +15,7 @@ import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.MemberPermission import net.mamoe.mirai.event.Event import net.mamoe.mirai.message.data.MessageChain +import net.mamoe.mirai.message.data.MessageSource import net.mamoe.mirai.message.data.OnlineMessageSource import net.mamoe.mirai.message.data.source import net.mamoe.mirai.utils.getValue @@ -30,6 +31,11 @@ class GroupMessage( sender: Member, override val message: MessageChain ) : ContactMessage(), Event { + init { + val source = message.getOrNull(MessageSource) ?: error("Cannot find MessageSource from message") + check(source is OnlineMessageSource.Incoming.FromGroup) { "source provided to a GroupMessage must be an instance of OnlineMessageSource.Incoming.FromGroup" } + } + override val sender: Member by sender.unsafeWeakRef() val group: Group get() = sender.group override val bot: Bot get() = sender.bot diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/TempMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/TempMessage.kt index f02688e16..3b4062979 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/TempMessage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/TempMessage.kt @@ -4,19 +4,31 @@ import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.Member import net.mamoe.mirai.event.BroadcastControllable import net.mamoe.mirai.message.data.MessageChain +import net.mamoe.mirai.message.data.MessageSource import net.mamoe.mirai.message.data.OnlineMessageSource import net.mamoe.mirai.message.data.source +import net.mamoe.mirai.utils.SinceMirai import net.mamoe.mirai.utils.getValue import net.mamoe.mirai.utils.unsafeWeakRef +/** + * 临时会话消息 + */ +@SinceMirai("0.35.0") class TempMessage( sender: Member, override val message: MessageChain ) : ContactMessage(), BroadcastControllable { + init { + val source = message.getOrNull(MessageSource) ?: error("Cannot find MessageSource from message") + check(source is OnlineMessageSource.Incoming.FromTemp) { "source provided to a TempMessage must be an instance of OnlineMessageSource.Incoming.FromTemp" } + } + override val sender: Member by sender.unsafeWeakRef() override val bot: Bot get() = sender.bot override val subject: Member get() = sender override val source: OnlineMessageSource.Incoming.FromTemp get() = message.source as OnlineMessageSource.Incoming.FromTemp - override fun toString(): String = "TempMessage(sender=${sender.id} from group(${sender.group.id}), message=$message)" + override fun toString(): String = + "TempMessage(sender=${sender.id} from group(${sender.group.id}), message=$message)" } \ No newline at end of file