feat: support kotlinc command

This commit is contained in:
2024-09-12 22:45:07 +08:00
parent 31c8455666
commit d286613f01
4 files changed
+89 -1

No files matched your search

@@ -128,7 +128,8 @@ val commands = listOf(
RemakeCommand(), PixivCommand(),
RUACommand(), NslookupCommand(),
NiuziSignCommand(), MyNiuziCommand(),
JiJianCommand(), LikeMeCommand()
JiJianCommand(), LikeMeCommand(),
KotlinCCommand()
)
suspend fun main() {
@@ -0,0 +1,45 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/12
*/
package cn.rtast.fancybot.commands
import cn.rtast.fancybot.entity.kcs.KCSPayload
import cn.rtast.fancybot.entity.kcs.KCSResponse
import cn.rtast.fancybot.util.Http
import cn.rtast.fancybot.util.str.toJson
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OBMessage
class KotlinCCommand : BaseCommand() {
override val commandNames = listOf("/kotlinc", "/ktc")
private val kotlinCompilerServer = "https://api.kotlinlang.org/api/2.0.20/compiler/run"
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
if (args.isEmpty()) {
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addText("使用`/ktc` <code>传入Kotlin代码即可执行~")
.build()
listener.sendGroupMessage(message.groupId, msg)
return
}
val code = args.joinToString(" ")
val response = Http.post<KCSResponse>(
kotlinCompilerServer, jsonBody = KCSPayload(listOf(KCSPayload.File(code))).toJson()
)
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addText("执行结果如下:")
.addNewLine()
.addText(response.text.replace("</outStream>", "").replace("<outStream>", ""))
.build()
listener.sendGroupMessage(message.groupId, msg)
}
}
@@ -0,0 +1,20 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/12
*/
package cn.rtast.fancybot.entity.kcs
data class KCSPayload(
val files: List<File>,
val args: String = "",
val confType: String = "java"
) {
data class File(
val text: String,
val name: String = "File.kt",
val publicId: String = ""
)
}
@@ -0,0 +1,22 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/12
*/
package cn.rtast.fancybot.entity.kcs
import com.google.gson.annotations.SerializedName
data class KCSResponse(
val errors: Errors,
val exception: Any,
val jvmByteCode: Any,
val text: String
) {
data class Errors(
@SerializedName("File.kt")
val file: List<Any>
)
}