Files
FancyBot/src/main/kotlin/cn/rtast/fancybot/commands/misc/CompilerCommand.kt
T

73 lines
2.7 KiB
Kotlin
Raw Normal View History

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.annotations.CommandDescription
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
2024-10-02 21:02:05 +08:00
import cn.rtast.fancybot.enums.CommandAction
2024-09-13 12:44:10 +08:00
import cn.rtast.fancybot.util.Http
2024-10-02 21:02:05 +08:00
import cn.rtast.fancybot.util.file.insertActionRecord
2024-09-13 12:44:10 +08:00
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.OneBotListener
2024-09-13 12:44:10 +08:00
2024-10-02 18:32:47 +08:00
@CommandDescription("执行不同语言的代码")
2024-09-13 12:44:10 +08:00
class CompilerCommand : BaseCommand() {
2024-10-02 18:32:47 +08:00
override val commandNames = listOf("/compiler", "/exec", "/e")
2024-09-13 12:44:10 +08:00
private val kotlinCompilerServer = "https://api.kotlinlang.org/api/2.0.20/compiler/run"
private val glotCompilerServer = "https://glot.io/run"
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
2024-09-13 12:44:10 +08:00
if (args.isEmpty()) {
2024-09-26 22:11:28 +08:00
message.reply("使用`/exec` <语言> <代码> 即可执行~")
2024-09-13 12:44:10 +08:00
return
}
2024-10-02 20:05:52 +08:00
var language = args.first().lowercase()
2024-09-13 12:44:10 +08:00
val code = args.drop(1).joinToString(" ")
val response = when (language) {
2024-10-02 20:05:52 +08:00
"kotlin", "kt" -> {
2024-09-13 12:44:10 +08:00
Http.post<KCSResponse>(
2024-10-02 18:32:47 +08:00
kotlinCompilerServer,
jsonBody = KCSPayload(listOf(KCSPayload.File(code))).toJson()
2024-09-13 12:44:10 +08:00
).text
}
else -> {
2024-10-02 18:32:47 +08:00
if (language == "js") language = "javascript"
if (language == "cs") language = "csharp"
if (language == "sh") language = "bash"
if (language == "asm") language = "assembly"
if (language == "py") language = "python"
2024-09-13 12:44:10 +08:00
Http.post<GLOTResponse>(
"$glotCompilerServer/$language",
params = mapOf("version" to "latest"),
jsonBody = GLOTPayload(listOf(GLOTPayload.File(code))).toJson()
).stdout
}
}
2024-10-02 18:32:47 +08:00
try {
val msg = MessageChain.Builder()
.addReply(message.messageId)
.addText("执行结果如下:")
.addNewLine()
.addText(response.replace("<outStream>", "").replace("</outStream>", ""))
.build()
2024-10-24 16:55:14 +08:00
message.reply(msg)
2024-10-02 18:32:47 +08:00
} catch (e: Exception) {
message.reply("执行错误: ${e.message}")
2024-10-02 21:02:05 +08:00
} finally {
insertActionRecord(CommandAction.Compiler, message.sender.userId)
2024-10-02 18:32:47 +08:00
}
2024-09-13 12:44:10 +08:00
}
}