更新到v1.0

弃用pastebin粘贴
增加pastebin运行支持
增加pastebin运行时增加输入
更新依赖
This commit is contained in:
2022-03-30 11:19:59 +08:00
parent 8c3c316d5d
commit f374a446ee
7 changed files with 80 additions and 41 deletions

View File

@ -1,8 +1,19 @@
# JCC - J Compiler Collection
## 基于Glot接口的mirai-console在线编译器插件
### 运行截图
![运行截图](https://mirai.mamoe.net/assets/uploads/files/1627802860237-jcc.png)
### 使用帮助
```
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> # 获取指定语言的模板
```
---

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.0"
repositories {
maven("https://maven.aliyun.com/repository/public")

View File

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

View File

@ -1,6 +1,5 @@
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
@ -10,20 +9,19 @@ 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(
object JCompilerCollection : KotlinPlugin(
JvmPluginDescription(
id = "me.jie65535.jcc",
id = "top.jie65535.mirai-console-jcc-plugin",
name = "J Compiler Collection",
version = "0.2",
version = "1.0",
) {
author("jie65535")
info("""在线编译器集合""")
}
) {
const val CMD_PREFIX = "jcc"
const val MSG_MAX_LENGTH = 550
const val CMD_PREFIX = "run"
private const val MSG_MAX_LENGTH = 550
override fun onEnable() {
logger.info { "Plugin loaded" }
@ -40,23 +38,41 @@ object JCC : KotlinPlugin(
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) {
var code = if (index >= 0) {
msg.substring(index).trim()
} else {
return@reply "$CMD_PREFIX $language\n" + GlotAPI.getTemplateFile(language).content
}
try {
val si = code.indexOfFirst(Char::isWhitespace)
val url = if (si > 0) {
code.substring(0, si)
} else {
code
}
var input: String? = null
// 如果参数是一个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("请求执行代码")
val result = GlotAPI.runCode(language, code)
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
var msgLength = 0
if (subject is Group) {
builder.add(At(sender))
builder.add("\n")
@ -65,29 +81,26 @@ object JCC : KotlinPlugin(
if (c == 0) {
builder.add("没有任何结果呢~")
} else {
val sb = StringBuilder()
if (result.error.isNotEmpty()) {
builder.add("error:\n")
builder.add(result.error)
msgLength += result.error.length + 7
sb.appendLine("error:")
sb.append(result.error)
}
if (result.stdout.isNotEmpty()) {
if (title) builder.add("\nstdout:\n")
builder.add(result.stdout)
msgLength += result.stdout.length
if (title) sb.appendLine("\nstdout:")
sb.append(result.stdout)
}
if (result.stderr.isNotEmpty()) {
if (title) builder.add("\nstderr:\n")
builder.add(result.stderr)
msgLength += result.stderr.length
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消息内容过长,已截断")
}
val messageChain = builder.build()
if (msgLength > MSG_MAX_LENGTH) {
val messageContent = messageChain.contentToString()
return@reply "消息内容过长已贴到Pastebin\n" + UbuntuPastebinHelper.paste(messageContent)
} else {
return@reply messageChain
builder.append(sb.toString())
}
return@reply builder.build()
} catch (e: Exception) {
logger.warning(e)
return@reply "执行失败\n原因:${e.message}"

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,23 @@ 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(
"$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" +
usage
)
}
@SubCommand

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!")