-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
274 lines (257 loc) · 7.79 KB
/
bot.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
{"commands":{"argstest":{"code":"channel.send(args);","info":""},"sum":{"code":" \nvar result = 0; \nfor (var i = 0; i < args.length; i++) { \n result += Number(args[i]); \n} \nchannel.send(args.join(\" + \") + \" = \" + result); \n","info":""},"calc":{"code":" \nargs = message.content.split(\" \"); \nif (args[1] == \"+\") { \n channel.send(Number(args[0]) + Number(args[2])); } \nelse if (args[1] == \"-\"){ \n channel.send(Number(args[0]) - Number(args[2])); } \nelse if (args[1] == \"*\") { \n channel.send(Number(args[0]) * Number(args[2])); } \nelse if (args[1] == \"/\") { \n channel.send(Number(args[0]) / Number(args[2])); } \nelse if (args[1] == \"%\") { \n channel.send(Number(args[0]) / Number(args[2])); } \nelse { \n channel.send(\"wtf r u talking about\"); } \n","info":""},"remind":{"code":"var time = args[0]; \nargs.shift(); \nvar m = args.join(\" \"); \nsetTimeout(function() { \n channel.send(m); \n}, time * 1000);","info":""}},"global":{"count":2}}
*/
var vm = require("vm");
var fs = require("fs");
var prefix = "$";
var guild = process.argv[3];
var dataPath = __dirname + "/data/" + guild + ".json"
var saveInterval = 60 * 1000;
var commandNames = ["run", "create", "edit", "show", "info", "delete", "export", "import", "list", "help"];
var commands = {};
var globalData = {};
module.exports = function(client) {
loadData();
function run(command, options, message) {
try {
switch (command) {
case "run":
options.shift();
var code = extractCode(options.join(" "));
runScript(createScript(code, "<anonymous>"), message, null);
break;
case "create":
var name = options[0];
options.shift();
var code = extractCode(options.join(" "));
if (commandNames.indexOf(name) != -1) {
message.channel.send("Cannot create command `" + name + "`: it is a reserved name");
}
if (name in commands) {
message.channel.send("Cannot create command `" + name + "`: it already exists");
}
var success = true;
try {
commands[name] = {
script: createScript(code, name),
code: code,
info: ""
};
} catch(e) {
success = false;
message.channel.send("```" + formatError(e) + "```");
}
if (success) {
message.channel.send("Created command `" + name + "`");
}
break;
case "edit":
var name = options[0];
options.shift();
var code = extractCode(options.join(" "));
if (name in commands) {
var success = true;
try {
commands[name].script = createScript(code, name);
commands[name].code = code;
} catch(e) {
success = false;
message.channel.send("```" + formatError(e) + "```");
}
if (success) {
message.channel.send("Edited command `" + name + "`");
}
} else {
message.channel.send("Command not found: `" + name + "`");
}
break;
case "show":
var name = options[0];
if (name in commands) {
message.channel.send("```JS\n" + commands[name].code + "\n```");
}
break;
case "info":
var name = options[0];
if (name in commands) {
if (options.length > 1) {
options.shift();
var info = options.join(" ").trim();
commands[name].info = info;
message.channel.send("Saved info for `" + name + "`");
} else {
if (commands[name].info != "") {
message.channel.send("```" + name + ": " + commands[name].info + "```");
} else {
message.channel.send("No info set for `" + name + "`");
}
}
} else {
message.channel.send("Command not found: `" + name + "`");
}
break;
case "delete":
var name = options[0];
if (name in commands) {
delete commands[name];
message.channel.send("Deleted command `" + name + "`");
} else {
message.channel.send("Command not found: `" + name + "`");
}
break;
case "export":
var string = exportData();
message.channel.send("```JSON\n" + string + "\n```");
break;
case "import":
var data = options.join(" ");
var error = importData(data);
if (error != null) {
message.channel.send(error);
} else {
message.channel.send("Successfully imported data");
}
break;
case "list":
if (Object.keys(commands).length > 0) {
var string = "```\n";
for (var command in commands) {
string += command;
if (commands[command].info != "") {
string += ": " + commands[command].info;
}
string += "\n";
}
string += "```";
message.channel.send(string);
} else {
message.channel.send("No commands found");
}
break;
case "help":
message.channel.send("Help page coming soon!");
break;
default:
if (command in commands) {
message.content = options.join(" ");
runScript(commands[command].script, message, command);
} else {
message.channel.send("Command not found: `" + command + "`");
}
break;
}
} catch(e) {
console.log(e);
message.channel.send("Malformed input");
}
}
client.on("message", function(message) {
if (message.author != client.user && message.channel.guild.id == guild) {
var text = message.content;
if (text[0] == prefix) {
var parts = text.split("\n").join(" \n").split(" ");
var command = parts[0].substring(1);
parts.shift();
run(command, parts, message);
}
}
});
setInterval(saveData, saveInterval);
function exitHandler() {
saveData();
console.log("[" + guild + "] Process exiting");
process.exit();
}
process.on("exit", exitHandler);
process.on("SIGINT", exitHandler);
process.on("SIGUSR1", exitHandler);
process.on("SIGUSR2", exitHandler);
process.on("uncaughtException", exitHandler);
function loadData() {
if (fs.existsSync(dataPath)) {
importData(fs.readFileSync(dataPath, "utf8"));
} else {
saveData();
}
}
function saveData() {
fs.writeFileSync(dataPath, exportData());
}
function importData(data) {
data = JSON.parse(data);
for (var command in data.commands) {
try {
commands[command] = {
script: createScript(data.commands[command].code, command),
code: data.commands[command].code,
info: data.commands[command].info
};
} catch(e) {
return "Error when importing `" + command + "`\n```" + formatError(e) + "```";
}
for (var g in data.global) {
globalData[g] = data.global[g];
}
}
return null;
}
function exportData() {
var data = {};
data.commands = {};
data.global = globalData;
for (var command in commands) {
data.commands[command] = {
code: commands[command].code,
info: commands[command].info
};
}
return JSON.stringify(data);
}
function createScript(code, name) {
return new vm.Script(code, {
filename: name,
timeout: 500,
contextName: name,
displayErrors: true
});
}
function runScript(script, message, command) {
try {
script.runInNewContext({
command: command,
message: message,
text: message.content,
args: message.content.split(" "),
guild: message.channel.guild,
channel: message.channel,
user: message.author,
member: message.member,
global: globalData,
Buffer: Buffer,
clearImmediate: clearImmediate,
clearInterval: clearInterval,
clearTimeout: clearTimeout,
setImmediate: setImmediate,
setInterval: setInterval,
setTimeout: setTimeout
});
} catch(e) {
message.channel.send("```" + formatError(e) + "```");
}
}
function extractCode(code) {
code = code.trim();
if (code.substring(0, 13) == "```JavaScript") {
code = code.substring(5, code.length - 3);
} else if (code.substring(0, 5) == "```JS") {
code = code.substring(5, code.length - 3);
} else if (code.substring(0, 3) == "```") {
code = code.substring(3, code.length - 3);
}
return code;
}
function formatError(e) {
var parts = e.stack.split("\n\n");
return parts[0] + "\n" + parts[1].split("\n")[0];
}
}