添加简易在线运行代码功能

This commit is contained in:
2081878832 2021-04-17 12:07:06 +08:00
parent e2a7f01e1b
commit 0a57f3d78f
2 changed files with 44 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import (
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/github" _ "github.com/Yiwen-Chan/ZeroBot-Plugin/github"
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/manager" _ "github.com/Yiwen-Chan/ZeroBot-Plugin/manager"
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/music" _ "github.com/Yiwen-Chan/ZeroBot-Plugin/music"
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/runcode"
//_ "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime" //_ "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime"
) )

View File

@ -1,4 +1,4 @@
package program package runcode
import ( import (
"encoding/json" "encoding/json"
@ -12,21 +12,61 @@ import (
) )
func init() { func init() {
runAllow := true
runTypes := map[string][2]string{ runTypes := map[string][2]string{
"Py2": {"0","py"},
"Ruby": {"1","rb"},
"PHP": {"3","php"},
"Go": {"6","go"}, "Go": {"6","go"},
"C": {"7","c"}, "C": {"7","c"},
"C++": {"7","cpp"}, "C++": {"7","cpp"},
"Java": {"8","java"}, "Java": {"8","java"},
"Rust": {"9","rs"},
"C#": {"10","cs"}, "C#": {"10","cs"},
"Perl": {"14","pl"},
"Python": {"15","py3"}, "Python": {"15","py3"},
"Swift": {"16","swift"},
"Lua": {"17","lua"}, "Lua": {"17","lua"},
} }
zero.OnCommand("runList").Handle(func(ctx *zero.Ctx) { zero.OnCommand("runList").Handle(func(ctx *zero.Ctx) {
ctx.Send("Run 语种<<<\n代码块\n>>>\n支持语种: Go || Python || Java || C/C++ || C# || Lua") ctx.Send(`[使用说明]
Run 语种<<<
代码块
>>>
[支持语种]
Go || Python || Java || C/C++ || C# || Lua
Rust || PHP || Perl || Ruby || Swift || Py2`)
})
zero.OnCommand("runOpen").Handle(func(ctx *zero.Ctx) {
if ctx.Event.UserID == 213864964{
runAllow = true
ctx.Send(fmt.Sprintf(
"[CQ:at,qq=%d]在线运行代码功能已启用",
ctx.Event.UserID,
))
}
})
zero.OnCommand("runClose").Handle(func(ctx *zero.Ctx) {
if ctx.Event.UserID == 213864964{
runAllow = false
ctx.Send(fmt.Sprintf(
"[CQ:at,qq=%d]在线运行代码功能已禁用",
ctx.Event.UserID,
))
}
}) })
zero.OnRegex("(?is:Run (.+?)<<<(.+?)>>>)").Handle(func(ctx *zero.Ctx) { zero.OnRegex("(?is:Run (.+?)<<<(.+?)>>>)").Handle(func(ctx *zero.Ctx) {
if runAllow==false{
ctx.Send(fmt.Sprintf(
"[CQ:at,qq=%d]在线运行代码功能已被禁用",
ctx.Event.UserID,
))
return
}
getType := ctx.State["regex_matched"].([]string)[1] getType := ctx.State["regex_matched"].([]string)[1]
if runType,exist:=runTypes[getType];exist{ if runType,exist:=runTypes[getType];exist{
println("正在尝试执行",getType,"代码块") println("正在尝试执行",getType,"代码块")
@ -99,4 +139,4 @@ func netPost(api string,data map[string]string,headers map[string]string) []byte
defer res.Body.Close() defer res.Body.Close()
result,_ := ioutil.ReadAll(res.Body) result,_ := ioutil.ReadAll(res.Body)
return result return result
} }