22package runcode
33
44import (
5- "errors"
6- "io"
7- "net/http"
8- "net/url"
95 "strings"
10- "time"
116
7+ "github.com/FloatTech/AnimeAPI/runoob"
128 ctrl "github.com/FloatTech/zbpctrl"
139 "github.com/FloatTech/zbputils/control"
1410 "github.com/FloatTech/zbputils/ctxext"
1511 zero "github.com/wdvxdr1123/ZeroBot"
1612 "github.com/wdvxdr1123/ZeroBot/message"
17-
18- "github.com/tidwall/gjson"
1913)
2014
21- var (
22- templates = map [string ]string {
23- "py2" : "print 'Hello World!'" ,
24- "ruby" : "puts \" Hello World!\" ;" ,
25- "rb" : "puts \" Hello World!\" ;" ,
26- "php" : "<?php\n \t echo 'Hello World!';\n ?>" ,
27- "javascript" : "console.log(\" Hello World!\" );" ,
28- "js" : "console.log(\" Hello World!\" );" ,
29- "node.js" : "console.log(\" Hello World!\" );" ,
30- "scala" : "object Main {\n def main(args:Array[String])\n {\n println(\" Hello World!\" )\n }\n \t \t \n }" ,
31- "go" : "package main\n \n import \" fmt\" \n \n func main() {\n fmt.Println(\" Hello, World!\" )\n }" ,
32- "c" : "#include <stdio.h>\n \n int main()\n {\n printf(\" Hello, World! \n \" );\n return 0;\n }" ,
33- "c++" : "#include <iostream>\n using namespace std;\n \n int main()\n {\n cout << \" Hello World\" ;\n return 0;\n }" ,
34- "cpp" : "#include <iostream>\n using namespace std;\n \n int main()\n {\n cout << \" Hello World\" ;\n return 0;\n }" ,
35- "java" : "public class HelloWorld {\n public static void main(String []args) {\n System.out.println(\" Hello World!\" );\n }\n }" ,
36- "rust" : "fn main() {\n println!(\" Hello World!\" );\n }" ,
37- "rs" : "fn main() {\n println!(\" Hello World!\" );\n }" ,
38- "c#" : "using System;\n namespace HelloWorldApplication\n {\n class HelloWorld\n {\n static void Main(string[] args)\n {\n Console.WriteLine(\" Hello World!\" );\n }\n }\n }" ,
39- "cs" : "using System;\n namespace HelloWorldApplication\n {\n class HelloWorld\n {\n static void Main(string[] args)\n {\n Console.WriteLine(\" Hello World!\" );\n }\n }\n }" ,
40- "csharp" : "using System;\n namespace HelloWorldApplication\n {\n class HelloWorld\n {\n static void Main(string[] args)\n {\n Console.WriteLine(\" Hello World!\" );\n }\n }\n }" ,
41- "shell" : "echo 'Hello World!'" ,
42- "bash" : "echo 'Hello World!'" ,
43- "erlang" : "% escript will ignore the first line\n \n main(_) ->\n io:format(\" Hello World!~n\" )." ,
44- "perl" : "print \" Hello, World!\n \" ;" ,
45- "python" : "print(\" Hello, World!\" )" ,
46- "py" : "print(\" Hello, World!\" )" ,
47- "swift" : "var myString = \" Hello, World!\" \n print(myString)" ,
48- "lua" : "var myString = \" Hello, World!\" \n print(myString)" ,
49- "pascal" : "runcode Hello;\n begin\n writeln ('Hello, world!')\n end." ,
50- "kotlin" : "fun main(args : Array<String>){\n println(\" Hello World!\" )\n }" ,
51- "kt" : "fun main(args : Array<String>){\n println(\" Hello World!\" )\n }" ,
52- "r" : "myString <- \" Hello, World!\" \n print ( myString)" ,
53- "vb" : "Module Module1\n \n Sub Main()\n Console.WriteLine(\" Hello World!\" )\n End Sub\n \n End Module" ,
54- "typescript" : "const hello : string = \" Hello World!\" \n console.log(hello)" ,
55- "ts" : "const hello : string = \" Hello World!\" \n console.log(hello)" ,
56- }
57- table = map [string ][2 ]string {
58- "py2" : {"0" , "py" },
59- "ruby" : {"1" , "rb" },
60- "rb" : {"1" , "rb" },
61- "php" : {"3" , "php" },
62- "javascript" : {"4" , "js" },
63- "js" : {"4" , "js" },
64- "node.js" : {"4" , "js" },
65- "scala" : {"5" , "scala" },
66- "go" : {"6" , "go" },
67- "c" : {"7" , "c" },
68- "c++" : {"7" , "cpp" },
69- "cpp" : {"7" , "cpp" },
70- "java" : {"8" , "java" },
71- "rust" : {"9" , "rs" },
72- "rs" : {"9" , "rs" },
73- "c#" : {"10" , "cs" },
74- "cs" : {"10" , "cs" },
75- "csharp" : {"10" , "cs" },
76- "shell" : {"10" , "sh" },
77- "bash" : {"10" , "sh" },
78- "erlang" : {"12" , "erl" },
79- "perl" : {"14" , "pl" },
80- "python" : {"15" , "py3" },
81- "py" : {"15" , "py3" },
82- "swift" : {"16" , "swift" },
83- "lua" : {"17" , "lua" },
84- "pascal" : {"18" , "pas" },
85- "kotlin" : {"19" , "kt" },
86- "kt" : {"19" , "kt" },
87- "r" : {"80" , "r" },
88- "vb" : {"84" , "vb" },
89- "typescript" : {"1010" , "ts" },
90- "ts" : {"1010" , "ts" },
91- }
92- )
15+ var ro = runoob .NewRunOOB ("b6365362a90ac2ac7098ba52c13e352b" )
9316
9417func init () {
9518 control .Register ("runcode" , & ctrl.Options [* zero.Ctx ]{
@@ -108,7 +31,7 @@ func init() {
10831 israw := ctx .State ["regex_matched" ].([]string )[1 ] != ""
10932 language := ctx .State ["regex_matched" ].([]string )[2 ]
11033 language = strings .ToLower (language )
111- if runType , exist := table [language ]; ! exist {
34+ if _ , exist := runoob . LangTable [language ]; ! exist {
11235 // 不支持语言
11336 ctx .SendChain (
11437 message .Text ("> " , ctx .Event .Sender .NickName , "\n " ),
@@ -123,18 +46,19 @@ func init() {
12346 message .Text ("> " , ctx .Event .Sender .NickName , " " , language , "-template:\n " ),
12447 message .Text (
12548 ">runcode " , language , "\n " ,
126- templates [language ],
49+ runoob . Templates [language ],
12750 ),
12851 )
12952 default :
130- if output , err := runCode (block , runType ); err != nil {
53+ if output , err := ro . Run (block , language , "" ); err != nil {
13154 // 运行失败
13255 ctx .SendChain (
13356 message .Text ("> " , ctx .Event .Sender .NickName , "\n " ),
13457 message .Text ("ERROR: " , err ),
13558 )
13659 } else {
13760 // 运行成功
61+ output = cutTooLong (strings .Trim (output , "\n " ))
13862 if israw {
13963 ctx .SendChain (message .Text (output ))
14064 } else {
@@ -149,59 +73,6 @@ func init() {
14973 })
15074}
15175
152- func runCode (code string , runType [2 ]string ) (string , error ) {
153- // 对菜鸟api发送数据并返回结果
154- api := "https://tool.runoob.com/compile2.php"
155-
156- header := http.Header {
157- "Content-Type" : []string {"application/x-www-form-urlencoded; charset=UTF-8" },
158- "Referer" : []string {"https://c.runoob.com/" },
159- "User-Agent" : []string {"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0" },
160- }
161-
162- val := url.Values {
163- "code" : []string {code },
164- "token" : []string {"4381fe197827ec87cbac9552f14ec62a" },
165- "stdin" : []string {"" },
166- "language" : []string {runType [0 ]},
167- "fileext" : []string {runType [1 ]},
168- }
169- // 发送请求
170- client := & http.Client {
171- Timeout : 15 * time .Second ,
172- }
173- request , _ := http .NewRequest ("POST" , api , strings .NewReader (val .Encode ()))
174- request .Header = header
175- body , err := client .Do (request )
176- if err != nil {
177- return "" , err
178- }
179- defer body .Body .Close ()
180- if body .StatusCode != http .StatusOK {
181- return "" , errors .New ("code not 200" )
182- }
183- res , err := io .ReadAll (body .Body )
184- if err != nil {
185- return "" , err
186- }
187- // 结果处理
188- content := gjson .ParseBytes (res )
189- if e := content .Get ("errors" ).Str ; e != "\n \n " {
190- return "" , errors .New (cutTooLong (clearNewLineSuffix (e )))
191- }
192- output := content .Get ("output" ).Str
193-
194- return cutTooLong (clearNewLineSuffix (output )), nil
195- }
196-
197- // 清除末尾多余的换行符
198- func clearNewLineSuffix (text string ) string {
199- for strings .HasSuffix (text , "\n " ) {
200- text = text [:len (text )- 1 ]
201- }
202- return text
203- }
204-
20576// 截断过长文本
20677func cutTooLong (text string ) string {
20778 temp := []rune (text )
0 commit comments