mirror of
https://github.com/jie65535/mirai-console-jcc-plugin.git
synced 2025-06-02 17:39:14 +08:00
更新到v1.1.0
支持执行引用的代码(含pastebin网址) 执行引用的代码时,参数可包含输入
This commit is contained in:
parent
f374a446ee
commit
646a340171
@ -7,7 +7,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "top.jie65535"
|
||||
version = "1.0"
|
||||
version = "1.1.0"
|
||||
|
||||
repositories {
|
||||
maven("https://maven.aliyun.com/repository/public")
|
||||
|
@ -35,8 +35,6 @@ object GlotAPI {
|
||||
@Serializable
|
||||
data class RunResult(val stdout: String, val stderr: String, val error: String)
|
||||
|
||||
private var languages: List<Language>? = null
|
||||
private val templateFiles: MutableMap<String, CodeFile> = mutableMapOf()
|
||||
// val fileExtensions: Map<String, String> = 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<Language> {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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<PlainText>()?.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\")"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
15
src/main/kotlin/JccPluginData.kt
Normal file
15
src/main/kotlin/JccPluginData.kt
Normal file
@ -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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user