Skip to content

Commit

Permalink
feat[onebot11]: introduce brigadier: create command using mojang's br…
Browse files Browse the repository at this point in the history
…igadier lib
  • Loading branch information
RTAkland committed Dec 14, 2024
1 parent 48f39fb commit e299ae2
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ allprojects {
repositories {
mavenCentral()
maven("https://jitpack.io")
maven("https://libraries.minecraft.net")
}

tasks.compileKotlin {
Expand Down
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.5.2
libVersion=2.6.0
#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=7890
#systemProp.https.proxyHost=127.0.0.1
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
protobufVersion = "0.9.4"
kotlinVersion = "2.0.21"
kotlinVersion = "2.1.0"
gsonVersion = "2.11.0"
kotlinCoroutineVersion = "1.9.0"
javaWebsocketVersion = "1.5.7"
Expand All @@ -16,6 +16,7 @@ bcprovVersion = "1.70"
silk-codec = "0.0.5"
jlayer = "1.0.3"
jflac = "1.5.2"
brigadier = "1.0.18"

[libraries]
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gsonVersion" }
Expand All @@ -39,6 +40,7 @@ bcpg = { group = "org.bouncycastle", name = "bcpg-jdk15on", version.ref = "bcpro
silk-codec = { group = "io.github.kasukusakura", name = "silk-codec", version.ref = "silk-codec" }
jlayer = { group = "com.github.umjammer", name = "jlayer", version.ref = "jlayer" }
jflac = { group = "org.jflac", name = "jflac-codec", version.ref = "jflac" }
brigadier = { group = "com.mojang", name = "brigadier", version.ref = "brigadier" }

[bundles]

Expand Down
1 change: 1 addition & 0 deletions ronebot-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dependencies {
api(libs.slf4j.api)
api(libs.logback.classic)
api(libs.kotlin.reflect)
api(libs.brigadier)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/12/14
*/


package cn.rtast.rob.command

import cn.rtast.rob.BaseBotInstance
import com.mojang.brigadier.CommandDispatcher

interface BrigadierCommandManager<B : BaseBotInstance> {
/**
* 命令分发器
*/
val dispatcher: CommandDispatcher<B>

/**
* 所有Bot实例
*/
val botInstances: List<B>

/**
* 注册命令
*/
fun register(command: IBrigadierCommand<B>) {
command.register(dispatcher)
}

/**
* 执行命令
*/
fun execute(command: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/


package cn.rtast.rob.util
package cn.rtast.rob.command

import cn.rtast.rob.entity.IBaseCommand
import cn.rtast.rob.entity.IGroupMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/12/14
*/


package cn.rtast.rob.command

import cn.rtast.rob.BaseBotInstance
import com.mojang.brigadier.CommandDispatcher

interface IBrigadierCommand<B : BaseBotInstance> {
/**
* 注册指令
*/
fun register(dispatcher: CommandDispatcher<B>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import cn.rtast.rob.interceptor.IExecutionInterceptor
import cn.rtast.rob.onebot.OneBotListener
import cn.rtast.rob.scheduler.GlobalCoroutineScheduler
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.BrigadierCommandManagerImpl
import cn.rtast.rob.util.CommandManagerImpl
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -46,6 +47,11 @@ object ROneBotFactory : BotFactory {
*/
val commandManager = CommandManagerImpl()

/**
* 使用Brigadier来管理的指令
*/
val brigadierCommandManager = BrigadierCommandManagerImpl(botInstances)

/**
* 获取所有的Bot实例数量
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/12/14
*/


package cn.rtast.rob.util

import cn.rtast.rob.BotInstance
import cn.rtast.rob.command.IBrigadierCommand

abstract class BrigadierCommand: IBrigadierCommand<BotInstance>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/12/14
*/


package cn.rtast.rob.util

import cn.rtast.rob.BotInstance
import cn.rtast.rob.command.BrigadierCommandManager
import com.mojang.brigadier.CommandDispatcher

class BrigadierCommandManagerImpl internal constructor(
override val botInstances: List<BotInstance>
) : BrigadierCommandManager<BotInstance> {

override val dispatcher = CommandDispatcher<BotInstance>()
override fun execute(command: String) {
try {
botInstances.forEach {
dispatcher.execute(command, it)
}
} catch (_: Exception) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package cn.rtast.rob.util

import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.annotations.CommandMatchingStrategy
import cn.rtast.rob.command.CommandManager
import cn.rtast.rob.entity.*
import cn.rtast.rob.enums.MatchingStrategy
import cn.rtast.rob.interceptor.defaultInterceptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class MessageHandler(
}
listener.onGroupMessage(msg, message)
ROneBotFactory.commandManager.handleGroup(msg)
ROneBotFactory.brigadierCommandManager.execute(msg.text)
}

MessageType.private -> {
Expand Down
33 changes: 22 additions & 11 deletions ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

package test

import cn.rtast.rob.BotInstance
import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.entity.custom.ErrorEvent
import cn.rtast.rob.onebot.OneBotListener
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
import cn.rtast.rob.util.BrigadierCommand
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.builder.LiteralArgumentBuilder

class TestClient : OneBotListener {

Expand All @@ -27,19 +29,28 @@ val commands = listOf(
EchoCommand(), DelayCommand(), MatchedCommand(),
)

class TestBrigadierCommand : BrigadierCommand() {
override fun register(dispatcher: CommandDispatcher<BotInstance>) {
dispatcher.register(
LiteralArgumentBuilder.literal<BotInstance>("test")
.executes { context ->
println("executed")
0
}.then(
LiteralArgumentBuilder.literal<BotInstance>("test")
.executes { context ->
println("sub command executed")
0
}
))
}
}

suspend fun main() {
val client = TestClient()
// val wsAddress = "ws://127.0.0.1:4646"
val wsAddress = System.getenv("WS_ADDRESS")
val wsAccessToken = System.getenv("WS_ACCESS_TOKEN")
val instance1 = ROneBotFactory.createClient(wsAddress, wsAccessToken, client)
ROneBotFactory.interceptor = CustomInterceptor()
instance1.addListeningGroups(985927054)
commands.forEach {
ROneBotFactory.commandManager.register(it)
}
val task = instance1.scheduler.scheduleTask({
println("11")
}, 1.seconds, 1.minutes)
task.cancel()
ROneBotFactory.brigadierCommandManager.register(TestBrigadierCommand())
}

0 comments on commit e299ae2

Please sign in to comment.