2024-09-13 12:44:10 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2024 RTAkland
|
|
|
|
|
* Author: RTAkland
|
|
|
|
|
* Date: 2024/9/12
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2024-09-20 16:30:33 +08:00
|
|
|
package cn.rtast.fancybot.commands.misc
|
2024-09-13 12:44:10 +08:00
|
|
|
|
|
|
|
|
import cn.rtast.fancybot.entity.compiler.GLOTPayload
|
|
|
|
|
import cn.rtast.fancybot.entity.compiler.GLOTResponse
|
|
|
|
|
import cn.rtast.fancybot.entity.compiler.KCSPayload
|
|
|
|
|
import cn.rtast.fancybot.entity.compiler.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
|
2024-09-19 11:06:40 +08:00
|
|
|
import cn.rtast.rob.util.ob.OneBotListener
|
2024-09-13 12:44:10 +08:00
|
|
|
|
|
|
|
|
class CompilerCommand : BaseCommand() {
|
|
|
|
|
override val commandNames = listOf("/compiler", "/exec")
|
|
|
|
|
|
|
|
|
|
private val kotlinCompilerServer = "https://api.kotlinlang.org/api/2.0.20/compiler/run"
|
|
|
|
|
private val glotCompilerServer = "https://glot.io/run"
|
|
|
|
|
|
2024-09-19 11:06:40 +08:00
|
|
|
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
2024-09-13 12:44:10 +08:00
|
|
|
if (args.isEmpty()) {
|
|
|
|
|
val msg = MessageChain.Builder()
|
|
|
|
|
.addAt(message.sender.userId)
|
|
|
|
|
.addText("使用`/exec` <语言> <代码> 即可执行~")
|
|
|
|
|
.build()
|
|
|
|
|
listener.sendGroupMessage(message.groupId, msg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
val language = args.first()
|
|
|
|
|
val code = args.drop(1).joinToString(" ")
|
|
|
|
|
val response = when (language) {
|
|
|
|
|
"kotlin", "Kotlin", "kt", "Kt", "KT" -> {
|
|
|
|
|
Http.post<KCSResponse>(
|
|
|
|
|
kotlinCompilerServer, jsonBody = KCSPayload(listOf(KCSPayload.File(code))).toJson()
|
|
|
|
|
).text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
|
Http.post<GLOTResponse>(
|
|
|
|
|
"$glotCompilerServer/$language",
|
|
|
|
|
params = mapOf("version" to "latest"),
|
|
|
|
|
jsonBody = GLOTPayload(listOf(GLOTPayload.File(code))).toJson()
|
|
|
|
|
).stdout
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val msg = MessageChain.Builder()
|
|
|
|
|
.addAt(message.sender.userId)
|
|
|
|
|
.addText("执行结果如下:")
|
|
|
|
|
.addNewLine()
|
|
|
|
|
.addText(response.replace("<outStream>", "").replace("</outStream>", ""))
|
|
|
|
|
.build()
|
|
|
|
|
listener.sendGroupMessage(message.groupId, msg)
|
|
|
|
|
}
|
|
|
|
|
}
|