From 646a340171b2051d270e1bbafe044bce1af7304b Mon Sep 17 00:00:00 2001 From: jie65535 Date: Wed, 30 Mar 2022 17:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B0v1.1.0=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=89=A7=E8=A1=8C=E5=BC=95=E7=94=A8=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=88=E5=90=ABpastebin=E7=BD=91=E5=9D=80=EF=BC=89?= =?UTF-8?q?=20=E6=89=A7=E8=A1=8C=E5=BC=95=E7=94=A8=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=97=B6=EF=BC=8C=E5=8F=82=E6=95=B0=E5=8F=AF=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E8=BE=93=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- src/main/kotlin/GlotAPI.kt | 14 +++--- src/main/kotlin/JCompilerCollection.kt | 65 +++++++++++++++++--------- src/main/kotlin/JccPluginData.kt | 15 ++++++ 4 files changed, 64 insertions(+), 32 deletions(-) create mode 100644 src/main/kotlin/JccPluginData.kt diff --git a/build.gradle.kts b/build.gradle.kts index fd3f635..9b1199b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "top.jie65535" -version = "1.0" +version = "1.1.0" repositories { maven("https://maven.aliyun.com/repository/public") diff --git a/src/main/kotlin/GlotAPI.kt b/src/main/kotlin/GlotAPI.kt index f4348e7..b8613b0 100644 --- a/src/main/kotlin/GlotAPI.kt +++ b/src/main/kotlin/GlotAPI.kt @@ -35,8 +35,6 @@ object GlotAPI { @Serializable data class RunResult(val stdout: String, val stderr: String, val error: String) - private var languages: List? = null - private val templateFiles: MutableMap = mutableMapOf() // val fileExtensions: Map = mapOf("assembly" to "asm", "ats" to "dats", "bash" to "sh", "c" to "c", "clojure" to "clj", "cobol" to "cob", "coffeescript" to "coffee", "cpp" to "cpp", "crystal" to "cr", "csharp" to "cs", "d" to "d", "elixir" to "ex", "elm" to "elm", "erlang" to "erl", "fsharp" to "fs", "go" to "go", "groovy" to "groovy", "haskell" to "hs", "idris" to "idr", "java" to "java", "javascript" to "js", "julia" to "jl", "kotlin" to "kt", "lua" to "lua", "mercury" to "m", "nim" to "nim", "nix" to "nix", "ocaml" to "ml", "perl" to "pl", "php" to "php", "python" to "py", "raku" to "raku", "ruby" to "rb", "rust" to "rs", "scala" to "scala", "swift" to "swift", "typescript" to "ts", "plaintext" to "txt", ) /** @@ -56,10 +54,10 @@ object GlotAPI { * ``` */ fun listLanguages(): List { - if (languages == null) { - languages = Json.decodeFromString(HttpUtil.get(URL_LIST_LANGUAGES)) ?: throw Exception("未获取到任何数据") + if (JccPluginData.languages.isEmpty()) { + JccPluginData.languages = Json.decodeFromString(HttpUtil.get(URL_LIST_LANGUAGES)) ?: throw Exception("未获取到任何数据") } - return languages!! + return JccPluginData.languages } /** @@ -83,13 +81,13 @@ object GlotAPI { */ fun getTemplateFile(language: String): CodeFile { val lang = getSupport(language) - if (templateFiles.containsKey(lang.name)) - return templateFiles[lang.name]!! + if (JccPluginData.templateFiles.containsKey(lang.name)) + return JccPluginData.templateFiles[lang.name]!! val document = HttpUtil.getDocument(URL_NEW + lang.name) val filename = HttpUtil.documentSelect(document, ".filename").firstOrNull()?.text() ?: throw Exception("无法获取文件名") val fileContent = HttpUtil.documentSelect(document, "#editor-1").text() ?: throw Exception("无法获取模板文件内容") val templateFile = CodeFile(filename, fileContent) - templateFiles[lang.name] = templateFile + JccPluginData.templateFiles[lang.name] = templateFile return templateFile } diff --git a/src/main/kotlin/JCompilerCollection.kt b/src/main/kotlin/JCompilerCollection.kt index e88a6d0..31c60b6 100644 --- a/src/main/kotlin/JCompilerCollection.kt +++ b/src/main/kotlin/JCompilerCollection.kt @@ -1,20 +1,23 @@ import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister +import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.load import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.isBotMuted +import net.mamoe.mirai.event.events.GroupMessageEvent +import net.mamoe.mirai.event.events.MessageEvent import net.mamoe.mirai.event.globalEventChannel import net.mamoe.mirai.event.subscribeMessages -import net.mamoe.mirai.message.data.At -import net.mamoe.mirai.message.data.MessageChainBuilder +import net.mamoe.mirai.message.MessageReceipt +import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.info object JCompilerCollection : KotlinPlugin( JvmPluginDescription( id = "top.jie65535.mirai-console-jcc-plugin", name = "J Compiler Collection", - version = "1.0", + version = "1.1.0", ) { author("jie65535") info("""在线编译器集合""") @@ -26,35 +29,53 @@ object JCompilerCollection : KotlinPlugin( override fun onEnable() { logger.info { "Plugin loaded" } JccCommand.register() + JccPluginData.reload() - - globalEventChannel().subscribeMessages { - startsWith(CMD_PREFIX, false) reply { - if (subject is Group && (subject as Group).isBotMuted) - return@reply null - val msg = it.substring(CMD_PREFIX.length).trim() - if (msg.isNotEmpty()) { - val index = msg.indexOfFirst(Char::isWhitespace) - val language = if (index >= 0) msg.substring(0, index) else msg - if (!GlotAPI.checkSupport(language)) - return@reply "不支持这种编程语言\n/jcc list #列出所有支持的编程语言" - var code = if (index >= 0) { - msg.substring(index).trim() - } else { - return@reply "$CMD_PREFIX $language\n" + GlotAPI.getTemplateFile(language).content + globalEventChannel() + .parentScope(this) + .subscribeMessages { + content { + message.firstIsInstanceOrNull()?.content?.trimStart()?.startsWith(CMD_PREFIX) == true + } reply { + val msg = message.firstIsInstance<PlainText>().content.trimStart().removePrefix(CMD_PREFIX).trim() + if (msg.isBlank()) { + return@reply "请输入正确的命令!例如:\n$CMD_PREFIX python print(\"Hello world\")" } + val index = msg.indexOfFirst(Char::isWhitespace) + val language = if (index >= 0) msg.substring(0, index) else msg + if (!GlotAPI.checkSupport(language)) + return@reply "不支持这种编程语言\n/jcc list #列出所有支持的编程语言" + try { - val si = code.indexOfFirst(Char::isWhitespace) + // 检查命令的引用 + val quote = message[QuoteReply] + var input: String? = null + // 支持运行引用的消息的代码 + var code = if (quote != null) { + // run c [input] + if (index >= 0) { + input = msg.substring(index).trim() + } + quote.source.originalMessage.content + } else if (index >= 0) { + msg.substring(index).trim() + } else { + return@reply "$CMD_PREFIX $language\n" + GlotAPI.getTemplateFile(language).content + } + + // 如果是引用消息,则不再从原消息中分析。否则,还要从消息中判断是否存在输入参数 + val si = if (quote != null) 0 else code.indexOfFirst(Char::isWhitespace) + // 尝试得到url val url = if (si > 0) { code.substring(0, si) } else { code } - var input: String? = null - // 如果参数是一个ubuntu pastebin的链接,则去获取 + // 如果参数是一个ubuntu pastebin的链接,则去获取具体代码 if (UbuntuPastebinHelper.checkUrl(url)) { if (si > 0) { + // 如果确实是一个链接,则链接后面跟的内容就是输入内容 input = code.substring(si+1) } logger.info("从 $url 中获取代码") @@ -106,8 +127,6 @@ object JCompilerCollection : KotlinPlugin( return@reply "执行失败\n原因:${e.message}" } } - return@reply "请输入正确的命令!例如:\n$CMD_PREFIX python print(\"Hello world\")" - } } } diff --git a/src/main/kotlin/JccPluginData.kt b/src/main/kotlin/JccPluginData.kt new file mode 100644 index 0000000..aede652 --- /dev/null +++ b/src/main/kotlin/JccPluginData.kt @@ -0,0 +1,15 @@ +import net.mamoe.mirai.console.data.AutoSavePluginData +import net.mamoe.mirai.console.data.value + +object JccPluginData: AutoSavePluginData("GlotCache") { + + /** + * 支持的语言 + */ + var languages: List<GlotAPI.Language> by value() + + /** + * 模板文件 + */ + val templateFiles: MutableMap<String, GlotAPI.CodeFile> by value() +} \ No newline at end of file