Ver código fonte

Add checks for empty message being sent

Him188 5 anos atrás
pai
commit
56fc3aa88b

+ 2 - 0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/FriendImpl.kt

@@ -25,6 +25,7 @@ import net.mamoe.mirai.event.events.ImageUploadEvent
 import net.mamoe.mirai.message.MessageReceipt
 import net.mamoe.mirai.message.data.Message
 import net.mamoe.mirai.message.data.OfflineFriendImage
+import net.mamoe.mirai.message.data.isContentNotEmpty
 import net.mamoe.mirai.qqandroid.QQAndroidBot
 import net.mamoe.mirai.qqandroid.network.highway.postImage
 import net.mamoe.mirai.qqandroid.network.protocol.data.proto.Cmd0x352
@@ -72,6 +73,7 @@ internal class FriendImpl(
     @JvmSynthetic
     @Suppress("DuplicatedCode")
     override suspend fun sendMessage(message: Message): MessageReceipt<Friend> {
+        require(message.isContentNotEmpty()) { "message is empty" }
         return sendMessageImpl(this, message).also {
             logMessageSent(message)
         }

+ 1 - 0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt

@@ -275,6 +275,7 @@ internal class GroupImpl(
     @OptIn(MiraiExperimentalAPI::class, LowLevelAPI::class)
     @JvmSynthetic
     override suspend fun sendMessage(message: Message): MessageReceipt<Group> {
+        require(message.isContentNotEmpty()) { "message is empty" }
         check(!isBotMuted) { throw BotIsBeingMutedException(this) }
 
         return sendMessageImpl(message, false).also {

+ 3 - 0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/MemberImpl.kt

@@ -25,6 +25,7 @@ import net.mamoe.mirai.message.MessageReceipt
 import net.mamoe.mirai.message.data.Message
 import net.mamoe.mirai.message.data.OfflineFriendImage
 import net.mamoe.mirai.message.data.asMessageChain
+import net.mamoe.mirai.message.data.isContentNotEmpty
 import net.mamoe.mirai.qqandroid.QQAndroidBot
 import net.mamoe.mirai.qqandroid.message.MessageSourceToTempImpl
 import net.mamoe.mirai.qqandroid.network.protocol.data.jce.StTroopMemberInfo
@@ -54,6 +55,8 @@ internal class MemberImpl constructor(
 
     @JvmSynthetic
     override suspend fun sendMessage(message: Message): MessageReceipt<Member> {
+        require(message.isContentNotEmpty()) { "message is empty" }
+
         return (this.asFriendOrNull()?.sendMessageImpl(this, message) ?: sendMessageImpl(message))
             .also { logMessageSent(message) }
     }

+ 1 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt

@@ -63,6 +63,7 @@ abstract class Contact : CoroutineScope, ContactJavaFriendlyAPI(), ContactOrBot
      * @throws EventCancelledException 当发送消息事件被取消时抛出
      * @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
      * @throws MessageTooLargeException 当消息过长时抛出
+     * @throws IllegalArgumentException 当消息内容为空时抛出 (详见 [Message.isContentEmpty])
      *
      * @return 消息回执. 可 [引用回复][MessageReceipt.quote](仅群聊)或 [撤回][MessageReceipt.recall] 这条消息.
      */

+ 1 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Friend.kt

@@ -63,6 +63,7 @@ abstract class Friend : QQ(), CoroutineScope {
      * @throws EventCancelledException 当发送消息事件被取消时抛出
      * @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
      * @throws MessageTooLargeException 当消息过长时抛出
+     * @throws IllegalArgumentException 当消息内容为空时抛出 (详见 [Message.isContentEmpty])
      *
      * @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
      */

+ 1 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt

@@ -145,6 +145,7 @@ abstract class Group : Contact(), CoroutineScope {
      * @throws EventCancelledException 当发送消息事件被取消时抛出
      * @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
      * @throws MessageTooLargeException 当消息过长时抛出
+     * @throws IllegalArgumentException 当消息内容为空时抛出 (详见 [Message.isContentEmpty])
      *
      * @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
      */

+ 1 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Member.kt

@@ -139,6 +139,7 @@ abstract class Member : MemberJavaFriendlyAPI() {
      * @throws EventCancelledException 当发送消息事件被取消时抛出
      * @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
      * @throws MessageTooLargeException 当消息过长时抛出
+     * @throws IllegalArgumentException 当消息内容为空时抛出 (详见 [Message.isContentEmpty])
      *
      * @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
      */

+ 1 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/User.kt

@@ -64,6 +64,7 @@ abstract class User : Contact(), CoroutineScope {
      * @throws EventCancelledException 当发送消息事件被取消时抛出
      * @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
      * @throws MessageTooLargeException 当消息过长时抛出
+     * @throws IllegalArgumentException 当消息内容为空时抛出 (详见 [Message.isContentEmpty])
      *
      * @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
      */