mirror of
https://github.com/jie65535/mirai-console-jcc-plugin.git
synced 2025-06-02 17:39:14 +08:00
更新到v1.0
弃用pastebin粘贴 增加pastebin运行支持 增加pastebin运行时增加输入 更新依赖
This commit is contained in:
parent
8c3c316d5d
commit
f374a446ee
15
README.md
15
README.md
@ -1,8 +1,19 @@
|
|||||||
# JCC - J Compiler Collection
|
# JCC - J Compiler Collection
|
||||||
## 基于Glot接口的mirai-console在线编译器插件
|
## 基于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> # 获取指定语言的模板
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
plugins {
|
plugins {
|
||||||
val kotlinVersion = "1.5.10"
|
val kotlinVersion = "1.6.10"
|
||||||
kotlin("jvm") version kotlinVersion
|
kotlin("jvm") version kotlinVersion
|
||||||
kotlin("plugin.serialization") 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"
|
group = "top.jie65535"
|
||||||
version = "0.2"
|
version = "1.0"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
maven("https://maven.aliyun.com/repository/public")
|
maven("https://maven.aliyun.com/repository/public")
|
||||||
|
@ -1 +1 @@
|
|||||||
rootProject.name = "jcc"
|
rootProject.name = "mirai-console-jcc-plugin"
|
@ -1,6 +1,5 @@
|
|||||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
||||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
|
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.JvmPluginDescription
|
||||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||||
import net.mamoe.mirai.contact.Group
|
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.At
|
||||||
import net.mamoe.mirai.message.data.MessageChainBuilder
|
import net.mamoe.mirai.message.data.MessageChainBuilder
|
||||||
import net.mamoe.mirai.utils.info
|
import net.mamoe.mirai.utils.info
|
||||||
import okhttp3.internal.indexOfNonWhitespace
|
|
||||||
|
|
||||||
object JCC : KotlinPlugin(
|
object JCompilerCollection : KotlinPlugin(
|
||||||
JvmPluginDescription(
|
JvmPluginDescription(
|
||||||
id = "me.jie65535.jcc",
|
id = "top.jie65535.mirai-console-jcc-plugin",
|
||||||
name = "J Compiler Collection",
|
name = "J Compiler Collection",
|
||||||
version = "0.2",
|
version = "1.0",
|
||||||
) {
|
) {
|
||||||
author("jie65535")
|
author("jie65535")
|
||||||
info("""在线编译器集合""")
|
info("""在线编译器集合""")
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
const val CMD_PREFIX = "jcc"
|
const val CMD_PREFIX = "run"
|
||||||
const val MSG_MAX_LENGTH = 550
|
private const val MSG_MAX_LENGTH = 550
|
||||||
|
|
||||||
override fun onEnable() {
|
override fun onEnable() {
|
||||||
logger.info { "Plugin loaded" }
|
logger.info { "Plugin loaded" }
|
||||||
@ -40,23 +38,41 @@ object JCC : KotlinPlugin(
|
|||||||
val language = if (index >= 0) msg.substring(0, index) else msg
|
val language = if (index >= 0) msg.substring(0, index) else msg
|
||||||
if (!GlotAPI.checkSupport(language))
|
if (!GlotAPI.checkSupport(language))
|
||||||
return@reply "不支持这种编程语言\n/jcc list #列出所有支持的编程语言"
|
return@reply "不支持这种编程语言\n/jcc list #列出所有支持的编程语言"
|
||||||
val code = if (index >= 0) {
|
var code = if (index >= 0) {
|
||||||
msg.substring(index).trim()
|
msg.substring(index).trim()
|
||||||
} else {
|
} else {
|
||||||
return@reply "$CMD_PREFIX $language\n" + GlotAPI.getTemplateFile(language).content
|
return@reply "$CMD_PREFIX $language\n" + GlotAPI.getTemplateFile(language).content
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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("正在执行,请稍等...")
|
// subject.sendMessage("正在执行,请稍等...")
|
||||||
logger.info("请求执行代码")
|
logger.info("请求执行代码\n$code")
|
||||||
val result = GlotAPI.runCode(language, code)
|
val result = GlotAPI.runCode(language, code, input)
|
||||||
val builder = MessageChainBuilder()
|
val builder = MessageChainBuilder()
|
||||||
var c = 0
|
var c = 0
|
||||||
if (result.stdout.isNotEmpty()) c++
|
if (result.stdout.isNotEmpty()) c++
|
||||||
if (result.stderr.isNotEmpty()) c++
|
if (result.stderr.isNotEmpty()) c++
|
||||||
if (result.error.isNotEmpty()) c++
|
if (result.error.isNotEmpty()) c++
|
||||||
val title = c >= 2
|
val title = c >= 2
|
||||||
var msgLength = 0
|
|
||||||
if (subject is Group) {
|
if (subject is Group) {
|
||||||
builder.add(At(sender))
|
builder.add(At(sender))
|
||||||
builder.add("\n")
|
builder.add("\n")
|
||||||
@ -65,29 +81,26 @@ object JCC : KotlinPlugin(
|
|||||||
if (c == 0) {
|
if (c == 0) {
|
||||||
builder.add("没有任何结果呢~")
|
builder.add("没有任何结果呢~")
|
||||||
} else {
|
} else {
|
||||||
|
val sb = StringBuilder()
|
||||||
if (result.error.isNotEmpty()) {
|
if (result.error.isNotEmpty()) {
|
||||||
builder.add("error:\n")
|
sb.appendLine("error:")
|
||||||
builder.add(result.error)
|
sb.append(result.error)
|
||||||
msgLength += result.error.length + 7
|
|
||||||
}
|
}
|
||||||
if (result.stdout.isNotEmpty()) {
|
if (result.stdout.isNotEmpty()) {
|
||||||
if (title) builder.add("\nstdout:\n")
|
if (title) sb.appendLine("\nstdout:")
|
||||||
builder.add(result.stdout)
|
sb.append(result.stdout)
|
||||||
msgLength += result.stdout.length
|
|
||||||
}
|
}
|
||||||
if (result.stderr.isNotEmpty()) {
|
if (result.stderr.isNotEmpty()) {
|
||||||
if (title) builder.add("\nstderr:\n")
|
if (title) sb.appendLine("\nstderr:")
|
||||||
builder.add(result.stderr)
|
sb.append(result.stderr)
|
||||||
msgLength += result.stderr.length
|
|
||||||
}
|
}
|
||||||
|
if (sb.length > MSG_MAX_LENGTH) {
|
||||||
|
sb.deleteRange(MSG_MAX_LENGTH, sb.length)
|
||||||
|
sb.append("\n消息内容过长,已截断")
|
||||||
}
|
}
|
||||||
val messageChain = builder.build()
|
builder.append(sb.toString())
|
||||||
if (msgLength > MSG_MAX_LENGTH) {
|
|
||||||
val messageContent = messageChain.contentToString()
|
|
||||||
return@reply "消息内容过长,已贴到Pastebin:\n" + UbuntuPastebinHelper.paste(messageContent)
|
|
||||||
} else {
|
|
||||||
return@reply messageChain
|
|
||||||
}
|
}
|
||||||
|
return@reply builder.build()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.warning(e)
|
logger.warning(e)
|
||||||
return@reply "执行失败\n原因:${e.message}"
|
return@reply "执行失败\n原因:${e.message}"
|
@ -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.CommandSender
|
||||||
import net.mamoe.mirai.console.command.CompositeCommand
|
import net.mamoe.mirai.console.command.CompositeCommand
|
||||||
|
|
||||||
object JccCommand : CompositeCommand(
|
object JccCommand : CompositeCommand(
|
||||||
JCC, "jcc",
|
JCompilerCollection, "jcc",
|
||||||
description = "在线编译器集合"
|
description = "在线编译器集合"
|
||||||
) {
|
) {
|
||||||
@SubCommand
|
@SubCommand
|
||||||
@ -13,14 +13,23 @@ object JccCommand : CompositeCommand(
|
|||||||
sendMessage(GlotAPI.listLanguages().joinToString { it.name })
|
sendMessage(GlotAPI.listLanguages().joinToString { it.name })
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
sendMessage("执行失败\n${e.message}")
|
sendMessage("执行失败\n${e.message}")
|
||||||
JCC.logger.warning(e)
|
JCompilerCollection.logger.warning(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubCommand
|
@SubCommand
|
||||||
@Description("帮助")
|
@Description("帮助")
|
||||||
suspend fun CommandSender.help() {
|
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
|
@SubCommand
|
||||||
|
@ -21,6 +21,7 @@ object UbuntuPastebinHelper {
|
|||||||
* 获取支持的语法列表(缓存)
|
* 获取支持的语法列表(缓存)
|
||||||
* @return 返回一个map,其中key是给人看的,value是作为参数传递的
|
* @return 返回一个map,其中key是给人看的,value是作为参数传递的
|
||||||
*/
|
*/
|
||||||
|
@Deprecated("paste.ubuntu.com 现在需要登录 首页不再显示符号列表,因此该方法弃用")
|
||||||
fun getSyntaxList(): Map<String, String> {
|
fun getSyntaxList(): Map<String, String> {
|
||||||
if (syntaxList != null)
|
if (syntaxList != null)
|
||||||
return syntaxList!!
|
return syntaxList!!
|
||||||
@ -33,16 +34,20 @@ object UbuntuPastebinHelper {
|
|||||||
return map
|
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/
|
* @param url pastebin地址,如:https://paste.ubuntu.com/p/nmn8yKMtND/
|
||||||
* @return 返回链接中贴的内容
|
* @return 返回链接中贴的内容
|
||||||
*/
|
*/
|
||||||
fun get(url: String): String {
|
fun get(url: String): String {
|
||||||
if (url.isEmpty() || !url.startsWith("https://paste.ubuntu.com/p/"))
|
if (!checkUrl(url))
|
||||||
throw Exception("非法的url")
|
throw Exception("非法的url")
|
||||||
val document = HttpUtil.getDocument(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"
|
* @param expiration 过期时间((empty)/day/week/month/year) 默认值:"day"
|
||||||
* @return 返回访问地址,如:https://paste.ubuntu.com/p/nmn8yKMtND/
|
* @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 {
|
fun paste(content: String, syntax: String = "text", poster: String = "temp", expiration: String = "day"): String {
|
||||||
if (poster.length > 30)
|
if (poster.length > 30)
|
||||||
throw Exception("poster length too long!")
|
throw Exception("poster length too long!")
|
||||||
|
@ -1 +1 @@
|
|||||||
JCC
|
JCompilerCollection
|
Loading…
Reference in New Issue
Block a user