mirror of
https://github.com/jie65535/mirai-console-jcc-plugin.git
synced 2025-06-02 17:39:14 +08:00
移除 第三方Json解析库,使用Mirai自带库
增加 获取代码模板功能
This commit is contained in:
parent
17a8893fe8
commit
8c3c316d5d
@ -7,7 +7,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "me.jie65535"
|
group = "me.jie65535"
|
||||||
version = "0.1"
|
version = "0.2"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
maven("https://maven.aliyun.com/repository/public")
|
maven("https://maven.aliyun.com/repository/public")
|
||||||
@ -16,5 +16,4 @@ repositories {
|
|||||||
|
|
||||||
dependencies{
|
dependencies{
|
||||||
implementation("org.jsoup:jsoup:1.13.1")
|
implementation("org.jsoup:jsoup:1.13.1")
|
||||||
implementation("com.beust:klaxon:5.5")
|
|
||||||
}
|
}
|
@ -1,6 +1,5 @@
|
|||||||
import com.beust.klaxon.Json
|
import kotlinx.serialization.*
|
||||||
import com.beust.klaxon.Klaxon
|
import kotlinx.serialization.json.*
|
||||||
import java.security.InvalidKeyException
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* # glot.io api 封装
|
* # glot.io api 封装
|
||||||
@ -8,7 +7,6 @@ import java.security.InvalidKeyException
|
|||||||
* 它提供了免费API供外界使用,API文档见 [https://github.com/glotcode/glot/blob/master/api_docs]
|
* 它提供了免费API供外界使用,API文档见 [https://github.com/glotcode/glot/blob/master/api_docs]
|
||||||
* 本类是对该API文档的封装
|
* 本类是对该API文档的封装
|
||||||
* 通过 [listLanguages] 获取支持在线运行的编程语言列表
|
* 通过 [listLanguages] 获取支持在线运行的编程语言列表
|
||||||
* ~~通过 [getVersion] 获取对应语言的最新版本请求地址~~
|
|
||||||
* 通过 [getSupport] 判断指定编程语言是否支持
|
* 通过 [getSupport] 判断指定编程语言是否支持
|
||||||
* 通过 [getTemplateFile] 来获取指定编程语言的模板文件(runCode需要)
|
* 通过 [getTemplateFile] 来获取指定编程语言的模板文件(runCode需要)
|
||||||
* 以上接口均有缓存,仅首次获取不同数据时会发起请求。因此,首次运行可能较慢。
|
* 以上接口均有缓存,仅首次获取不同数据时会发起请求。因此,首次运行可能较慢。
|
||||||
@ -25,12 +23,16 @@ object GlotAPI {
|
|||||||
// 运行代码需要api token,这是的我账号申请的,可以在[https://glot.io/auth/page/simple/register]注册帐号
|
// 运行代码需要api token,这是的我账号申请的,可以在[https://glot.io/auth/page/simple/register]注册帐号
|
||||||
private const val API_TOKEN = "074ef4a7-7a94-47f2-9891-85511ef1fb52"
|
private const val API_TOKEN = "074ef4a7-7a94-47f2-9891-85511ef1fb52"
|
||||||
|
|
||||||
|
@Serializable
|
||||||
data class Language(val name: String, val url: String)
|
data class Language(val name: String, val url: String)
|
||||||
|
@Serializable
|
||||||
data class CodeFile(val name: String, val content: String)
|
data class CodeFile(val name: String, val content: String)
|
||||||
|
|
||||||
data class RunCodeRequest(@Json(serializeNull = false) val stdin: String?,
|
@Serializable
|
||||||
@Json(serializeNull = false) val command: String?,
|
data class RunCodeRequest(val stdin: String? = null,
|
||||||
|
val command: String? = null,
|
||||||
val files: List<CodeFile>)
|
val files: List<CodeFile>)
|
||||||
|
@Serializable
|
||||||
data class RunResult(val stdout: String, val stderr: String, val error: String)
|
data class RunResult(val stdout: String, val stderr: String, val error: String)
|
||||||
|
|
||||||
private var languages: List<Language>? = null
|
private var languages: List<Language>? = null
|
||||||
@ -55,7 +57,7 @@ object GlotAPI {
|
|||||||
*/
|
*/
|
||||||
fun listLanguages(): List<Language> {
|
fun listLanguages(): List<Language> {
|
||||||
if (languages == null) {
|
if (languages == null) {
|
||||||
languages = Klaxon().parseArray(HttpUtil.get(URL_LIST_LANGUAGES)) ?: throw Exception("未获取到任何数据")
|
languages = Json.decodeFromString(HttpUtil.get(URL_LIST_LANGUAGES)) ?: throw Exception("未获取到任何数据")
|
||||||
}
|
}
|
||||||
return languages!!
|
return languages!!
|
||||||
}
|
}
|
||||||
@ -71,10 +73,10 @@ object GlotAPI {
|
|||||||
* 获取编程语言请求地址,若不支持将会抛出异常
|
* 获取编程语言请求地址,若不支持将会抛出异常
|
||||||
* @param language 编程语言名字(忽略大小写)
|
* @param language 编程语言名字(忽略大小写)
|
||||||
* @return 返回语言请求地址
|
* @return 返回语言请求地址
|
||||||
* @exception InvalidKeyException 不支持的语言
|
* @exception Exception 不支持的语言
|
||||||
*/
|
*/
|
||||||
fun getSupport(language: String): Language =
|
fun getSupport(language: String): Language =
|
||||||
listLanguages().find { it.name.equals(language, true) } ?: throw InvalidKeyException("不支持的语言")
|
listLanguages().find { it.name.equals(language, true) } ?: throw Exception("不支持的语言")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取指定编程语言的模板文件(缓存)
|
* 获取指定编程语言的模板文件(缓存)
|
||||||
@ -91,7 +93,6 @@ object GlotAPI {
|
|||||||
return templateFile
|
return templateFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* # 运行代码
|
* # 运行代码
|
||||||
*
|
*
|
||||||
@ -165,8 +166,8 @@ object GlotAPI {
|
|||||||
* 导致程序无法在限定时间内返回,将会报告超时异常
|
* 导致程序无法在限定时间内返回,将会报告超时异常
|
||||||
*/
|
*/
|
||||||
fun runCode(language: Language, requestData: RunCodeRequest): RunResult {
|
fun runCode(language: Language, requestData: RunCodeRequest): RunResult {
|
||||||
val response = HttpUtil.post(language.url + "/latest", Klaxon().toJsonString(requestData), mapOf("Authorization" to API_TOKEN))
|
val response = HttpUtil.post(language.url + "/latest", Json.encodeToString(requestData), mapOf("Authorization" to API_TOKEN))
|
||||||
return Klaxon().parse(response) ?: throw Exception("未获取到任何数据")
|
return Json.decodeFromString(response) ?: throw Exception("未获取到任何数据")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ import okhttp3.MediaType
|
|||||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.RequestBody
|
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import org.jsoup.Jsoup
|
import org.jsoup.Jsoup
|
||||||
import org.jsoup.nodes.Document
|
import org.jsoup.nodes.Document
|
||||||
|
@ -16,13 +16,14 @@ object JCC : KotlinPlugin(
|
|||||||
JvmPluginDescription(
|
JvmPluginDescription(
|
||||||
id = "me.jie65535.jcc",
|
id = "me.jie65535.jcc",
|
||||||
name = "J Compiler Collection",
|
name = "J Compiler Collection",
|
||||||
version = "0.1",
|
version = "0.2",
|
||||||
) {
|
) {
|
||||||
author("jie65535")
|
author("jie65535")
|
||||||
info("""在线编译器集合""")
|
info("""在线编译器集合""")
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
const val CMD_PREFIX = "jcc"
|
const val CMD_PREFIX = "jcc"
|
||||||
|
const val MSG_MAX_LENGTH = 550
|
||||||
|
|
||||||
override fun onEnable() {
|
override fun onEnable() {
|
||||||
logger.info { "Plugin loaded" }
|
logger.info { "Plugin loaded" }
|
||||||
@ -36,14 +37,15 @@ object JCC : KotlinPlugin(
|
|||||||
val msg = it.substring(CMD_PREFIX.length).trim()
|
val msg = it.substring(CMD_PREFIX.length).trim()
|
||||||
if (msg.isNotEmpty()) {
|
if (msg.isNotEmpty()) {
|
||||||
val index = msg.indexOfFirst(Char::isWhitespace)
|
val index = msg.indexOfFirst(Char::isWhitespace)
|
||||||
if (index >= 0)
|
val language = if (index >= 0) msg.substring(0, index) else msg
|
||||||
{
|
|
||||||
val language = msg.substring(0, index)
|
|
||||||
val code = msg.substring(index).trim()
|
|
||||||
if (!GlotAPI.checkSupport(language))
|
if (!GlotAPI.checkSupport(language))
|
||||||
return@reply "不支持这种编程语言\n/jcc list #列出所有支持的编程语言"
|
return@reply "不支持这种编程语言\n/jcc list #列出所有支持的编程语言"
|
||||||
if (code.isEmpty())
|
val code = if (index >= 0) {
|
||||||
return@reply "请输入要运行的代码"
|
msg.substring(index).trim()
|
||||||
|
} else {
|
||||||
|
return@reply "$CMD_PREFIX $language\n" + GlotAPI.getTemplateFile(language).content
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// subject.sendMessage("正在执行,请稍等...")
|
// subject.sendMessage("正在执行,请稍等...")
|
||||||
logger.info("请求执行代码")
|
logger.info("请求执行代码")
|
||||||
@ -60,6 +62,9 @@ object JCC : KotlinPlugin(
|
|||||||
builder.add("\n")
|
builder.add("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (c == 0) {
|
||||||
|
builder.add("没有任何结果呢~")
|
||||||
|
} else {
|
||||||
if (result.error.isNotEmpty()) {
|
if (result.error.isNotEmpty()) {
|
||||||
builder.add("error:\n")
|
builder.add("error:\n")
|
||||||
builder.add(result.error)
|
builder.add(result.error)
|
||||||
@ -75,8 +80,9 @@ object JCC : KotlinPlugin(
|
|||||||
builder.add(result.stderr)
|
builder.add(result.stderr)
|
||||||
msgLength += result.stderr.length
|
msgLength += result.stderr.length
|
||||||
}
|
}
|
||||||
|
}
|
||||||
val messageChain = builder.build()
|
val messageChain = builder.build()
|
||||||
if (msgLength > 500) {
|
if (msgLength > MSG_MAX_LENGTH) {
|
||||||
val messageContent = messageChain.contentToString()
|
val messageContent = messageChain.contentToString()
|
||||||
return@reply "消息内容过长,已贴到Pastebin:\n" + UbuntuPastebinHelper.paste(messageContent)
|
return@reply "消息内容过长,已贴到Pastebin:\n" + UbuntuPastebinHelper.paste(messageContent)
|
||||||
} else {
|
} else {
|
||||||
@ -87,7 +93,6 @@ object JCC : KotlinPlugin(
|
|||||||
return@reply "执行失败\n原因:${e.message}"
|
return@reply "执行失败\n原因:${e.message}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return@reply "请输入正确的命令!例如:\n$CMD_PREFIX python print(\"Hello world\")"
|
return@reply "请输入正确的命令!例如:\n$CMD_PREFIX python print(\"Hello world\")"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import JCC.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
|
||||||
|
|
||||||
@ -19,6 +20,17 @@ object JccCommand : CompositeCommand(
|
|||||||
@SubCommand
|
@SubCommand
|
||||||
@Description("帮助")
|
@Description("帮助")
|
||||||
suspend fun CommandSender.help() {
|
suspend fun CommandSender.help() {
|
||||||
sendMessage("直接调用jcc即可运行代码\n例如:jcc python print(\"Hello world\")\n其它指令:\n$usage")
|
sendMessage("直接调用${CMD_PREFIX}即可运行代码\n例如:${CMD_PREFIX} python print(\"Hello world\")\n其它指令:\n$usage")
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubCommand
|
||||||
|
@Description("获取指定语言的模板")
|
||||||
|
suspend fun CommandSender.template(language: String) {
|
||||||
|
if (!GlotAPI.checkSupport(language)) {
|
||||||
|
sendMessage("不支持该语言,请使用/jcc list列出所有支持的编程语言")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val file = GlotAPI.getTemplateFile(language)
|
||||||
|
sendMessage("$CMD_PREFIX $language\n" + file.content)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -53,7 +53,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/
|
||||||
*/
|
*/
|
||||||
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!")
|
||||||
if (content.isEmpty())
|
if (content.isEmpty())
|
||||||
|
Loading…
Reference in New Issue
Block a user