mirror of
https://github.com/jie65535/mirai-console-jcc-plugin.git
synced 2025-12-15 18:41:39 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f4945fc34 | |||
| 3958c2a385 | |||
| 646a340171 |
56
README.md
56
README.md
@@ -1,31 +1,55 @@
|
||||
# JCC - J Compiler Collection
|
||||
## 基于Glot接口的mirai-console在线编译器插件
|
||||
|
||||
### 使用帮助
|
||||
```
|
||||
run <language> <code|pastebin> [stdin]
|
||||
例如:run python print("Hello world")
|
||||
支持从pastebin.ubuntu.com中运行代码:
|
||||
run c https://pastebin.ubuntu.com/p/KhBB7ZjVbD/
|
||||
你还可以在后面跟随标准输入(仅pastebin支持):
|
||||
run c https://pastebin.ubuntu.com/p/S2PyvRqJNf/ 1 2 3 4
|
||||
其它指令:
|
||||
/jcc help # 帮助
|
||||
/jcc list # 列出所有支持的编程语言
|
||||
/jcc template <language> # 获取指定语言的模板
|
||||
```
|
||||
## `run`命令原型
|
||||
- `run <language> <code>`
|
||||
- `run <language> <pastebinUrl> [stdin]`
|
||||
- `引用消息: run <language> [stdin]`
|
||||
### 参数说明
|
||||
| 参数 | 说明 | 示例 | 备注 |
|
||||
| ---- | ---- | --- | --- |
|
||||
| `language` | 编程语言 | `python` | 用`/jcc list`列出所有支持的语言 |
|
||||
| `code` | 代码 | `print("Hello world")` | 要运行的代码,支持换行 |
|
||||
| `pastebinUrl` | pastebin地址 | `https://pastebin.ubuntu.com/p/KhBB7ZjVbD/` | 需要在 [pastebin](https://pastebin.ubuntu.com/) 上传代码 |
|
||||
| `stdin` | 标准输入 | `1 2 3 4 5` | 可选 用于`scanf`之类 |
|
||||
|
||||
## 使用示例
|
||||
### 直接使用
|
||||
`run python print("Hello world")`
|
||||
|
||||
### 从 [pastebinUrl](https://pastebin.ubuntu.com/) 运行代码:
|
||||
`run c https://pastebin.ubuntu.com/p/KhBB7ZjVbD/`
|
||||
|
||||
### 从 引用 执行代码:
|
||||
> 引用: print("Hello world")
|
||||
|
||||
`run python`
|
||||
|
||||
### 支持运行程序带输入:
|
||||
#### 例1
|
||||
`run c https://pastebin.ubuntu.com/p/S2PyvRqJNf/ 1 2 3 4 5`
|
||||
|
||||
#### 例2
|
||||
> 引用: https://pastebin.ubuntu.com/p/S2PyvRqJNf/
|
||||
|
||||
`run c 1 2 3 4 5`
|
||||
|
||||
## 其他指令
|
||||
- /jcc help # 帮助
|
||||
- /jcc list # 列出所有支持的编程语言
|
||||
- /jcc template <language> # 获取指定语言的模板
|
||||
|
||||
---
|
||||
|
||||
### 使用方法
|
||||
## 插件使用方法
|
||||
本插件基于[Mirai-Console](https://github.com/mamoe/mirai-console)运行,您可以通过阅读[Mirai用户手册](https://docs.mirai.mamoe.net/UserManual.html)来了解如何安装、启动机器人。
|
||||
|
||||
`MiraiConsole`成功启动后,只需要将本项目[发布](https://github.com/jie65535/mirai-console-jcc-plugin/releases)的`.jar`文件放入`.\plugins\`目录下即可加载插件。
|
||||
|
||||
### 发布地址
|
||||
## 发布地址
|
||||
本插件在[Mirai论坛](https://mirai.mamoe.net/)发布,[点击这里](https://mirai.mamoe.net/topic/463/jcc-%E5%9F%BA%E4%BA%8Emirai-console%E7%9A%84%E5%9C%A8%E7%BA%BF%E7%BC%96%E8%AF%91%E6%8F%92%E4%BB%B6)查看
|
||||
|
||||
### 反馈
|
||||
## 反馈
|
||||
如使用或安装插件过程中遇到非本插件功能问题,您首先应该在[Mirai论坛](https://mirai.mamoe.net/)中搜索解决方案,若未解决,可以在[本项目的主题贴](https://mirai.mamoe.net/topic/463/jcc-%E5%9F%BA%E4%BA%8Emirai-console%E7%9A%84%E5%9C%A8%E7%BA%BF%E7%BC%96%E8%AF%91%E6%8F%92%E4%BB%B6)中回帖提问。
|
||||
|
||||
如果是插件本身的问题或漏洞,您可以向我提交一个[issue](https://github.com/jie65535/mirai-console-jcc-plugin/issues)。若您有能力且愿意帮助我修复这些问题,请提交[Pull request](https://github.com/jie65535/mirai-console-jcc-plugin/pulls)。
|
||||
|
||||
@@ -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\")"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,11 @@ object JccCommand : CompositeCommand(
|
||||
@Description("帮助")
|
||||
suspend fun CommandSender.help() {
|
||||
sendMessage(
|
||||
"$CMD_PREFIX <language> <code|pastebin> [stdin]\n" +
|
||||
"例如:${CMD_PREFIX} python print(\"Hello world\")\n" +
|
||||
"支持从pastebin.ubuntu.com中运行代码:\n" +
|
||||
"$CMD_PREFIX c https://pastebin.ubuntu.com/p/KhBB7ZjVbD/\n" +
|
||||
"你还可以在后面跟随标准输入(仅pastebin支持):\n" +
|
||||
"$CMD_PREFIX c https://pastebin.ubuntu.com/p/S2PyvRqJNf/ 1 2 3 4\n" +
|
||||
"在线运行代码指令:\n" +
|
||||
"$CMD_PREFIX <language> <code>\n" +
|
||||
"$CMD_PREFIX <language> <pastebinUrl> [stdin]\n" +
|
||||
"引用消息: $CMD_PREFIX <language> [stdin]\n" +
|
||||
"仓库地址:https://github.com/jie65535/mirai-console-jcc-plugin\n" +
|
||||
"其它指令:\n" +
|
||||
usage
|
||||
)
|
||||
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user