From 573f08955cdcf03223ef41c5bec76760bc4140e9 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 31 Oct 2022 10:40:26 +0000 Subject: [PATCH] [core] Show specific reason for error 46 while sending message. Close #2127 --- .../android/api/android.api | 2 ++ .../compatibility-validation/jvm/api/jvm.api | 2 ++ .../src/commonMain/kotlin/contact/Exceptions.kt | 13 ++++++++++++- .../protocol/impl/GeneralMessageSenderProtocol.kt | 7 +++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/mirai-core-api/compatibility-validation/android/api/android.api b/mirai-core-api/compatibility-validation/android/api/android.api index e4d676a61..1ee4c9c52 100644 --- a/mirai-core-api/compatibility-validation/android/api/android.api +++ b/mirai-core-api/compatibility-validation/android/api/android.api @@ -563,6 +563,7 @@ public final class net/mamoe/mirai/contact/Platform$Companion { } public class net/mamoe/mirai/contact/SendMessageFailedException : java/lang/RuntimeException { + public synthetic fun (Lnet/mamoe/mirai/contact/Contact;Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason;Lnet/mamoe/mirai/message/data/Message;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getOriginalMessage ()Lnet/mamoe/mirai/message/data/Message; public final fun getReason ()Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public fun getTarget ()Lnet/mamoe/mirai/contact/Contact; @@ -572,6 +573,7 @@ public final class net/mamoe/mirai/contact/SendMessageFailedException$Reason : j public static final field AT_ALL_LIMITED Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static final field BOT_MUTED Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static final field GROUP_CHAT_LIMITED Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; + public static final field LIMITED_MESSAGING Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static final field MESSAGE_TOO_LARGE Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static fun valueOf (Ljava/lang/String;)Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static fun values ()[Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; diff --git a/mirai-core-api/compatibility-validation/jvm/api/jvm.api b/mirai-core-api/compatibility-validation/jvm/api/jvm.api index dc47878f4..48ce87022 100644 --- a/mirai-core-api/compatibility-validation/jvm/api/jvm.api +++ b/mirai-core-api/compatibility-validation/jvm/api/jvm.api @@ -563,6 +563,7 @@ public final class net/mamoe/mirai/contact/Platform$Companion { } public class net/mamoe/mirai/contact/SendMessageFailedException : java/lang/RuntimeException { + public synthetic fun (Lnet/mamoe/mirai/contact/Contact;Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason;Lnet/mamoe/mirai/message/data/Message;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getOriginalMessage ()Lnet/mamoe/mirai/message/data/Message; public final fun getReason ()Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public fun getTarget ()Lnet/mamoe/mirai/contact/Contact; @@ -572,6 +573,7 @@ public final class net/mamoe/mirai/contact/SendMessageFailedException$Reason : j public static final field AT_ALL_LIMITED Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static final field BOT_MUTED Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static final field GROUP_CHAT_LIMITED Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; + public static final field LIMITED_MESSAGING Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static final field MESSAGE_TOO_LARGE Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static fun valueOf (Ljava/lang/String;)Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; public static fun values ()[Lnet/mamoe/mirai/contact/SendMessageFailedException$Reason; diff --git a/mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt b/mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt index aff615cf0..fc0eb75f7 100644 --- a/mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt +++ b/mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt @@ -74,8 +74,12 @@ public open class SendMessageFailedException @MiraiInternalApi constructor( public open val target: Contact, public val reason: Reason, public val originalMessage: Message, + /** + * @since 2.14 + */ + tips: String? = null, ) : RuntimeException( - "Failed sending message to $target, reason=$reason" + "Failed sending message to $target, reason=$reason. Tips: $tips" ) { public enum class Reason { /** @@ -98,5 +102,12 @@ public open class SendMessageFailedException @MiraiInternalApi constructor( * @since 2.11 */ AT_ALL_LIMITED, + + /** + * 被服务器限制发送消息, 属于冻结的一种 + * + * @since 2.14 + */ + LIMITED_MESSAGING, } } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/message/protocol/impl/GeneralMessageSenderProtocol.kt b/mirai-core/src/commonMain/kotlin/message/protocol/impl/GeneralMessageSenderProtocol.kt index 4ffe8fcdc..0d9d744c3 100644 --- a/mirai-core/src/commonMain/kotlin/message/protocol/impl/GeneralMessageSenderProtocol.kt +++ b/mirai-core/src/commonMain/kotlin/message/protocol/impl/GeneralMessageSenderProtocol.kt @@ -145,6 +145,13 @@ internal class GeneralMessageSenderProtocol : MessageProtocol(PRIORITY_GENERAL_S SendMessageFailedException.Reason.GROUP_CHAT_LIMITED, originalMessage ) + // #2127 + 46 -> throw SendMessageFailedException( + contact, + SendMessageFailedException.Reason.LIMITED_MESSAGING, + originalMessage, + tips = "问题原因可能是账号被多次举报或被服务器认为不安全. 可尝试访问 https://accounts.qq.com/safe/message/unlock?lock_info=5_5 解冻." + ) } } check(resp is MessageSvcPbSendMsg.Response.SUCCESS) {