-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
105 lines (100 loc) · 3.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const Discord = require("discord.js");
const client = new Discord.Client();
var request = require("request");
const runCode = require("jdoodlecoderunner")
require("dotenv").config();
const prefix = "coder ";
client.on("ready", () => {
console.log("coder-BOT is online!");
});
client.on("message", async (message) => {
try {
let args = message.content.substring(prefix.length).split(" ");
switch (args[0]) {
case "run":
var input = "";
var language, index;
if (args[1] == "C" || args[1] == "c") {
language = "c";
index = "0";
} else if (args[1] == "python") {
language = "python3";
index = "3";
}
var fileName = message.attachments.array()[0];
for (var i = 2; i < args.length; i++) input += args[i] + " ";
request.get(fileName.url, (err, response, body) => {
const runRequestBody = {
script: body,
language: language,
stdin: input,
versionIndex: index,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
};
request
.post({
url: "https://api.jdoodle.com/execute",
json: runRequestBody,
})
.on("error", (error) => {
console.log("request.post error", error);
return;
})
.on("data", (data) => {
const parsedData = JSON.parse(data.toString());
if (parsedData.error) {
return;
} else {
var output = "";
for (var i = 0; i < parsedData.output.length; i++) {
if (parsedData.output[i] == "\n") output += "\n";
else output += parsedData.output[i];
}
let outputOfProgram = new Discord.MessageEmbed()
.setAuthor(client.user.username)
.setDescription("Result!!!")
.setColor("33FFB3")
.setDescription(output);
message.channel.send(outputOfProgram);
return;
}
});
});
break;
case "runjdoodle":
var input = "";
var language, index;
if (args[1] == "C" || args[1] == "c") {
language = "c";
index = "0";
} else if (args[1] == "python") {
language = "python3";
index = "3";
}
var fileName = message.attachments.array()[0];
for (var i = 2; i < args.length; i++) input += args[i] + " ";
var output=""
output=await runCode.runCode(fileName.url,language,index,input,process.env.CLIENT_ID,process.env.CLIENT_SECRET)
if(output)
{
let outputOfProgram = new Discord.MessageEmbed()
.setAuthor(client.user.username)
.setDescription("Result!!!")
.setColor("33FFB3")
.setDescription(output);
message.channel.send(outputOfProgram);
}
break;
}
} catch (Exception) {
console.log(Exception);
let Errorbotembed = new Discord.MessageEmbed()
.setAuthor(client.user.username)
.setDescription("Error!!!")
.setColor("FFFFFF")
.setDescription("Error Encountered\nFor Help use\nDM help");
message.channel.send(Errorbotembed);
}
});
client.login(process.env.CLIENT_TOKEN);