Compare commits

...

4 Commits
v0.2 ... master

Author SHA1 Message Date
7f4945fc34 更新帮助
优化说明
2022-03-30 17:57:50 +08:00
3958c2a385 更新使用说明 2022-03-30 17:46:12 +08:00
646a340171 更新到v1.1.0
支持执行引用的代码(含pastebin网址)
执行引用的代码时,参数可包含输入
2022-03-30 17:28:19 +08:00
f374a446ee 更新到v1.0
弃用pastebin粘贴
增加pastebin运行支持
增加pastebin运行时增加输入
更新依赖
2022-03-30 11:19:59 +08:00
10 changed files with 223 additions and 129 deletions

View File

@ -1,20 +1,55 @@
# JCC - J Compiler Collection
## 基于Glot接口的mirai-console在线编译器插件
### 运行截图
![运行截图](https://mirai.mamoe.net/assets/uploads/files/1627802860237-jcc.png)
## `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)。

View File

@ -1,13 +1,13 @@
plugins {
val kotlinVersion = "1.5.10"
val kotlinVersion = "1.6.10"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
id("net.mamoe.mirai-console") version "2.6.7"
id("net.mamoe.mirai-console") version "2.10.0"
}
group = "me.jie65535"
version = "0.2"
group = "top.jie65535"
version = "1.1.0"
repositories {
maven("https://maven.aliyun.com/repository/public")

View File

@ -1 +1 @@
rootProject.name = "jcc"
rootProject.name = "mirai-console-jcc-plugin"

View File

@ -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
}

View File

@ -1,104 +0,0 @@
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
import net.mamoe.mirai.console.command.parse.CommandCallParser
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.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.utils.info
import okhttp3.internal.indexOfNonWhitespace
object JCC : KotlinPlugin(
JvmPluginDescription(
id = "me.jie65535.jcc",
name = "J Compiler Collection",
version = "0.2",
) {
author("jie65535")
info("""在线编译器集合""")
}
) {
const val CMD_PREFIX = "jcc"
const val MSG_MAX_LENGTH = 550
override fun onEnable() {
logger.info { "Plugin loaded" }
JccCommand.register()
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 #列出所有支持的编程语言"
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)
msgLength += result.error.length + 7
}
if (result.stdout.isNotEmpty()) {
if (title) builder.add("\nstdout:\n")
builder.add(result.stdout)
msgLength += result.stdout.length
}
if (result.stderr.isNotEmpty()) {
if (title) builder.add("\nstderr:\n")
builder.add(result.stderr)
msgLength += result.stderr.length
}
}
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\")"
}
}
}
override fun onDisable() {
JccCommand.unregister()
}
}

View File

@ -0,0 +1,136 @@
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.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.1.0",
) {
author("jie65535")
info("""在线编译器集合""")
}
) {
const val CMD_PREFIX = "run"
private const val MSG_MAX_LENGTH = 550
override fun onEnable() {
logger.info { "Plugin loaded" }
JccCommand.register()
JccPluginData.reload()
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 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
}
// 如果参数是一个ubuntu pastebin的链接则去获取具体代码
if (UbuntuPastebinHelper.checkUrl(url)) {
if (si > 0) {
// 如果确实是一个链接,则链接后面跟的内容就是输入内容
input = code.substring(si+1)
}
logger.info("$url 中获取代码")
code = UbuntuPastebinHelper.get(url)
if (code.isBlank()) {
return@reply "未获取到有效代码"
}
}
// subject.sendMessage("正在执行,请稍等...")
logger.info("请求执行代码\n$code")
val result = GlotAPI.runCode(language, code, input)
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
if (subject is Group) {
builder.add(At(sender))
builder.add("\n")
}
if (c == 0) {
builder.add("没有任何结果呢~")
} else {
val sb = StringBuilder()
if (result.error.isNotEmpty()) {
sb.appendLine("error:")
sb.append(result.error)
}
if (result.stdout.isNotEmpty()) {
if (title) sb.appendLine("\nstdout:")
sb.append(result.stdout)
}
if (result.stderr.isNotEmpty()) {
if (title) sb.appendLine("\nstderr:")
sb.append(result.stderr)
}
if (sb.length > MSG_MAX_LENGTH) {
sb.deleteRange(MSG_MAX_LENGTH, sb.length)
sb.append("\n消息内容过长,已截断")
}
builder.append(sb.toString())
}
return@reply builder.build()
} catch (e: Exception) {
logger.warning(e)
return@reply "执行失败\n原因:${e.message}"
}
}
}
}
override fun onDisable() {
JccCommand.unregister()
}
}

View File

@ -1,9 +1,9 @@
import JCC.CMD_PREFIX
import JCompilerCollection.CMD_PREFIX
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.CompositeCommand
object JccCommand : CompositeCommand(
JCC, "jcc",
JCompilerCollection, "jcc",
description = "在线编译器集合"
) {
@SubCommand
@ -13,14 +13,22 @@ object JccCommand : CompositeCommand(
sendMessage(GlotAPI.listLanguages().joinToString { it.name })
} catch (e: Exception) {
sendMessage("执行失败\n${e.message}")
JCC.logger.warning(e)
JCompilerCollection.logger.warning(e)
}
}
@SubCommand
@Description("帮助")
suspend fun CommandSender.help() {
sendMessage("直接调用${CMD_PREFIX}即可运行代码\n例如:${CMD_PREFIX} python print(\"Hello world\")\n其它指令:\n$usage")
sendMessage(
"在线运行代码指令:\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
)
}
@SubCommand

View 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()
}

View File

@ -21,6 +21,7 @@ object UbuntuPastebinHelper {
* 获取支持的语法列表缓存
* @return 返回一个map其中key是给人看的value是作为参数传递的
*/
@Deprecated("paste.ubuntu.com 现在需要登录 首页不再显示符号列表,因此该方法弃用")
fun getSyntaxList(): Map<String, String> {
if (syntaxList != null)
return syntaxList!!
@ -33,16 +34,20 @@ object UbuntuPastebinHelper {
return map
}
fun checkUrl(url: String): Boolean {
return url.startsWith("https://pastebin.ubuntu.com/p/") && url.endsWith('/') && url.length < 45
}
/**
* 获取内容
* @param url pastebin地址https://paste.ubuntu.com/p/nmn8yKMtND/
* @return 返回链接中贴的内容
*/
fun get(url: String): String {
if (url.isEmpty() || !url.startsWith("https://paste.ubuntu.com/p/"))
if (!checkUrl(url))
throw Exception("非法的url")
val document = HttpUtil.getDocument(url)
return HttpUtil.documentSelect(document, ".paste > pre").text()
return HttpUtil.documentSelect(document, "#hidden-content").text()
}
/**
@ -53,6 +58,7 @@ object UbuntuPastebinHelper {
* @param expiration 过期时间(empty)/day/week/month/year 默认值"day"
* @return 返回访问地址https://paste.ubuntu.com/p/nmn8yKMtND/
*/
@Deprecated("paste.ubuntu.com 现在需要登录,因此不能再粘贴")
fun paste(content: String, syntax: String = "text", poster: String = "temp", expiration: String = "day"): String {
if (poster.length > 30)
throw Exception("poster length too long!")