From 1d2c219b9dd474038c54e27b5baca7893c9a7bd2 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Wed, 9 Feb 2022 22:21:09 +0800 Subject: [PATCH] Fix MessagePreSendEvent name; fix #1851 --- .../commonMain/kotlin/contact/GroupSendMessageImpl.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupSendMessageImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupSendMessageImpl.kt index 22e4067ca..b9308094e 100644 --- a/mirai-core/src/commonMain/kotlin/contact/GroupSendMessageImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/GroupSendMessageImpl.kt @@ -28,14 +28,18 @@ internal suspend fun C.broadcastMessagePreSendEvent( eventConstructor: (C, Message) -> MessagePreSendEvent, ): MessageChain { if (isMiraiInternal) return message.toMessageChain() + var eventName: String? = null return kotlin.runCatching { - eventConstructor(this, message).broadcast() + eventConstructor(this, message).also { + eventName = it.javaClass.simpleName + }.broadcast() }.onSuccess { check(!it.isCancelled) { - throw EventCancelledException("cancelled by GroupMessagePreSendEvent") + throw EventCancelledException("cancelled by $eventName") } }.getOrElse { - throw EventCancelledException("exception thrown when broadcasting GroupMessagePreSendEvent", it) + eventName = eventName ?: (this@broadcastMessagePreSendEvent.javaClass.simpleName + "MessagePreSendEvent") + throw EventCancelledException("exception thrown when broadcasting $eventName", it) }.message.toMessageChain() }