From c72dbf53e69334eeebcc6d932c36a54bf5aa87e1 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Fri, 8 Nov 2024 12:17:38 +0800 Subject: [PATCH] fix: fix message chain ext method not work properly --- gradle.properties | 2 +- .../cn/rtast/rob/util/ob/MessageChain.kt | 37 +--- .../cn/rtast/rob/util/ob/MessageChainExt.kt | 167 ++++++------------ .../src/test/kotlin/test/MessageChainTest.kt | 14 +- onebot/src/test/kotlin/test/TestClient.kt | 15 +- 5 files changed, 77 insertions(+), 158 deletions(-) diff --git a/gradle.properties b/gradle.properties index 49b828a..0158650 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ kotlin.code.style=official -libVersion=2.4.2 \ No newline at end of file +libVersion=2.4.3 \ No newline at end of file diff --git a/onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChain.kt b/onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChain.kt index 180fb46..2b06a8a 100644 --- a/onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChain.kt +++ b/onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChain.kt @@ -290,42 +290,7 @@ class MessageChain internal constructor(arrayMessageList: MutableList addText(segment.text) - is AT -> addAt(segment.qq) - is Face -> addFace(segment.id) - is Image -> addImage(segment.file) - is Record -> addRecord(segment.file) - is Video -> addVideo(segment.file) - is Poke -> addPoke(segment.poke) - is Reply -> addReply(segment.id.toLong()) - is XML -> addXML(segment.xml) - is FriendContact -> addContactFriend(segment.id.toLong()) - is GroupContact -> addContactGroup(segment.id.toLong()) - is JSON -> addJSON(segment.json) - is MusicShare -> addMusicShare(segment.type, segment.id) - is Rps -> addRPS() - is Dice -> addDice() - is Shake -> addShake() - is Share -> addShare(segment.url, segment.title, segment.content, segment.image) - is NewLine -> addNewLine(segment.times) - is QFace -> addFace(segment.id) - is Location -> addLocation( - segment.lat.toDouble(), - segment.lon.toDouble(), - segment.title, - segment.content - ) - - is CustomMusicShare -> addCustomMusicShare( - segment.url, - segment.audio, - segment.title, - segment.content, - segment.image - ) - } - return this + return segment.plusMessageChain(this) } fun build(): MessageChain { diff --git a/onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChainExt.kt b/onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChainExt.kt index 9670344..dce85c7 100644 --- a/onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChainExt.kt +++ b/onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChainExt.kt @@ -14,8 +14,8 @@ import cn.rtast.rob.segment.Dice import cn.rtast.rob.segment.Face import cn.rtast.rob.segment.FriendContact import cn.rtast.rob.segment.GroupContact -import cn.rtast.rob.segment.IPlainText import cn.rtast.rob.segment.Image +import cn.rtast.rob.segment.InternalBaseSegment import cn.rtast.rob.segment.JSON import cn.rtast.rob.segment.Location import cn.rtast.rob.segment.MusicShare @@ -53,29 +53,13 @@ fun Collection.toNode(senderId: Long): NodeMessageChain { return node.build() } -/** - * 将一个数组([Array])的[MessageChain.Builder]对象转换成合并转发消息链([NodeMessageChain.Builder]) - * 并且返回已经构造的[NodeMessageChain.Builder] - */ -fun Array.toNode(senderId: Long): NodeMessageChain.Builder { - return this.toList().toNode(senderId) -} - -/** - * 将一个数组([Array])的[MessageChain]对象转换成合并转发消息链([NodeMessageChain]) - * 并且返回已经构造的[NodeMessageChain] - */ -fun Array.toNode(senderId: Long): NodeMessageChain { - return this.toList().toNode(senderId) -} - /** * 将一个任意类型的集合[Collection]转换为一个构造好的消息链 * 并且接收一个参数表示是否在每个元素之后插入一个换行符 * ***注意: 元素必须重写了[toString]方法, 如果一个元素没有重写[toString]方法则会使用这个元素的内存地址*** */ @JvmOverloads -fun Collection.toMessageChain(newLine: Boolean = false, filterNull: Boolean = false): MessageChain { +fun Collection.toMessageChainString(newLine: Boolean = false, filterNull: Boolean = false): MessageChain { val msg = MessageChain.Builder() if (filterNull) this.filter { it != null }.forEach { msg.addText(it.toString()) @@ -89,35 +73,11 @@ fun Collection.toMessageChain(newLine: Boolean = false, filterNull: Bool return msg.build() } -/** - * 将任意类型的数据转换成[MessageChain], 但是最终都会调用这个类型的[toString]方法 - */ -fun T?.toMessageChain() = MessageChain.Builder().addText(this.toString()).build() - -/** - * 将任意类型的数据转换成[MessageChain.Builder], 但是最终都会调用这个类型的[toString]方法 - */ -fun T?.toMessageChainBuilder() = MessageChain.Builder().addText(this.toString()) - -/** - * 将一个任意类型的数组[Array]转换为一个构造完成的消息链 - * 接收一个参数表示是否在每个元素之后插入换行符 - */ -@JvmOverloads -fun Array.toMessageChain(newLine: Boolean = false): MessageChain { - val msg = MessageChain.Builder() - this.forEach { - msg.addText(it.toString()) - if (newLine) msg.addNewLine() - } - return msg.build() -} - /** * 将任意类型的集合[Collection]转换为未构造的消息链构造器 */ @JvmOverloads -fun Collection.toMessageChainBuilder(newLine: Boolean = false): MessageChain.Builder { +fun Collection.toMessageChainBuilderString(newLine: Boolean = false): MessageChain.Builder { val msgBuilder = MessageChain.Builder() this.forEach { msgBuilder.addText(it.toString()) @@ -127,16 +87,23 @@ fun Collection.toMessageChainBuilder(newLine: Boolean = false): MessageCh } /** - * 将任意类型的数组[Array]转换为未构造的消息连构造器 + * 将任意类型的数据转换成[MessageChain], 但是最终都会调用这个类型的[toString]方法 */ -@JvmOverloads -fun Array.toMessageChainBuilder(newLine: Boolean = false): MessageChain.Builder { - val msgBuilder = MessageChain.Builder() - this.forEach { - msgBuilder.addText(it.toString()) - if (newLine) msgBuilder.addNewLine() - } - return msgBuilder +fun T?.toMessageChain() = MessageChain.Builder().addText(this.toString()).build() + +/** + * 将一个[Collection] [Segment]转换成[MessageChain] + */ +fun Collection.toMessageChain() = this.toMessageChainBuilder().build() + +/** + * 将一个[Collection] [Segment]转换成[MessageChain.Builder] + */ +fun Collection.toMessageChainBuilder(): MessageChain.Builder { + var msg = MessageChain.Builder() + var b = MessageChain.Builder() + this.forEach { b = it.plusMessageChain(msg) } + return b } /** @@ -144,7 +111,8 @@ fun Array.toMessageChainBuilder(newLine: Boolean = false): MessageChain.B * 完整的[MessageChain] */ operator fun MessageChain.plus(other: MessageChain): MessageChain { - return this.finalArrayMsgList.addAll(other.finalArrayMsgList).toMessageChain() + this.finalArrayMsgList.addAll(other.finalArrayMsgList) + return this.finalArrayMsgList.toMessageChainInternal() } /** @@ -152,21 +120,50 @@ operator fun MessageChain.plus(other: MessageChain): MessageChain { * 完整的[MessageChain.Builder] */ operator fun MessageChain.Builder.plus(other: MessageChain.Builder): MessageChain.Builder { - return this.arrayMessageList.addAll(other.arrayMessageList).toMessageChainBuilder() + this.addRawArrayMessage(other.arrayMessageList) + return this.arrayMessageList.toMessageChainBuilderInternal() +} + +/** + * 将一个手动构造的Segment拼接成一个MessageChain + */ +operator fun Segment.plus(other: Segment): MessageChain { + return other.plusMessageChain(this.plusMessageChain(MessageChain.Builder())).build() +} + +/** + * 对一个[MessageChain]对象使用+操作符拼接[Segment] + * 这个操作符的使用是为了连接[Segment.plus]方法 + */ +operator fun MessageChain.plus(segment: Segment): MessageChain { + val newBuilder = MessageChain.Builder() + newBuilder.arrayMessageList.addAll(this.finalArrayMsgList) + newBuilder.addSegment(segment) + return newBuilder.build() } /** - * 对一个构造好的[MessageChain]使用+操作符快速添加一个[IPlainText]类型的消息段 + * 内部使用 + * 将一个[Collection] [InternalBaseSegment] 转换成[MessageChain] */ -operator fun MessageChain.plus(other: Any): MessageChain { - return this.finalArrayMsgList.add(IPlainText(IPlainText.Data(other.toString()))).toMessageChain() +internal fun Collection.toMessageChainInternal() = this.toMessageChainBuilderInternal().build() + +/** + * 内部使用 + * 将一个[Collection] [InternalBaseSegment] 转换成[MessageChain.Builder] + */ +internal fun Collection.toMessageChainBuilderInternal(): MessageChain.Builder { + val builder = MessageChain.Builder() + builder.addRawArrayMessage(this as List) + return builder } /** - * 将一个手动构造的Segment拼接成一个MessageChain + * 内部使用 + * 传入一个[MessageChain.Builder] 将[Segment]追加到[MessageChain.Builder]最后返回 + * [MessageChain.Builder] */ -operator fun Segment.plus(other: Segment): MessageChain { - val msg = MessageChain.Builder() +internal fun Segment.plusMessageChain(msg: MessageChain.Builder): MessageChain.Builder { when (this) { is Text -> msg.addText(this.text) is AT -> msg.addAt(this.qq) @@ -190,53 +187,5 @@ operator fun Segment.plus(other: Segment): MessageChain { is NewLine -> msg.addNewLine(this.times) is QFace -> msg.addFace(this.id) } - when (other) { - is Text -> msg.addText(other.text) - is AT -> msg.addAt(other.qq) - is Face -> msg.addFace(other.id) - is Image -> msg.addImage(other.file) - is Record -> msg.addRecord(other.file) - is Video -> msg.addVideo(other.file) - is Poke -> msg.addPoke(other.poke) - is Reply -> msg.addReply(other.id) - is XML -> msg.addXML(other.xml) - is FriendContact -> msg.addContactFriend(other.id) - is GroupContact -> msg.addContactGroup(other.id) - is JSON -> msg.addJSON(other.json) - is MusicShare -> msg.addMusicShare(other.type, other.id) - is Rps -> msg.addRPS() - is Dice -> msg.addDice() - is Shake -> msg.addShake() - is Share -> msg.addShare(other.url, other.title, other.content, other.image) - is Location -> msg.addLocation(other.lat, other.lon, other.title, other.content) - is CustomMusicShare -> msg.addCustomMusicShare(other.url, other.audio, other.title, other.content, other.image) - is NewLine -> msg.addNewLine(other.times) - is QFace -> msg.addFace(other.id) - } - return msg.build() -} - -/** - * 对一个[MessageChain]对象使用+操作符拼接[Segment] - * 这个操作符的使用是为了连接[Segment.plus]方法 - */ -operator fun MessageChain.plus(segment: Segment): MessageChain { - val newBuilder = MessageChain.Builder() - newBuilder.arrayMessageList.addAll(this.finalArrayMsgList) - newBuilder.addSegment(segment) - return newBuilder.build() + return msg } - -/** - * [Segment]+[String]拼接 - */ -operator fun Segment.plus(other: String): MessageChain { - return MessageChain.Builder().addSegment(this).addText(other).build() -} - -/** - * [String]+[Segment]拼接 - */ -operator fun String.plus(other: Segment): MessageChain { - return MessageChain.Builder().addText(this).addSegment(other).build() -} \ No newline at end of file diff --git a/onebot/src/test/kotlin/test/MessageChainTest.kt b/onebot/src/test/kotlin/test/MessageChainTest.kt index 1ef9a8c..8a9a761 100644 --- a/onebot/src/test/kotlin/test/MessageChainTest.kt +++ b/onebot/src/test/kotlin/test/MessageChainTest.kt @@ -10,22 +10,18 @@ import cn.rtast.rob.segment.AT import cn.rtast.rob.segment.Face import cn.rtast.rob.segment.Image import cn.rtast.rob.segment.NewLine -import cn.rtast.rob.segment.Reply import cn.rtast.rob.segment.Text -import cn.rtast.rob.util.ob.MessageChain import cn.rtast.rob.util.ob.plus +import cn.rtast.rob.util.ob.toMessageChain fun main() { - // 可以直接对一个链式调用构造的消息使用操作符重载 - val chain = MessageChain.Builder().addText("1").build() + Text("") - println(chain) val operator = AT(3458671395) + Text("114514") + - Image("https://example.com/example.png") + - Reply(114514L) + + Image("https://static.rtast.cn/images/%E5%8F%88%E6%8B%8D%E4%BA%91_logo2.png") + Face(666) + - NewLine() + - "" + NewLine() + val a = listOf(Text("1"), Text("2"), Text("3"), Text("4")).toMessageChain() + println(a) println(operator) } diff --git a/onebot/src/test/kotlin/test/TestClient.kt b/onebot/src/test/kotlin/test/TestClient.kt index d43c3d9..35876cb 100644 --- a/onebot/src/test/kotlin/test/TestClient.kt +++ b/onebot/src/test/kotlin/test/TestClient.kt @@ -9,15 +9,24 @@ package test import cn.rtast.rob.ROneBotFactory import cn.rtast.rob.entity.* import cn.rtast.rob.entity.custom.ErrorEvent +import cn.rtast.rob.segment.AT +import cn.rtast.rob.segment.Face import cn.rtast.rob.segment.Image +import cn.rtast.rob.segment.NewLine +import cn.rtast.rob.segment.Text import cn.rtast.rob.util.ob.OneBotListener +import cn.rtast.rob.util.ob.plus class TestClient : OneBotListener { override suspend fun onGroupMessage(message: GroupMessage, json: String) { - val image = message.images.first() - val outboundImage = Image(image.file, false) - message.reply(outboundImage) + message.reply( + AT(3458671395) + + Text("114514") + + Image("https://static.rtast.cn/images/%E5%8F%88%E6%8B%8D%E4%BA%91_logo2.png") + + Face(666) + + NewLine() + ) } override suspend fun onWebsocketErrorEvent(event: ErrorEvent) {