Skip to content

Commit

Permalink
feat[onebot11]: add more message type
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Dec 14, 2024
1 parent 2316442 commit 89527d6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 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.1
libVersion=2.6.2
#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=7890
#systemProp.https.proxyHost=127.0.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,37 @@
package cn.rtast.rob.command

import cn.rtast.rob.BaseBotInstance
import cn.rtast.rob.entity.IGroupMessage
import cn.rtast.rob.entity.IMessage
import cn.rtast.rob.entity.IPrivateMessage
import cn.rtast.rob.enums.BrigadierMessageType

/**
* 指令上下文接口
*/
interface ICommandContext {
/**
* 当前Bot实例
*/
val botInstance: BaseBotInstance

/**
* 两种消息类型的父类
*/
val message: IMessage

/**
* 消息类型
*/
val messageType: BrigadierMessageType

/**
* 群聊消息(可能为空)
*/
val groupMessage: IGroupMessage?

/**
* 私聊消息(可能为空)
*/
val privateMessage: IPrivateMessage?
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package cn.rtast.rob.util

import cn.rtast.rob.BotInstance
import cn.rtast.rob.command.BrigadierCommandManager
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.entity.IMessage
import cn.rtast.rob.entity.PrivateMessage
import cn.rtast.rob.enums.BrigadierMessageType
import com.mojang.brigadier.CommandDispatcher

Expand All @@ -21,7 +23,23 @@ class BrigadierCommandManagerImpl internal constructor(
override fun execute(command: String, message: IMessage, messageType: BrigadierMessageType) {
try {
botInstances.forEach {
val context = CommandContext(it, message, messageType)
val context = when (messageType) {
BrigadierMessageType.Group -> CommandContext(
it,
message,
messageType,
message as GroupMessage,
null
)

BrigadierMessageType.Private -> CommandContext(
it,
message,
messageType,
null,
message as PrivateMessage
)
}
dispatcher.execute(command, context)
}
} catch (_: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@

package cn.rtast.rob.util

import cn.rtast.rob.BaseBotInstance
import cn.rtast.rob.BotInstance
import cn.rtast.rob.command.ICommandContext
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.entity.IMessage
import cn.rtast.rob.entity.PrivateMessage
import cn.rtast.rob.enums.BrigadierMessageType

/**
* 指令上下文
*/
data class CommandContext(
override val botInstance: BaseBotInstance,
override val botInstance: BotInstance,
override val message: IMessage,
override val messageType: BrigadierMessageType
override val messageType: BrigadierMessageType,
override val groupMessage: GroupMessage?,
override val privateMessage: PrivateMessage?
) : ICommandContext
1 change: 1 addition & 0 deletions ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TestBrigadierCommand : BrigadierCommand() {
LiteralArgumentBuilder.literal<CommandContext>("test")
.executes { context ->
scope.launch {
context.source.groupMessage?.reply("114514")
(context.source.message as GroupMessage).reply("114514")
}
0
Expand Down

0 comments on commit 89527d6

Please sign in to comment.