2
2
package runcode
3
3
4
4
import (
5
- "errors"
6
- "io"
7
- "net/http"
8
- "net/url"
9
5
"strings"
10
- "time"
11
6
7
+ "github.com/FloatTech/AnimeAPI/runoob"
12
8
ctrl "github.com/FloatTech/zbpctrl"
13
9
"github.com/FloatTech/zbputils/control"
14
10
"github.com/FloatTech/zbputils/ctxext"
15
11
zero "github.com/wdvxdr1123/ZeroBot"
16
12
"github.com/wdvxdr1123/ZeroBot/message"
17
-
18
- "github.com/tidwall/gjson"
19
13
)
20
14
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" )
93
16
94
17
func init () {
95
18
control .Register ("runcode" , & ctrl.Options [* zero.Ctx ]{
@@ -108,7 +31,7 @@ func init() {
108
31
israw := ctx .State ["regex_matched" ].([]string )[1 ] != ""
109
32
language := ctx .State ["regex_matched" ].([]string )[2 ]
110
33
language = strings .ToLower (language )
111
- if runType , exist := table [language ]; ! exist {
34
+ if _ , exist := runoob . LangTable [language ]; ! exist {
112
35
// 不支持语言
113
36
ctx .SendChain (
114
37
message .Text ("> " , ctx .Event .Sender .NickName , "\n " ),
@@ -123,18 +46,19 @@ func init() {
123
46
message .Text ("> " , ctx .Event .Sender .NickName , " " , language , "-template:\n " ),
124
47
message .Text (
125
48
">runcode " , language , "\n " ,
126
- templates [language ],
49
+ runoob . Templates [language ],
127
50
),
128
51
)
129
52
default :
130
- if output , err := runCode (block , runType ); err != nil {
53
+ if output , err := ro . Run (block , language , "" ); err != nil {
131
54
// 运行失败
132
55
ctx .SendChain (
133
56
message .Text ("> " , ctx .Event .Sender .NickName , "\n " ),
134
57
message .Text ("ERROR: " , err ),
135
58
)
136
59
} else {
137
60
// 运行成功
61
+ output = cutTooLong (strings .Trim (output , "\n " ))
138
62
if israw {
139
63
ctx .SendChain (message .Text (output ))
140
64
} else {
@@ -149,59 +73,6 @@ func init() {
149
73
})
150
74
}
151
75
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
-
205
76
// 截断过长文本
206
77
func cutTooLong (text string ) string {
207
78
temp := []rune (text )
0 commit comments