Introduce BotIsBeingMutedException to replace IllegalStateException

This commit is contained in:
Him188 2020-04-04 14:17:32 +08:00
parent e8b0c99006
commit 3cc5cbfcfd
19 changed files with 101 additions and 61 deletions

View File

@ -259,16 +259,16 @@ internal class GroupImpl(
override operator fun get(id: Long): Member {
return members.delegate.firstOrNull { it.id == id }
return members.firstOrNull { it.id == id }
?: throw NoSuchElementException("member $id not found in group $uin")
}
override fun contains(id: Long): Boolean {
return members.delegate.firstOrNull { it.id == id } != null
return members.firstOrNull { it.id == id } != null
}
override fun getOrNull(id: Long): Member? {
return members.delegate.firstOrNull { it.id == id }
return members.firstOrNull { it.id == id }
}
@OptIn(MiraiExperimentalAPI::class, LowLevelAPI::class)

View File

@ -81,7 +81,7 @@ internal class MessageSourceFromMsg(
} else toJceDataImplForGroup()
}
val elems by lazy {
private val elems by lazy {
delegate.msgBody.richText.elems.toMutableList().also {
if (it.last().elemFlags2 == null) it.add(ImMsgBody.Elem(elemFlags2 = ImMsgBody.ElemFlags2()))
}
@ -256,7 +256,7 @@ internal class MessageSourceFromSendGroup(
override val groupId: Long,
override val originalMessage: MessageChain
) : MessageSourceFromSend() {
internal lateinit var sequenceIdDeferred: Deferred<Int>
private lateinit var sequenceIdDeferred: Deferred<Int>
@OptIn(ExperimentalCoroutinesApi::class)
override val id: Long

View File

@ -67,7 +67,7 @@ internal inline fun <T> Iterable<T>.sumUpTo(upTo: Int, selector: (T, remaining:
}
internal inline fun CharSequence.sumUpTo(upTo: Int, selector: (Char) -> Int): Int {
var sum: Int = 0
var sum = 0
for (element in this) {
sum += selector(element)
if (sum >= upTo) {

View File

@ -60,7 +60,7 @@ actual abstract class Contact : CoroutineScope, ContactJavaFriendlyAPI() {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. [引用回复][MessageReceipt.quote]仅群聊 [撤回][MessageReceipt.recall] 这条消息.

View File

@ -54,7 +54,7 @@ actual abstract class ContactJavaFriendlyAPI {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出 * @throws MessageTooLargeException 当消息过长时抛出
*

View File

@ -131,7 +131,7 @@ actual abstract class Group : Contact(), CoroutineScope {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])

View File

@ -116,7 +116,7 @@ actual abstract class Member : MemberJavaFriendlyAPI() {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])

View File

@ -84,7 +84,7 @@ actual abstract class QQ : Contact(), CoroutineScope {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])

View File

@ -63,7 +63,7 @@ expect abstract class Contact() : CoroutineScope, ContactJavaFriendlyAPI {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. [引用回复][MessageReceipt.quote]仅群聊 [撤回][MessageReceipt.recall] 这条消息.

View File

@ -7,11 +7,12 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("EXPERIMENTAL_API_USAGE")
@file:Suppress("EXPERIMENTAL_API_USAGE", "unused")
package net.mamoe.mirai.contact
import net.mamoe.mirai.utils.*
import kotlin.jvm.JvmName
/**
@ -21,17 +22,8 @@ import net.mamoe.mirai.utils.*
*/
@OptIn(MiraiInternalAPI::class)
@Suppress("unused")
class ContactList<C : Contact>(@MiraiInternalAPI val delegate: LockFreeLinkedList<C>) {
/**
* ID 列表的字符串表示.
* :
* ```
* [123456, 321654, 123654]
* ```
*/
val idContentString: String get() = "[" + buildString { delegate.forEach { append(it.id).append(", ") } }.dropLast(2) + "]"
operator fun get(id: Long): C = delegate[id]
class ContactList<C : Contact>(@MiraiInternalAPI val delegate: LockFreeLinkedList<C>) : Iterable<C> {
operator fun get(id: Long): C = delegate.asSequence().first { it.id == id }
fun getOrNull(id: Long): C? = delegate.getOrNull(id)
val size: Int get() = delegate.size
@ -39,21 +31,53 @@ class ContactList<C : Contact>(@MiraiInternalAPI val delegate: LockFreeLinkedLis
operator fun contains(id: Long): Boolean = delegate.getOrNull(id) != null
fun containsAll(elements: Collection<C>): Boolean = elements.all { contains(it) }
fun isEmpty(): Boolean = delegate.isEmpty()
override fun toString(): String =
delegate.asSequence().joinToString(separator = ", ", prefix = "ContactList(", postfix = ")")
override fun iterator(): Iterator<C> {
return this.delegate.asSequence().iterator()
}
@PlannedRemoval("1.0.0")
@Suppress("PropertyName")
@get:JvmName("getIdContentString")
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
val _idContentString: String
get() = this.idContentString
@PlannedRemoval("1.0.0")
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
inline fun forEach(block: (C) -> Unit) = delegate.forEach(block)
@PlannedRemoval("1.0.0")
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
fun first(): C {
forEach { return it }
throw NoSuchElementException()
}
@PlannedRemoval("1.0.0")
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
fun firstOrNull(): C? {
forEach { return it }
return null
}
override fun toString(): String =
delegate.asSequence().joinToString(separator = ", ", prefix = "ContactList(", postfix = ")")
}
/**
* ID 列表的字符串表示.
* :
* ```
* [123456, 321654, 123654]
* ```
*/
val ContactList<*>.idContentString: String
get() = "[" + @OptIn(MiraiInternalAPI::class) buildString { delegate.forEach { append(it.id).append(", ") } }.dropLast(
2
) + "]"
operator fun <C : Contact> LockFreeLinkedList<C>.get(id: Long): C {
forEach { if (it.id == id) return it }
throw NoSuchElementException("No such contact: $id")
@ -64,46 +88,45 @@ fun <C : Contact> LockFreeLinkedList<C>.getOrNull(id: Long): C? {
return null
}
@PlannedRemoval("1.0.0")
@Deprecated(
"use firstOrNull from stdlib",
replaceWith = ReplaceWith("this.asSequence().firstOrNull(filter)"),
level = DeprecationLevel.ERROR
)
inline fun <C : Contact> LockFreeLinkedList<C>.firstOrNull(filter: (C) -> Boolean): C? {
forEach { if (filter(it)) return it }
return null
}
@PlannedRemoval("1.0.0")
@Deprecated("use firstOrNull", replaceWith = ReplaceWith("firstOrNull(filter)"), level = DeprecationLevel.ERROR)
inline fun <C : Contact> LockFreeLinkedList<C>.filteringGetOrNull(filter: (C) -> Boolean): C? {
return this.firstOrNull(filter)
}
@Deprecated(
"use firstOrNull from stdlib",
replaceWith = ReplaceWith("firstOrNull(filter)"),
level = DeprecationLevel.ERROR
)
inline fun <C : Contact> LockFreeLinkedList<C>.filteringGetOrNull(filter: (C) -> Boolean): C? =
this.asSequence().firstOrNull(filter)
/**
* Collect all the elements into a [MutableList] then cast it as a [List]
*/
@PlannedRemoval("1.0.0")
@Deprecated("use Iterator.toList from stdlib", level = DeprecationLevel.HIDDEN)
fun <E : Contact> ContactList<E>.toList(): List<E> = toMutableList()
/**
* Collect all the elements into a [MutableList].
*/
@PlannedRemoval("1.0.0")
@Deprecated("use Iterator.toMutableList from stdlib", level = DeprecationLevel.HIDDEN)
@OptIn(MiraiInternalAPI::class)
fun <E : Contact> ContactList<E>.toMutableList(): MutableList<E> = this.delegate.toMutableList()
/**
* Collect all the elements into a [MutableSet] then cast it as a [Set]
*/
@PlannedRemoval("1.0.0")
@Deprecated("use Iterator.toSet from stdlib", level = DeprecationLevel.HIDDEN)
fun <E : Contact> ContactList<E>.toSet(): Set<E> = toMutableSet()
/**
* Collect all the elements into a [MutableSet].
*/
@PlannedRemoval("1.0.0")
@Deprecated("use Iterator.toMutableSet from stdlib", level = DeprecationLevel.HIDDEN)
@OptIn(MiraiInternalAPI::class)
fun <E : Contact> ContactList<E>.toMutableSet(): MutableSet<E> = this.delegate.toMutableSet()
/**
* Builds a [Sequence] containing all the elements in [this] in the same order.
*
* Note that the sequence is dynamic, that is, elements are yielded atomically only when it is required
*/
@PlannedRemoval("1.0.0")
@Deprecated("use Iterator.asSequence from stdlib", level = DeprecationLevel.HIDDEN)
@OptIn(MiraiInternalAPI::class)
fun <E : Contact> ContactList<E>.asSequence(): Sequence<E> {
return this.delegate.asSequence()
}
fun <E : Contact> ContactList<E>.asSequence(): Sequence<E> = this.delegate.asSequence()

View File

@ -135,7 +135,7 @@ expect abstract class Group() : Contact, CoroutineScope {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])

View File

@ -138,7 +138,7 @@ expect abstract class Member() : MemberJavaFriendlyAPI {
* @see MessageSendEvent.GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])

View File

@ -16,6 +16,8 @@ import net.mamoe.mirai.utils.SinceMirai
/**
* 发送消息时消息过长抛出的异常.
*
* @see Contact.sendMessage
*/
@SinceMirai("0.32.0")
class MessageTooLargeException(
@ -29,4 +31,19 @@ class MessageTooLargeException(
*/
val messageAfterEvent: Message,
exceptionMessage: String
) : RuntimeException(exceptionMessage)
) : RuntimeException(exceptionMessage)
/**
* 发送消息时 bot 正处于被禁言状态时抛出的异常.
*
* @see Group.sendMessage
*/
@SinceMirai("0.33.0")
class BotIsBeingMutedException(
val target: Group,
/**
* 被禁言剩余时间
* @see Group.botMuteRemaining
*/
val remainingMillis: Int
) : RuntimeException()

View File

@ -93,7 +93,7 @@ expect abstract class QQ() : Contact, CoroutineScope {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])

View File

@ -59,7 +59,7 @@ actual abstract class Contact : CoroutineScope, ContactJavaFriendlyAPI() {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. [引用回复][MessageReceipt.quote]仅群聊 [撤回][MessageReceipt.recall] 这条消息.

View File

@ -54,7 +54,7 @@ actual abstract class ContactJavaFriendlyAPI {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. [引用回复][MessageReceipt.quote]仅群聊 [撤回][MessageReceipt.recall] 这条消息.

View File

@ -133,7 +133,7 @@ actual abstract class Group : Contact(), CoroutineScope {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])

View File

@ -124,7 +124,7 @@ actual abstract class Member : MemberJavaFriendlyAPI() {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])

View File

@ -84,7 +84,7 @@ actual abstract class QQ : Contact(), CoroutineScope {
* @see GroupMessageSendEvent 发送群消息事件. cancellable
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws IllegalStateException 发送群消息时若 [Bot] 被禁言抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])