mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-20 14:30:10 +08:00
🎨 优化代码结构
This commit is contained in:
parent
78cd9e2f9b
commit
fafd95098c
@ -1,142 +1,168 @@
|
|||||||
package runcode
|
package runcode
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
|
"github.com/wdvxdr1123/ZeroBot/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
runAllow := true
|
RunAllow := true
|
||||||
runTypes := map[string][2]string{
|
table := map[string][2]string{
|
||||||
"Py2": {"0","py"},
|
"py2": {"0", "py"},
|
||||||
"Ruby": {"1","rb"},
|
"ruby": {"1", "rb"},
|
||||||
"PHP": {"3","php"},
|
"rb": {"1", "rb"},
|
||||||
"Go": {"6","go"},
|
"php": {"3", "php"},
|
||||||
"C": {"7","c"},
|
"javascript": {"4", "js"},
|
||||||
"C++": {"7","cpp"},
|
"js": {"4", "js"},
|
||||||
"Java": {"8","java"},
|
"node.js": {"4", "js"},
|
||||||
"Rust": {"9","rs"},
|
"scala": {"5", "scala"},
|
||||||
"C#": {"10","cs"},
|
"go": {"6", "go"},
|
||||||
"Perl": {"14","pl"},
|
"c": {"7", "c"},
|
||||||
"Python": {"15","py3"},
|
"c++": {"7", "cpp"},
|
||||||
"Swift": {"16","swift"},
|
"cpp": {"7", "cpp"},
|
||||||
"Lua": {"17","lua"},
|
"java": {"8", "java"},
|
||||||
|
"rust": {"9", "rs"},
|
||||||
|
"rs": {"9", "rs"},
|
||||||
|
"c#": {"10", "cs"},
|
||||||
|
"cs": {"10", "cs"},
|
||||||
|
"csharp": {"10", "cs"},
|
||||||
|
"shell": {"10", "sh"},
|
||||||
|
"bash": {"10", "sh"},
|
||||||
|
"erlang": {"12", "erl"},
|
||||||
|
"perl": {"14", "pl"},
|
||||||
|
"python": {"15", "py3"},
|
||||||
|
"py": {"15", "py3"},
|
||||||
|
"swift": {"16", "swift"},
|
||||||
|
"lua": {"17", "lua"},
|
||||||
|
"pascal": {"18", "pas"},
|
||||||
|
"kotlin": {"19", "kt"},
|
||||||
|
"kt": {"19", "kt"},
|
||||||
|
"r": {"80", "r"},
|
||||||
|
"vb": {"84", "vb"},
|
||||||
|
"typescript": {"1010", "ts"},
|
||||||
|
"ts": {"1010", "ts"},
|
||||||
}
|
}
|
||||||
|
|
||||||
zero.OnCommand("runList").Handle(func(ctx *zero.Ctx) {
|
zero.OnFullMatch(">runcode help").SetBlock(true).FirstPriority().
|
||||||
ctx.Send(`[使用说明]
|
Handle(func(ctx *zero.Ctx) {
|
||||||
Run 语种<<<
|
ctx.SendChain(message.Text(
|
||||||
代码块
|
"使用说明: ", "\n",
|
||||||
>>>
|
">runcode [language] [code block]", "\n",
|
||||||
[支持语种]
|
"支持语种: ", "\n",
|
||||||
Go || Python || Java || C/C++ || C# || Lua
|
"Go || Python || C/C++ || C# || Java || Lua ", "\n",
|
||||||
Rust || PHP || Perl || Ruby || Swift || Py2`)
|
"JavaScript || TypeScript || PHP || Shell ", "\n",
|
||||||
|
"Kotlin || Rust || Erlang || Ruby || Swift ", "\n",
|
||||||
|
"R || VB || Py2 || Perl || Pascal || Scala ", "\n",
|
||||||
|
))
|
||||||
|
})
|
||||||
|
zero.OnFullMatch(">runcode on", zero.AdminPermission).SetBlock(true).FirstPriority().
|
||||||
|
Handle(func(ctx *zero.Ctx) {
|
||||||
|
RunAllow = true
|
||||||
|
ctx.SendChain(
|
||||||
|
message.Text("> ", ctx.Event.Sender.NickName, "\n"),
|
||||||
|
message.Text("在线运行代码功能已启用"),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
zero.OnCommand("runOpen").Handle(func(ctx *zero.Ctx) {
|
zero.OnFullMatch(">runcode off", zero.AdminPermission).SetBlock(true).FirstPriority().
|
||||||
if ctx.Event.UserID == 213864964{
|
Handle(func(ctx *zero.Ctx) {
|
||||||
runAllow = true
|
RunAllow = false
|
||||||
ctx.Send(fmt.Sprintf(
|
ctx.SendChain(
|
||||||
"[CQ:at,qq=%d]在线运行代码功能已启用",
|
message.Text("> ", ctx.Event.Sender.NickName, "\n"),
|
||||||
ctx.Event.UserID,
|
message.Text("在线运行代码功能已禁用"),
|
||||||
))
|
)
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
zero.OnCommand("runClose").Handle(func(ctx *zero.Ctx) {
|
zero.OnRegex(`>runcode\s(.+?)\s([\s\S]+)`).SetBlock(true).SecondPriority().
|
||||||
if ctx.Event.UserID == 213864964{
|
Handle(func(ctx *zero.Ctx) {
|
||||||
runAllow = false
|
language := ctx.State["regex_matched"].([]string)[1]
|
||||||
ctx.Send(fmt.Sprintf(
|
language = strings.ToLower(language)
|
||||||
"[CQ:at,qq=%d]在线运行代码功能已禁用",
|
if runType, exist := table[language]; !exist {
|
||||||
ctx.Event.UserID,
|
// 不支持语言
|
||||||
))
|
ctx.SendChain(
|
||||||
}
|
message.Text("> ", ctx.Event.Sender.NickName, "\n"),
|
||||||
})
|
message.Text("语言不是受支持的编程语种呢~"),
|
||||||
|
)
|
||||||
zero.OnRegex("(?is:Run (.+?)<<<(.+?)>>>)").Handle(func(ctx *zero.Ctx) {
|
return
|
||||||
if runAllow==false{
|
} else {
|
||||||
ctx.Send(fmt.Sprintf(
|
if RunAllow == false {
|
||||||
"[CQ:at,qq=%d]在线运行代码功能已被禁用",
|
// 运行代码被禁用
|
||||||
ctx.Event.UserID,
|
ctx.SendChain(
|
||||||
))
|
message.Text("> ", ctx.Event.Sender.NickName, "\n"),
|
||||||
|
message.Text("在线运行代码功能已被禁用"),
|
||||||
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
getType := ctx.State["regex_matched"].([]string)[1]
|
// 执行运行
|
||||||
if runType,exist:=runTypes[getType];exist{
|
block := ctx.State["regex_matched"].([]string)[2]
|
||||||
println("正在尝试执行",getType,"代码块")
|
block = message.UnescapeCQCodeText(block)
|
||||||
getCode := ctx.State["regex_matched"].([]string)[2]
|
if output, err := runCode(block, runType); err != nil {
|
||||||
getCode = strings.Replace(getCode,"[","[",-1)
|
// 运行失败
|
||||||
getCode = strings.Replace(getCode,"]","]",-1)
|
ctx.SendChain(
|
||||||
|
message.Text("> ", ctx.Event.Sender.NickName, "\n"),
|
||||||
res := runCode(getCode,runType)
|
message.Text("ERROR: ", err),
|
||||||
if res["errors"] == "\n\n"{
|
)
|
||||||
ctx.Send(fmt.Sprintf(
|
return
|
||||||
"[CQ:at,qq=%d]本次%s语言代码执行结果如下:\n%s",
|
} else {
|
||||||
ctx.Event.UserID,
|
// 运行成功
|
||||||
getType,
|
ctx.SendChain(
|
||||||
res["output"][:len(res["output"])-1],
|
message.Text("> ", ctx.Event.Sender.NickName, "\n"),
|
||||||
))
|
message.Text(output),
|
||||||
}else {
|
)
|
||||||
ctx.Send(fmt.Sprintf(
|
return
|
||||||
"[CQ:at,qq=%d]本次%s语言代码执行失败:%s",
|
|
||||||
ctx.Event.UserID,
|
|
||||||
getType,
|
|
||||||
res["errors"],
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}else {
|
|
||||||
ctx.Send(fmt.Sprintf(
|
|
||||||
"[CQ:at,qq=%d][%s]语言不是受支持的编程语种呢~",
|
|
||||||
ctx.Event.UserID,
|
|
||||||
getType,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCode(code string,runType [2]string) map[string]string {
|
func runCode(code string, runType [2]string) (string, error) {
|
||||||
//对菜鸟api发送数据并返回结果
|
// 对菜鸟api发送数据并返回结果
|
||||||
result := map[string]string{}
|
|
||||||
api := "https://tool.runoob.com/compile2.php"
|
api := "https://tool.runoob.com/compile2.php"
|
||||||
headers := map[string]string{
|
|
||||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
||||||
"Referer": "https://c.runoob.com/",
|
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0",
|
|
||||||
}
|
|
||||||
data := map[string]string{
|
|
||||||
"code": code,
|
|
||||||
"token": "4381fe197827ec87cbac9552f14ec62a",
|
|
||||||
"stdin": "",
|
|
||||||
"language": runType[0],
|
|
||||||
"fileext": runType[1],
|
|
||||||
}
|
|
||||||
json.Unmarshal(netPost(api,data,headers),&result)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func netPost(api string,data map[string]string,headers map[string]string) []byte {
|
header := http.Header{
|
||||||
//发送POST请求获取返回数据
|
"Content-Type": []string{"application/x-www-form-urlencoded; charset=UTF-8"},
|
||||||
|
"Referer": []string{"https://c.runoob.com/"},
|
||||||
|
"User-Agent": []string{"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0"},
|
||||||
|
}
|
||||||
|
|
||||||
|
val := url.Values{
|
||||||
|
"code": []string{code},
|
||||||
|
"token": []string{"4381fe197827ec87cbac9552f14ec62a"},
|
||||||
|
"stdin": []string{""},
|
||||||
|
"language": []string{runType[0]},
|
||||||
|
"fileext": []string{runType[1]},
|
||||||
|
}
|
||||||
|
// 发送请求
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: time.Duration(6 * time.Second),
|
Timeout: time.Duration(6 * time.Second),
|
||||||
}
|
}
|
||||||
|
request, _ := http.NewRequest("POST", api, strings.NewReader(val.Encode()))
|
||||||
param := url.Values{}
|
request.Header = header
|
||||||
for key,value := range data{
|
body, err := client.Do(request)
|
||||||
param.Set(key,value)
|
if err != nil {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
|
defer body.Body.Close()
|
||||||
request,_ := http.NewRequest("POST",api,strings.NewReader(param.Encode()))
|
if body.StatusCode != http.StatusOK {
|
||||||
for key,value := range headers{
|
return "", fmt.Errorf("code %d", body.StatusCode)
|
||||||
request.Header.Add(key,value)
|
|
||||||
}
|
}
|
||||||
res,_ := client.Do(request)
|
res, err := ioutil.ReadAll(body.Body)
|
||||||
defer res.Body.Close()
|
if err != nil {
|
||||||
result,_ := ioutil.ReadAll(res.Body)
|
return "", err
|
||||||
return result
|
}
|
||||||
|
// 结果处理
|
||||||
|
content := gjson.ParseBytes(res)
|
||||||
|
if e := content.Get("errors").Str; e != "\n\n" {
|
||||||
|
return "", fmt.Errorf(e)
|
||||||
|
}
|
||||||
|
output := content.Get("output").Str
|
||||||
|
return output[:len(output)-1], nil
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user