From 86aacc5d47958be3b76c590b74c33041f7c05a87 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 25 Apr 2020 12:40:40 +0800 Subject: [PATCH] Add `Message.isContentEmpty` --- .../kotlin/net.mamoe.mirai/message/data/Message.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt index 79e0cd627..9e4a533a7 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt @@ -57,6 +57,11 @@ import kotlin.jvm.JvmSynthetic * #### 实现规范 * 除 [MessageChain] 外, 所有 [Message] 的实现类都有伴生对象实现 [Key] 接口. * + * #### [CharSequence] 继承 + * 所有 [CharSequence] 的行为均由 [toString] 委托. + * + * 即, `appendable.append(message)` 相当于 `appendable.append(message.toString())` + * * @see PlainText 纯文本 * @see Image 图片 * @see Face 原生表情 @@ -261,6 +266,13 @@ interface Message { // must be interface. Don't consider any changes. this.followedByInternalForBinaryCompatibility(another.toString().toMessage()) } +@SinceMirai("0.39.3") +fun Message.isContentEmpty(): Boolean = when (this) { + is PlainText -> this.isEmpty() + is MessageChain -> this.any { it.isContentEmpty() } + else -> false +} + inline fun Message.isPlain(): Boolean = this is PlainText inline fun Message.isNotPlain(): Boolean = this !is PlainText