diff --git a/build.gradle.kts b/build.gradle.kts index f744107..5e2113d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "me.jie65535" -version = "0.1" +version = "0.2" repositories { maven("https://maven.aliyun.com/repository/public") @@ -16,5 +16,4 @@ repositories { dependencies{ implementation("org.jsoup:jsoup:1.13.1") - implementation("com.beust:klaxon:5.5") } \ No newline at end of file diff --git a/src/main/kotlin/GlotAPI.kt b/src/main/kotlin/GlotAPI.kt index fa4d035..f4348e7 100644 --- a/src/main/kotlin/GlotAPI.kt +++ b/src/main/kotlin/GlotAPI.kt @@ -1,6 +1,5 @@ -import com.beust.klaxon.Json -import com.beust.klaxon.Klaxon -import java.security.InvalidKeyException +import kotlinx.serialization.* +import kotlinx.serialization.json.* /** * # glot.io api 封装 @@ -8,7 +7,6 @@ import java.security.InvalidKeyException * 它提供了免费API供外界使用,API文档见 [https://github.com/glotcode/glot/blob/master/api_docs] * 本类是对该API文档的封装 * 通过 [listLanguages] 获取支持在线运行的编程语言列表 - * ~~通过 [getVersion] 获取对应语言的最新版本请求地址~~ * 通过 [getSupport] 判断指定编程语言是否支持 * 通过 [getTemplateFile] 来获取指定编程语言的模板文件(runCode需要) * 以上接口均有缓存,仅首次获取不同数据时会发起请求。因此,首次运行可能较慢。 @@ -25,12 +23,16 @@ object GlotAPI { // 运行代码需要api token,这是的我账号申请的,可以在[https://glot.io/auth/page/simple/register]注册帐号 private const val API_TOKEN = "074ef4a7-7a94-47f2-9891-85511ef1fb52" + @Serializable data class Language(val name: String, val url: String) + @Serializable data class CodeFile(val name: String, val content: String) - data class RunCodeRequest(@Json(serializeNull = false) val stdin: String?, - @Json(serializeNull = false) val command: String?, + @Serializable + data class RunCodeRequest(val stdin: String? = null, + val command: String? = null, val files: List) + @Serializable data class RunResult(val stdout: String, val stderr: String, val error: String) private var languages: List? = null @@ -55,7 +57,7 @@ object GlotAPI { */ fun listLanguages(): List { if (languages == null) { - languages = Klaxon().parseArray(HttpUtil.get(URL_LIST_LANGUAGES)) ?: throw Exception("未获取到任何数据") + languages = Json.decodeFromString(HttpUtil.get(URL_LIST_LANGUAGES)) ?: throw Exception("未获取到任何数据") } return languages!! } @@ -71,10 +73,10 @@ object GlotAPI { * 获取编程语言请求地址,若不支持将会抛出异常 * @param language 编程语言名字(忽略大小写) * @return 返回语言请求地址 - * @exception InvalidKeyException 不支持的语言 + * @exception Exception 不支持的语言 */ fun getSupport(language: String): Language = - listLanguages().find { it.name.equals(language, true) } ?: throw InvalidKeyException("不支持的语言") + listLanguages().find { it.name.equals(language, true) } ?: throw Exception("不支持的语言") /** * 获取指定编程语言的模板文件(缓存) @@ -91,7 +93,6 @@ object GlotAPI { return templateFile } - /** * # 运行代码 * @@ -165,8 +166,8 @@ object GlotAPI { * 导致程序无法在限定时间内返回,将会报告超时异常 */ fun runCode(language: Language, requestData: RunCodeRequest): RunResult { - val response = HttpUtil.post(language.url + "/latest", Klaxon().toJsonString(requestData), mapOf("Authorization" to API_TOKEN)) - return Klaxon().parse(response) ?: throw Exception("未获取到任何数据") + val response = HttpUtil.post(language.url + "/latest", Json.encodeToString(requestData), mapOf("Authorization" to API_TOKEN)) + return Json.decodeFromString(response) ?: throw Exception("未获取到任何数据") } diff --git a/src/main/kotlin/HttpUtil.kt b/src/main/kotlin/HttpUtil.kt index e415c4a..8de14cb 100644 --- a/src/main/kotlin/HttpUtil.kt +++ b/src/main/kotlin/HttpUtil.kt @@ -2,7 +2,6 @@ import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.OkHttpClient import okhttp3.Request -import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody import org.jsoup.Jsoup import org.jsoup.nodes.Document diff --git a/src/main/kotlin/JCC.kt b/src/main/kotlin/JCC.kt index b04f6cd..0f91820 100644 --- a/src/main/kotlin/JCC.kt +++ b/src/main/kotlin/JCC.kt @@ -16,13 +16,14 @@ object JCC : KotlinPlugin( JvmPluginDescription( id = "me.jie65535.jcc", name = "J Compiler Collection", - version = "0.1", + version = "0.2", ) { author("jie65535") info("""在线编译器集合""") } ) { const val CMD_PREFIX = "jcc" + const val MSG_MAX_LENGTH = 550 override fun onEnable() { logger.info { "Plugin loaded" } @@ -36,30 +37,34 @@ object JCC : KotlinPlugin( val msg = it.substring(CMD_PREFIX.length).trim() if (msg.isNotEmpty()) { val index = msg.indexOfFirst(Char::isWhitespace) - if (index >= 0) - { - val language = msg.substring(0, index) - val code = msg.substring(index).trim() - if (!GlotAPI.checkSupport(language)) - return@reply "不支持这种编程语言\n/jcc list #列出所有支持的编程语言" - if (code.isEmpty()) - return@reply "请输入要运行的代码" - try { - // subject.sendMessage("正在执行,请稍等...") - logger.info("请求执行代码") - val result = GlotAPI.runCode(language, code) - val builder = MessageChainBuilder() - var c = 0 - if (result.stdout.isNotEmpty()) c++ - if (result.stderr.isNotEmpty()) c++ - if (result.error.isNotEmpty()) c++ - val title = c >= 2 - var msgLength = 0 - if (subject is Group) { - builder.add(At(sender)) - builder.add("\n") - } + val language = if (index >= 0) msg.substring(0, index) else msg + if (!GlotAPI.checkSupport(language)) + return@reply "不支持这种编程语言\n/jcc list #列出所有支持的编程语言" + val code = if (index >= 0) { + msg.substring(index).trim() + } else { + return@reply "$CMD_PREFIX $language\n" + GlotAPI.getTemplateFile(language).content + } + try { + // subject.sendMessage("正在执行,请稍等...") + logger.info("请求执行代码") + val result = GlotAPI.runCode(language, code) + val builder = MessageChainBuilder() + var c = 0 + if (result.stdout.isNotEmpty()) c++ + if (result.stderr.isNotEmpty()) c++ + if (result.error.isNotEmpty()) c++ + val title = c >= 2 + var msgLength = 0 + if (subject is Group) { + builder.add(At(sender)) + builder.add("\n") + } + + if (c == 0) { + builder.add("没有任何结果呢~") + } else { if (result.error.isNotEmpty()) { builder.add("error:\n") builder.add(result.error) @@ -75,17 +80,17 @@ object JCC : KotlinPlugin( builder.add(result.stderr) msgLength += result.stderr.length } - val messageChain = builder.build() - if (msgLength > 500) { - val messageContent = messageChain.contentToString() - return@reply "消息内容过长,已贴到Pastebin:\n" + UbuntuPastebinHelper.paste(messageContent) - } else { - return@reply messageChain - } - } catch (e: Exception) { - logger.warning(e) - return@reply "执行失败\n原因:${e.message}" } + val messageChain = builder.build() + if (msgLength > MSG_MAX_LENGTH) { + val messageContent = messageChain.contentToString() + return@reply "消息内容过长,已贴到Pastebin:\n" + UbuntuPastebinHelper.paste(messageContent) + } else { + return@reply messageChain + } + } catch (e: Exception) { + logger.warning(e) + return@reply "执行失败\n原因:${e.message}" } } return@reply "请输入正确的命令!例如:\n$CMD_PREFIX python print(\"Hello world\")" diff --git a/src/main/kotlin/JccCommand.kt b/src/main/kotlin/JccCommand.kt index 04ec356..066217f 100644 --- a/src/main/kotlin/JccCommand.kt +++ b/src/main/kotlin/JccCommand.kt @@ -1,3 +1,4 @@ +import JCC.CMD_PREFIX import net.mamoe.mirai.console.command.CommandSender import net.mamoe.mirai.console.command.CompositeCommand @@ -19,6 +20,17 @@ object JccCommand : CompositeCommand( @SubCommand @Description("帮助") suspend fun CommandSender.help() { - sendMessage("直接调用jcc即可运行代码\n例如:jcc python print(\"Hello world\")\n其它指令:\n$usage") + sendMessage("直接调用${CMD_PREFIX}即可运行代码\n例如:${CMD_PREFIX} python print(\"Hello world\")\n其它指令:\n$usage") + } + + @SubCommand + @Description("获取指定语言的模板") + suspend fun CommandSender.template(language: String) { + if (!GlotAPI.checkSupport(language)) { + sendMessage("不支持该语言,请使用/jcc list列出所有支持的编程语言") + return + } + val file = GlotAPI.getTemplateFile(language) + sendMessage("$CMD_PREFIX $language\n" + file.content) } } \ No newline at end of file diff --git a/src/main/kotlin/UbuntuPastebinHelper.kt b/src/main/kotlin/UbuntuPastebinHelper.kt index 90acf7f..a339e14 100644 --- a/src/main/kotlin/UbuntuPastebinHelper.kt +++ b/src/main/kotlin/UbuntuPastebinHelper.kt @@ -53,7 +53,7 @@ object UbuntuPastebinHelper { * @param expiration 过期时间((empty)/day/week/month/year) 默认值:"day" * @return 返回访问地址,如:https://paste.ubuntu.com/p/nmn8yKMtND/ */ - fun paste(content: String, syntax: String = "text", poster: String = "temp", expiration: String = "day"): String? { + fun paste(content: String, syntax: String = "text", poster: String = "temp", expiration: String = "day"): String { if (poster.length > 30) throw Exception("poster length too long!") if (content.isEmpty())