Add Message.isContentNotEmpty

This commit is contained in:
Him188 2020-04-25 12:50:31 +08:00
parent 97dda77f04
commit e3add186fb

View File

@ -266,13 +266,27 @@ interface Message { // must be interface. Don't consider any changes.
this.followedByInternalForBinaryCompatibility(another.toString().toMessage()) this.followedByInternalForBinaryCompatibility(another.toString().toMessage())
} }
/**
* 判断消息内容是否为空.
*
* 以下情况视为 "空消息":
*
* - [MessageMetadata] (因为 [MessageMetadata.contentToString] 都必须返回空字符串)
* - [PlainText] 长度为 0
* - [MessageChain] 所有元素都满足 [isContentEmpty]
*/
@SinceMirai("0.39.3") @SinceMirai("0.39.3")
fun Message.isContentEmpty(): Boolean = when (this) { fun Message.isContentEmpty(): Boolean = when (this) {
is MessageMetadata -> true
is PlainText -> this.isEmpty() is PlainText -> this.isEmpty()
is MessageChain -> this.any { it.isContentEmpty() } is MessageChain -> this.any { it.isContentEmpty() }
else -> false else -> false
} }
@SinceMirai("0.39.3")
inline fun Message.isContentNotEmpty(): Boolean = !this.isContentEmpty()
inline fun Message.isPlain(): Boolean = this is PlainText inline fun Message.isPlain(): Boolean = this is PlainText
inline fun Message.isNotPlain(): Boolean = this !is PlainText inline fun Message.isNotPlain(): Boolean = this !is PlainText