diff --git a/mirai-core/src/commonMain/kotlin/contact/AbstractUser.kt b/mirai-core/src/commonMain/kotlin/contact/AbstractUser.kt index 37e35bb2c..580bb3991 100644 --- a/mirai-core/src/commonMain/kotlin/contact/AbstractUser.kt +++ b/mirai-core/src/commonMain/kotlin/contact/AbstractUser.kt @@ -18,7 +18,7 @@ import net.mamoe.mirai.event.broadcast import net.mamoe.mirai.event.events.* import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.message.contextualBugReportException -import net.mamoe.mirai.internal.message.flags.MiraiInternalMessageFlag +import net.mamoe.mirai.internal.message.flags.SkipEventBroadcast import net.mamoe.mirai.internal.message.image.* import net.mamoe.mirai.internal.message.protocol.MessageProtocolFacade import net.mamoe.mirai.internal.message.protocol.outgoing.HighwayUploader @@ -252,13 +252,13 @@ internal suspend fun C.sendMessageImpl( preSendEventConstructor: (C, Message) -> MessagePreSendEvent, postSendEventConstructor: (C, MessageChain, Throwable?, MessageReceipt?) -> MessagePostSendEvent, ): MessageReceipt { - val isMiraiInternal = if (message is MessageChain) { - message.anyIsInstance() + val skipEvent = if (message is MessageChain) { + message.anyIsInstance() } else false require(!message.isContentEmpty()) { "message is empty" } - val chain = broadcastMessagePreSendEvent(message, isMiraiInternal, preSendEventConstructor) + val chain = broadcastMessagePreSendEvent(message, skipEvent, preSendEventConstructor) val result = kotlin.runCatching { MessageProtocolFacade.preprocessAndSendOutgoing(this, message, buildComponentStorage { @@ -273,7 +273,7 @@ internal suspend fun C.sendMessageImpl( bot.logger.verbose("$this <- $chain".replaceMagicCodes()) } - if (!isMiraiInternal) { + if (!skipEvent) { postSendEventConstructor(this, chain, result.exceptionOrNull(), result.getOrNull()).broadcast() } diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupSendMessageImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupSendMessageImpl.kt index b3c534546..bb51bb18d 100644 --- a/mirai-core/src/commonMain/kotlin/contact/GroupSendMessageImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/GroupSendMessageImpl.kt @@ -24,10 +24,10 @@ import net.mamoe.mirai.message.data.toMessageChain */ internal suspend fun C.broadcastMessagePreSendEvent( message: Message, - isMiraiInternal: Boolean, + skipEvent: Boolean, eventConstructor: (C, Message) -> MessagePreSendEvent, ): MessageChain { - if (isMiraiInternal) return message.toMessageChain() + if (skipEvent) return message.toMessageChain() var eventName: String? = null return kotlin.runCatching { eventConstructor(this, message).also { diff --git a/mirai-core/src/commonMain/kotlin/message/flags/InternalFlagOnlyMessage.kt b/mirai-core/src/commonMain/kotlin/message/flags/InternalFlagOnlyMessage.kt index d5c399056..d333fc302 100644 --- a/mirai-core/src/commonMain/kotlin/message/flags/InternalFlagOnlyMessage.kt +++ b/mirai-core/src/commonMain/kotlin/message/flags/InternalFlagOnlyMessage.kt @@ -11,6 +11,7 @@ package net.mamoe.mirai.internal.message.flags +import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.internal.message.visitor.ex import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.visitor.MessageVisitor @@ -79,25 +80,28 @@ internal object IgnoreLengthCheck : MessageMetadata, ConstrainSingle, InternalFl } /** - * 代表来自 mirai 内部 + * Skip broadcasting events. Used for [Contact.sendMessage] */ -internal object MiraiInternalMessageFlag : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage, - AbstractMessageKey({ it.safeCast() }) { - override val key: MessageKey get() = this - override fun toString(): String = "" +internal sealed interface SkipEventBroadcast : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage { + override val key: MessageKey get() = Companion override fun accept(visitor: MessageVisitor, data: D): R { - return visitor.ex()?.visitMiraiInternalMessageFlag(this, data) ?: super.accept( + return visitor.ex()?.visitSkipEventBroadcast(this, data) ?: super.accept( visitor, data ) } + + companion object : AbstractMessageKey({ it.safeCast() }), SkipEventBroadcast { + // instance + + override fun toString(): String = "" + } } -internal object AllowSendFileMessage : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage, - AbstractMessageKey({ it.safeCast() }) { - override val key: MessageKey get() = this - override fun toString(): String = "" +internal sealed interface AllowSendFileMessage : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage, + SkipEventBroadcast { + override val key: MessageKey get() = Companion override fun accept(visitor: MessageVisitor, data: D): R { return visitor.ex()?.visitAllowSendFileMessage(this, data) ?: super.accept( @@ -105,4 +109,8 @@ internal object AllowSendFileMessage : MessageMetadata, ConstrainSingle, Interna data ) } + + companion object : AbstractMessageKey({ it.safeCast() }), AllowSendFileMessage { + override fun toString(): String = "" + } } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/message/visitor/MessageVisitorEx.kt b/mirai-core/src/commonMain/kotlin/message/visitor/MessageVisitorEx.kt index 8202510e0..513db8dd6 100644 --- a/mirai-core/src/commonMain/kotlin/message/visitor/MessageVisitorEx.kt +++ b/mirai-core/src/commonMain/kotlin/message/visitor/MessageVisitorEx.kt @@ -61,11 +61,11 @@ internal interface MessageVisitorEx : MessageVisitor { return visitInternalFlagOnlyMessage(message, data) } - fun visitMiraiInternalMessageFlag(message: MiraiInternalMessageFlag, data: D): R { + fun visitSkipEventBroadcast(message: SkipEventBroadcast, data: D): R { return visitInternalFlagOnlyMessage(message, data) } fun visitAllowSendFileMessage(message: AllowSendFileMessage, data: D): R { - return visitInternalFlagOnlyMessage(message, data) + return visitSkipEventBroadcast(message, data) } } \ No newline at end of file diff --git a/mirai-core/src/jvmBaseMain/kotlin/utils/RemoteFileImpl.kt b/mirai-core/src/jvmBaseMain/kotlin/utils/RemoteFileImpl.kt index 932393f40..f7f76eeab 100644 --- a/mirai-core/src/jvmBaseMain/kotlin/utils/RemoteFileImpl.kt +++ b/mirai-core/src/jvmBaseMain/kotlin/utils/RemoteFileImpl.kt @@ -21,7 +21,6 @@ import net.mamoe.mirai.internal.asQQAndroidBot import net.mamoe.mirai.internal.contact.groupCode import net.mamoe.mirai.internal.message.data.FileMessageImpl import net.mamoe.mirai.internal.message.flags.AllowSendFileMessage -import net.mamoe.mirai.internal.message.flags.MiraiInternalMessageFlag import net.mamoe.mirai.internal.network.highway.Highway import net.mamoe.mirai.internal.network.highway.ResourceKind import net.mamoe.mirai.internal.network.protocol