Skip to content

Commit

Permalink
feat[onebot11]: add dsl messageChain builder
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Jan 27, 2025
1 parent ee914ef commit 25f1574
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
libVersion=2.6.6
libVersion=2.6.7

#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=12334
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import cn.rtast.rob.enums.PokeMessage
import cn.rtast.rob.enums.QQFace
import cn.rtast.rob.enums.internal.ContactType
import cn.rtast.rob.segment.*
import kotlin.reflect.KClass

/**
* 快速构造一个数组形式的消息链
Expand Down Expand Up @@ -313,12 +312,43 @@ class MessageChain internal constructor(arrayMessageList: MutableList<InternalBa
return this
}

/**
* 添加Markdown
*/
@Deprecated(level = DeprecationLevel.HIDDEN, message = "Can't be used")
fun addMarkdown(content: String): Builder {
arrayMessageList.add(IMarkdown(IMarkdown.Data(content)))
return this
}

/**
* 用于dsl的方式添加[Segment], 使用下面的方式调用
* ```kotlin
* messageChain {
* +Text("22222")
* +Image("https://a.com/a.png")
* }
* ```
*/
operator fun Segment.unaryPlus() {
addSegment(this)
}

/**
* 添加任意的[Segment]
*/
fun add(segment: Segment) = this.addSegment(segment)

/**
* 用于dsl的方式添加[Segment]
* 使用下面两种方式来调用
* ```kotlin
* this(Text("test1"))
* invoke(Image("https://example.com/example.png"))
* ```
*/
operator fun invoke(segment: Segment) = this.addSegment(segment)

fun build(): MessageChain {
return MessageChain(arrayMessageList)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ class OneBotAction internal constructor(
}
}

/**
* 向群聊中发送[Segment]
*/
suspend fun sendGroupMessage(groupId: Long, content: Segment): Long? {
return this.sendGroupMessage(groupId, content.toMessageChain())
}

/**
* 向一个群聊中发送一段纯文本消息
*/
Expand All @@ -184,6 +191,13 @@ class OneBotAction internal constructor(
return this.sendGroupForwardMsg(groupId, content)
}

/**
* 异步的向群聊中发送[Segment]
*/
suspend fun sendGroupMessageAsync(groupId: Long, content: Segment) {
this.sendGroupMessageAsync(groupId, content.toMessageChain())
}

/**
* 用重载函数的方式将发送合并转发消息的接口包装成发送普通
* 消息的接口, 但是使用异步发送
Expand Down Expand Up @@ -276,6 +290,13 @@ class OneBotAction internal constructor(
)
}

/**
* 向好友发送[Segment]
*/
suspend fun sendPrivateMessage(userId: Long, content: Segment): Long? {
return this.sendPrivateMessage(userId, content.toMessageChain())
}

/**
* 发送私聊消息但是是纯文本
*/
Expand Down Expand Up @@ -386,6 +407,13 @@ class OneBotAction internal constructor(
)
}

/**
* 异步向好友发送[Segment]
*/
suspend fun sendPrivateMessageAsync(userId: Long, content: Segment) {
this.sendPrivateMessageAsync(userId, content.toMessageChain())
}

/**
* 撤回消息(recall/revoke)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/27
*/


package cn.rtast.rob.onebot.sdl

import cn.rtast.rob.onebot.MessageChain

/**
* dsl 风格的构造一个[MessageChain]
*/
inline fun messageChain(builder: MessageChain.Builder.() -> Unit) =
MessageChain.Builder().apply(builder).build()

19 changes: 8 additions & 11 deletions ronebot-onebot-v11/src/test/kotlin/test/MessageChainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@

package test

import cn.rtast.rob.onebot.MessageChain
import cn.rtast.rob.onebot.sdl.messageChain
import cn.rtast.rob.segment.Text
import cn.rtast.rob.segment.toMessageChain
import cn.rtast.rob.segment.plus


fun main() {
println(Text("1") + Text("2"))
val l1 = listOf(Text("1"), Text("2")).apply { this + (Text("3")) }
println(MessageChain.Builder().build() + Text("1"))
val l2 = listOf(Text("3"), Text("4"))
val a = listOf(Text("1"), Text("2"), Text("3"), Text("4")).toMessageChain()
println(a)
println(Text("1") + Text("2") + Text("3") + "" + Text("4"))
Text("11") + MessageChain.Builder().build()
val msg = messageChain {
addText("Hello World")
this(Text("1111"))
invoke(Text("2222"))
+Text("22222")
add(Text("22222"))
}
}
10 changes: 10 additions & 0 deletions ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.entity.custom.ErrorEvent
import cn.rtast.rob.enums.QQFace
import cn.rtast.rob.onebot.OneBotListener
import cn.rtast.rob.onebot.sdl.invoke
import cn.rtast.rob.onebot.sdl.messageChain
import cn.rtast.rob.segment.Text
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.BrigadierCommand
import cn.rtast.rob.util.CommandSource
Expand All @@ -29,6 +32,13 @@ class TestClient : OneBotListener {

override suspend fun onGroupMessage(message: GroupMessage, json: String) {
println(message)
val msg = messageChain {
addText("Hello World")
this(Text("1111"))
invoke(Text("2222"))
+Text("22222")
}
message.reply(msg)
}

override suspend fun onWebsocketErrorEvent(event: ErrorEvent) {
Expand Down

0 comments on commit 25f1574

Please sign in to comment.