@@ -8,67 +8,62 @@ export const command = "run";
88export const role = "admin" ;
99
1010export default async function ( msg : Message ) {
11- try {
12- const body = msg . body . trim ( ) ;
13- const match = body . match ( / ^ r u n \s + ( p y t h o n | j a v a | c | j s | p h p ) \s * \r ? \n ( [ \s \S ] + ) / i) ;
11+ const body = msg . body . trim ( ) ;
12+ const match = body . match ( / ^ r u n \s + ( p y t h o n | j a v a | c | j s | p h p ) \s * \r ? \n ( [ \s \S ] + ) / i) ;
1413
15- log . info ( "Command" , `Received body: ${ JSON . stringify ( body ) } ` ) ;
16- if ( ! match ) {
17- log . info ( "Command" , "Invalid run command format" ) ;
18- await msg . reply (
19- "Please use the format:\n\nrun python\n<code>\n\nor\n\nrun java\n<code>"
20- ) ;
21- return ;
22- }
14+ log . info ( "Command" , `Received body: ${ JSON . stringify ( body ) } ` ) ;
15+ if ( ! match ) {
16+ log . info ( "Command" , "Invalid run command format" ) ;
17+ await msg . reply (
18+ "Please use the format:\n\nrun python\n<code>\n\nor\n\nrun java\n<code>"
19+ ) ;
20+ return ;
21+ }
2322
24- const lang = match [ 1 ] . toLowerCase ( ) ;
25- const code = match [ 2 ] ;
23+ const lang = match [ 1 ] . toLowerCase ( ) ;
24+ const code = match [ 2 ] ;
2625
27- let command : string ;
28- let tempFile : string ;
26+ let command : string ;
27+ let tempFile : string ;
2928
30- const tempDir = "./.temp" ;
31- await fs . mkdir ( tempDir , { recursive : true } ) ;
29+ const tempDir = "./.temp" ;
30+ await fs . mkdir ( tempDir , { recursive : true } ) ;
3231
33- if ( lang === "python" ) {
34- tempFile = `${ tempDir } /run.py` ;
35- command = `python3 "${ tempFile } "` ;
36- } else if ( lang === "java" ) {
37- tempFile = `${ tempDir } /run.java` ;
38- command = `javac "${ tempFile } " && java -cp /tmp Code` ;
39- } else if ( lang === "c" ) {
40- tempFile = `${ tempDir } /run.c` ;
41- command = `gcc "${ tempFile } " -o /tmp/code && /tmp/code` ;
42- } else if ( lang === "js" ) {
43- tempFile = `${ tempDir } /code.js` ;
44- command = `node "${ tempFile } "` ;
45- } else if ( lang === "php" ) {
46- tempFile = `${ tempDir } /run.php` ;
47- command = `php "${ tempFile } "` ;
48- } else {
49- await msg . reply ( "Unsupported language." ) ;
50- return ;
51- }
32+ if ( lang === "python" ) {
33+ tempFile = `${ tempDir } /run.py` ;
34+ command = `python3 "${ tempFile } "` ;
35+ } else if ( lang === "java" ) {
36+ tempFile = `${ tempDir } /run.java` ;
37+ command = `javac "${ tempFile } " && java -cp /tmp Code` ;
38+ } else if ( lang === "c" ) {
39+ tempFile = `${ tempDir } /run.c` ;
40+ command = `gcc "${ tempFile } " -o /tmp/code && /tmp/code` ;
41+ } else if ( lang === "js" ) {
42+ tempFile = `${ tempDir } /code.js` ;
43+ command = `node "${ tempFile } "` ;
44+ } else if ( lang === "php" ) {
45+ tempFile = `${ tempDir } /run.php` ;
46+ command = `php "${ tempFile } "` ;
47+ } else {
48+ await msg . reply ( "Unsupported language." ) ;
49+ return ;
50+ }
5251
53- await fs . writeFile ( tempFile , code ) ;
52+ await fs . writeFile ( tempFile , code ) ;
5453
55- const execPromise = util . promisify ( exec ) ;
54+ const execPromise = util . promisify ( exec ) ;
5655
57- try {
58- const { stdout, stderr } = await execPromise ( command , {
59- timeout : 10000 ,
60- maxBuffer : 1024 * 1024 ,
61- } ) ;
62- let response = stdout || stderr || "No output." ;
63- if ( response . length > 4000 ) {
64- response = response . slice ( 0 , 4000 ) + "\n\n[Output truncated]" ;
65- }
66- await msg . reply ( "```" + response + "```" ) ;
67- } catch ( err : any ) {
68- await msg . reply ( "Error executing code:\n" + ( err . stderr || err . message ) ) ;
56+ try {
57+ const { stdout, stderr } = await execPromise ( command , {
58+ timeout : 10000 ,
59+ maxBuffer : 1024 * 1024 ,
60+ } ) ;
61+ let response = stdout || stderr || "No output." ;
62+ if ( response . length > 4000 ) {
63+ response = response . slice ( 0 , 4000 ) + "\n\n[Output truncated]" ;
6964 }
70- } catch ( error ) {
71- log . error ( "Command" , "Error occured while processing the request:" , error ) ;
72- await msg . reply ( "An error occurred while processing your request." ) ;
65+ await msg . reply ( "```" + response + "```" ) ;
66+ } catch ( err : any ) {
67+ await msg . reply ( "Error executing code:\n" + ( err . stderr || err . message ) ) ;
7368 }
7469}
0 commit comments