Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send message failed exception #1701

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ public abstract interface class net/mamoe/mirai/contact/AudioSupported : net/mam
public abstract fun uploadAudio (Lnet/mamoe/mirai/utils/ExternalResource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class net/mamoe/mirai/contact/BotIsBeingMutedException : java/lang/RuntimeException {
public final class net/mamoe/mirai/contact/BotIsBeingMutedException : net/mamoe/mirai/contact/SendMessageFailedException {
public fun <init> (Lnet/mamoe/mirai/contact/Group;)V
public final fun getTarget ()Lnet/mamoe/mirai/contact/Group;
public fun getMessage ()Ljava/lang/String;
public synthetic fun getTarget ()Lnet/mamoe/mirai/contact/Contact;
public fun getTarget ()Lnet/mamoe/mirai/contact/Group;
}

public final class net/mamoe/mirai/contact/ClientKind : java/lang/Enum {
Expand Down Expand Up @@ -439,11 +441,11 @@ public final class net/mamoe/mirai/contact/MemberPermissionKt {
public static final fun isOwner (Lnet/mamoe/mirai/contact/Member;)Z
}

public final class net/mamoe/mirai/contact/MessageTooLargeException : java/lang/RuntimeException {
public final class net/mamoe/mirai/contact/MessageTooLargeException : net/mamoe/mirai/contact/SendMessageFailedException {
public fun <init> (Lnet/mamoe/mirai/contact/Contact;Lnet/mamoe/mirai/message/data/Message;Lnet/mamoe/mirai/message/data/Message;Ljava/lang/String;)V
public fun getMessage ()Ljava/lang/String;
public final fun getMessageAfterEvent ()Lnet/mamoe/mirai/message/data/Message;
public final fun getOriginalMessage ()Lnet/mamoe/mirai/message/data/Message;
public final fun getTarget ()Lnet/mamoe/mirai/contact/Contact;
public fun getTarget ()Lnet/mamoe/mirai/contact/Contact;
}

public abstract interface class net/mamoe/mirai/contact/NormalMember : net/mamoe/mirai/contact/Member {
Expand Down Expand Up @@ -529,6 +531,20 @@ public final class net/mamoe/mirai/contact/Platform : java/lang/Enum {
public final class net/mamoe/mirai/contact/Platform$Companion {
}

public class net/mamoe/mirai/contact/SendMessageFailedException : java/lang/RuntimeException {
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;
}

public final class net/mamoe/mirai/contact/SendMessageFailedException$Reason : java/lang/Enum {
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 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;
}

public abstract interface class net/mamoe/mirai/contact/Stranger : kotlinx/coroutines/CoroutineScope, net/mamoe/mirai/contact/User {
public fun delete ()V
public abstract fun delete (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ public abstract interface class net/mamoe/mirai/contact/AudioSupported : net/mam
public abstract fun uploadAudio (Lnet/mamoe/mirai/utils/ExternalResource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class net/mamoe/mirai/contact/BotIsBeingMutedException : java/lang/RuntimeException {
public final class net/mamoe/mirai/contact/BotIsBeingMutedException : net/mamoe/mirai/contact/SendMessageFailedException {
public fun <init> (Lnet/mamoe/mirai/contact/Group;)V
public final fun getTarget ()Lnet/mamoe/mirai/contact/Group;
public fun getMessage ()Ljava/lang/String;
public synthetic fun getTarget ()Lnet/mamoe/mirai/contact/Contact;
public fun getTarget ()Lnet/mamoe/mirai/contact/Group;
}

public final class net/mamoe/mirai/contact/ClientKind : java/lang/Enum {
Expand Down Expand Up @@ -439,11 +441,11 @@ public final class net/mamoe/mirai/contact/MemberPermissionKt {
public static final fun isOwner (Lnet/mamoe/mirai/contact/Member;)Z
}

public final class net/mamoe/mirai/contact/MessageTooLargeException : java/lang/RuntimeException {
public final class net/mamoe/mirai/contact/MessageTooLargeException : net/mamoe/mirai/contact/SendMessageFailedException {
public fun <init> (Lnet/mamoe/mirai/contact/Contact;Lnet/mamoe/mirai/message/data/Message;Lnet/mamoe/mirai/message/data/Message;Ljava/lang/String;)V
public fun getMessage ()Ljava/lang/String;
public final fun getMessageAfterEvent ()Lnet/mamoe/mirai/message/data/Message;
public final fun getOriginalMessage ()Lnet/mamoe/mirai/message/data/Message;
public final fun getTarget ()Lnet/mamoe/mirai/contact/Contact;
public fun getTarget ()Lnet/mamoe/mirai/contact/Contact;
}

public abstract interface class net/mamoe/mirai/contact/NormalMember : net/mamoe/mirai/contact/Member {
Expand Down Expand Up @@ -529,6 +531,20 @@ public final class net/mamoe/mirai/contact/Platform : java/lang/Enum {
public final class net/mamoe/mirai/contact/Platform$Companion {
}

public class net/mamoe/mirai/contact/SendMessageFailedException : java/lang/RuntimeException {
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;
}

public final class net/mamoe/mirai/contact/SendMessageFailedException$Reason : java/lang/Enum {
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 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;
}

public abstract interface class net/mamoe/mirai/contact/Stranger : kotlinx/coroutines/CoroutineScope, net/mamoe/mirai/contact/User {
public fun delete ()V
public abstract fun delete (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down
64 changes: 54 additions & 10 deletions mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
package net.mamoe.mirai.contact

import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.messageChainOf
import net.mamoe.mirai.utils.DeprecatedSinceMirai
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.millisToHumanReadableString
import kotlin.time.ExperimentalTime

Expand All @@ -20,31 +23,72 @@ import kotlin.time.ExperimentalTime
*
* @see Contact.sendMessage
*/
public class MessageTooLargeException(
public val target: Contact,
public class MessageTooLargeException constructor(
public override val target: Contact,
/**
* 原发送消息
*/
public val originalMessage: Message,
originalMessage: Message,
/**
* 经过事件拦截处理后的消息
*/
public val messageAfterEvent: Message,
exceptionMessage: String
) : RuntimeException(exceptionMessage)
) : SendMessageFailedException(target, Reason.MESSAGE_TOO_LARGE, originalMessage) {
override val message: String = exceptionMessage
}

/**
* 发送消息时 bot 正处于被禁言状态时抛出的异常.
*
* @see Group.sendMessage
*/
@OptIn(ExperimentalTime::class)
public class BotIsBeingMutedException(
public val target: Group
) : RuntimeException(
"bot is being muted, remaining ${
public class BotIsBeingMutedException @MiraiInternalApi constructor(
// this constructor is since 2.9.0-RC
public override val target: Group,
originalMessage: Message,
) : SendMessageFailedException(target, Reason.BOT_MUTED, originalMessage) {
@DeprecatedSinceMirai("2.9")
@Deprecated("Deprecated without replacement. Please consider copy this exception to your code.")
// this constructor is since 2.0
public constructor(
target: Group,
) : this(target, messageChainOf())

override val message: String = "bot is being muted, remaining ${
target.botMuteRemaining.times(1000).millisToHumanReadableString()
} seconds"
)
}

public inline val BotIsBeingMutedException.botMuteRemaining: Int get() = target.botMuteRemaining

/**
* 发送消息失败时抛出的异常
*
* @since 2.9.0
*/
public open class SendMessageFailedException @MiraiInternalApi constructor(
public open val target: Contact,
public val reason: Reason,
public val originalMessage: Message,
) : RuntimeException(
"Failed sending message to $target, reason=$reason"
) {
public enum class Reason {
/**
* 消息过长
*/
MESSAGE_TOO_LARGE,

/**
* 机器人被禁言
*/
BOT_MUTED,

public inline val BotIsBeingMutedException.botMuteRemaining: Int get() = target.botMuteRemaining
/**
* 达到群每分钟发言次数限制
*/
GROUP_CHAT_LIMITED,
}
}
2 changes: 1 addition & 1 deletion mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ internal class GroupImpl constructor(
} else false

require(isMiraiInternal || !message.isContentEmpty()) { "message is empty" }
check(!isBotMuted) { throw BotIsBeingMutedException(this) }
check(!isBotMuted) { throw BotIsBeingMutedException(this, message) }

val chain = broadcastMessagePreSendEvent(message, isMiraiInternal, ::GroupMessagePreSendEvent)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,13 @@ internal abstract class SendMessageHandler<C : Contact> {
if (resp is MessageSvcPbSendMsg.Response.Failed) {
val contact = contact
when (resp.resultType) {
120 -> if (contact is Group) throw BotIsBeingMutedException(contact)
120 -> if (contact is Group) throw BotIsBeingMutedException(contact, originalMessage)
121 -> if (AtAll in finalMessage) throw IllegalStateException("Send message to $contact failed, reached maximum AtAll times limit.")
299 -> if (contact is Group) throw SendMessageFailedException(
contact,
SendMessageFailedException.Reason.GROUP_CHAT_LIMITED,
originalMessage
)
}
}
check(resp is MessageSvcPbSendMsg.Response.SUCCESS) {
Expand Down