Skip to content

Commit

Permalink
fix: fix message chain ext method not work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Nov 8, 2024
1 parent b314900 commit c72dbf5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 158 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
libVersion=2.4.2
libVersion=2.4.3
37 changes: 1 addition & 36 deletions onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,42 +290,7 @@ class MessageChain internal constructor(arrayMessageList: MutableList<InternalBa
* 追加一个[Segment], 使用when+隐式类型转换实现追加
*/
fun addSegment(segment: Segment): Builder {
when (segment) {
is Text -> 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 {
Expand Down
167 changes: 58 additions & 109 deletions onebot/src/main/kotlin/cn/rtast/rob/util/ob/MessageChainExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,29 +53,13 @@ fun Collection<MessageChain>.toNode(senderId: Long): NodeMessageChain {
return node.build()
}

/**
* 将一个数组([Array])的[MessageChain.Builder]对象转换成合并转发消息链([NodeMessageChain.Builder])
* 并且返回已经构造的[NodeMessageChain.Builder]
*/
fun Array<MessageChain.Builder>.toNode(senderId: Long): NodeMessageChain.Builder {
return this.toList().toNode(senderId)
}

/**
* 将一个数组([Array])的[MessageChain]对象转换成合并转发消息链([NodeMessageChain])
* 并且返回已经构造的[NodeMessageChain]
*/
fun Array<MessageChain>.toNode(senderId: Long): NodeMessageChain {
return this.toList().toNode(senderId)
}

/**
* 将一个任意类型的集合[Collection]转换为一个构造好的消息链
* 并且接收一个参数表示是否在每个元素之后插入一个换行符
* ***注意: 元素必须重写了[toString]方法, 如果一个元素没有重写[toString]方法则会使用这个元素的内存地址***
*/
@JvmOverloads
fun <T> Collection<T?>.toMessageChain(newLine: Boolean = false, filterNull: Boolean = false): MessageChain {
fun <T> Collection<T?>.toMessageChainString(newLine: Boolean = false, filterNull: Boolean = false): MessageChain {
val msg = MessageChain.Builder()
if (filterNull) this.filter { it != null }.forEach {
msg.addText(it.toString())
Expand All @@ -89,35 +73,11 @@ fun <T> Collection<T?>.toMessageChain(newLine: Boolean = false, filterNull: Bool
return msg.build()
}

/**
* 将任意类型的数据转换成[MessageChain], 但是最终都会调用这个类型的[toString]方法
*/
fun <T> T?.toMessageChain() = MessageChain.Builder().addText(this.toString()).build()

/**
* 将任意类型的数据转换成[MessageChain.Builder], 但是最终都会调用这个类型的[toString]方法
*/
fun <T> T?.toMessageChainBuilder() = MessageChain.Builder().addText(this.toString())

/**
* 将一个任意类型的数组[Array]转换为一个构造完成的消息链
* 接收一个参数表示是否在每个元素之后插入换行符
*/
@JvmOverloads
fun <T> Array<T>.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 <T> Collection<T>.toMessageChainBuilder(newLine: Boolean = false): MessageChain.Builder {
fun <T> Collection<T>.toMessageChainBuilderString(newLine: Boolean = false): MessageChain.Builder {
val msgBuilder = MessageChain.Builder()
this.forEach {
msgBuilder.addText(it.toString())
Expand All @@ -127,46 +87,83 @@ fun <T> Collection<T>.toMessageChainBuilder(newLine: Boolean = false): MessageCh
}

/**
* 将任意类型的数组[Array]转换为未构造的消息连构造器
* 将任意类型的数据转换成[MessageChain], 但是最终都会调用这个类型的[toString]方法
*/
@JvmOverloads
fun <T> Array<T>.toMessageChainBuilder(newLine: Boolean = false): MessageChain.Builder {
val msgBuilder = MessageChain.Builder()
this.forEach {
msgBuilder.addText(it.toString())
if (newLine) msgBuilder.addNewLine()
}
return msgBuilder
fun <T> T?.toMessageChain() = MessageChain.Builder().addText(this.toString()).build()

/**
* 将一个[Collection] [Segment]转换成[MessageChain]
*/
fun Collection<Segment>.toMessageChain() = this.toMessageChainBuilder().build()

/**
* 将一个[Collection] [Segment]转换成[MessageChain.Builder]
*/
fun Collection<Segment>.toMessageChainBuilder(): MessageChain.Builder {
var msg = MessageChain.Builder()
var b = MessageChain.Builder()
this.forEach { b = it.plusMessageChain(msg) }
return b
}

/**
* 使两个[MessageChain]对象可以快速拼接起来合并成一个
* 完整的[MessageChain]
*/
operator fun MessageChain.plus(other: MessageChain): MessageChain {
return this.finalArrayMsgList.addAll(other.finalArrayMsgList).toMessageChain()
this.finalArrayMsgList.addAll(other.finalArrayMsgList)
return this.finalArrayMsgList.toMessageChainInternal()
}

/**
* 使两个[MessageChain.Builder]对象可以快速拼接起来合并成一个
* 完整的[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<InternalBaseSegment>.toMessageChainInternal() = this.toMessageChainBuilderInternal().build()

/**
* 内部使用
* 将一个[Collection] [InternalBaseSegment] 转换成[MessageChain.Builder]
*/
internal fun Collection<InternalBaseSegment>.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)
Expand All @@ -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()
}
14 changes: 5 additions & 9 deletions onebot/src/test/kotlin/test/MessageChainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
15 changes: 12 additions & 3 deletions onebot/src/test/kotlin/test/TestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c72dbf5

Please sign in to comment.