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

59 lines
2.1 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
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.OneBotListener
2024-09-13 12:44:10 +08:00
@CommandDescription("执行不同语言的代码片段")
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"
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
}
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()
2024-09-26 22:11:28 +08:00
.addReply(message.messageId)
2024-09-13 12:44:10 +08:00
.addText("执行结果如下:")
.addNewLine()
.addText(response.replace("<outStream>", "").replace("</outStream>", ""))
.build()
listener.sendGroupMessage(message.groupId, msg)
}
}