forked from vze3f372/formatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodebot.js
334 lines (310 loc) · 9.49 KB
/
codebot.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'use strict';
const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
const configurer = require('@mstefan99/configurer');
const argumented = require('@mstefan99/argumented');
const fm = require('./lib/file-manager');
const formatter = require('./lib/formatter');
const syntaxChecker = require('./lib/syntax-checker');
argumented.description('A bot for formatting and checking C and C++ code');
argumented.add('config', ['-c', '--config'], 'filePath',
'The location of the config file');
const args = argumented.parse();
const config = configurer(args.config);
let jobPromise = Promise.resolve();
config.defaults = {
token: 'your_token_here',
admins: [],
projects: [],
channels: [],
welcome: {
enabled: false
},
tempDir: './projects/temp/',
commandPrefix: '!codebot'
};
config.load().then(config => {
client.login(config.token);
client.on('ready', () => {
console.info('Logged in as CodeBot!');
if (config.welcome.enabled) {
for (const channelID of config.channels) {
client.channels.resolve(channelID)
.send(config.welcome.phrases[Math.floor(Math.random() * config.welcome.phrases.length)]);
}
}
});
client.on('message', message => {
if (message.author.id === client.user.id) {
// Message sent by the bot, ignoring
} else if (message.content.startsWith(config.commandPrefix)) {
handleCommands(message);
} else if (!config.channels.includes(message.channel.id)) {
// Channel not added, ignoring
} else if (!message.content.match(/{(.|\n)*?}/)
&& !message.attachments.array().find(a => a.url.match(/.*\.(?:(?:c(?:pp)?)|(?:zip))?$/))) {
// Message does not contain code or source files, ignoring
} else {
jobPromise = jobPromise
.then(() => processMessage(message));
}
});
function handleCommands(message) {
const words = message.content.match(/\b\w+\b/g);
const commands = [
{
name: 'chadd',
admin: true,
help: 'Adds the current channel to CodeBot',
cb: () => {
if (!config.channels.includes(message.channel.id)) {
config.channels.push(message.channel.id);
config.save();
message.reply('Channel added!');
}
}
},
{
name: 'prset',
admin: true,
help: 'Sets the project to use for current channel',
cb: () => {
const project = config.projects.find(p => p.name === words[2]);
for (const p of config.projects) {
if (p !== project) {
p.channels.splice(p.channels.indexOf(message.channel.id));
}
}
if (project) {
project.channels.push(message.channel.id);
message.reply('Project for this channel is set to ' + project.name + '!');
} else {
message.reply('Project not found, using empty project.');
}
config.save();
}
},
{
name: 'project',
admin: true,
help: 'Shows the project set in the current channel',
cb: () => {
message.reply('The project in the current channel is set to "' +
(config.projects.find(p => p.channels.includes(message.channel.id))?.name ?? 'empty') + '"');
}
},
{
name: 'chdel',
admin: true,
help: 'Removes the current channel from CodeBot',
cb: () => {
config.channels.splice(config.channels.indexOf(message.channel.id));
config.save();
message.reply('Channel removed!');
}
},
{
name: 'channels',
admin: true,
help: 'Lists channels added to CodeBot',
cb: () => {
message.reply('List of CodeBot channels:\n' +
config.channels.join('\n'));
}
},
{
name: 'promote',
admin: true,
args: ['id'],
help: 'Sets user as admin',
cb: () => {
if (!config.admins.includes(words[2])) {
config.admins.push(words[2]);
config.save();
message.reply('User promoted!');
}
}
},
{
name: 'demote',
admin: true,
args: ['id'],
help: 'Removes the user from admins',
cb: () => {
config.admins.splice(config.admins.indexOf(words[2]));
config.save();
message.reply('User demoted!');
}
},
{
name: 'admins',
admin: true,
help: 'Lists all the admins of CodeBot',
cb: () => {
message.reply('List of CodeBot admins:\n' +
config.admins.join('\n'));
}
},
{
name: 'clean',
admin: true,
help: 'Deletes last 100 messages in the chat',
cb: () => {
message.channel.bulkDelete(100, true);
}
},
{
name: 'version',
help: 'Shows bot version',
cb: () => {
configurer('./package.json').load().then(pkg => {
message.reply('CodeBot version: ' + pkg.version);
});
}
},
{
name: 'ahelp',
admin: true,
help: 'Shows an admin help page',
cb: () => {
let commandsDesc = '**CodeBot admin help**\n';
for (const command of commands) {
commandsDesc += config.commandPrefix + ' ' + command.name + ' '
+ (command.args? '[' + command.args.join(', ') + ']' : '') +
' - ' + command.help + '\n';
}
message.reply(commandsDesc);
}
},
{
name: 'help',
help: 'Shows a help page',
cb: () => {
let commandsDesc = '**CodeBot help**\n' +
'Just send me your code and I\'ll format it and check it for any errors!\n' +
'I can read your code in the chat or you can send me your `.c`, `.cpp` or `.zip` files with code!\n';
for (const command of commands.filter(c => !c.admin)) {
commandsDesc += config.commandPrefix + ' ' + command.name + ' '
+ (command.args? '[' + command.args.join(', ') + ']' : '') +
' - ' + command.help + '\n';
}
message.reply(commandsDesc);
}
}
];
const command = commands.find(c => c.name === words[1]);
if (!words[1]) {
message.reply('Please type ' + config.commandPrefix + ' help for usage info.')
} else if (!command) {
message.reply('Command not found');
} else if (command.admin && !config.admins.includes(message.author.id)) {
message.reply('This command requires admin permissions');
} else {
command.cb();
}
}
function processMessage(message) {
return message.channel.send('Building, please wait...')
.then(reply => {
message.channel.startTyping(1);
return processCode(message)
.finally(() => reply.delete());
})
.finally(() => message.delete())
.finally(() => message.channel.stopTyping(true));
}
function processCode(message) {
let promise = Promise.resolve();
const project = config.projects.find(p => p.channels.includes(message.channel.id)) ??
config.projects.find(p => p.name === 'empty');
promise = promise
.then(() => fm.cleanDirectory(project.upload))
.then(() => fm.cleanDirectory(config.tempDir));
const attachments = message.attachments.array();
if (attachments.length) {
const sourceFile = attachments.find(a => a.url.match(/.*\.c(pp)?$/));
const sourceArchive = attachments.find(a => a.url.match(/.*\.zip$/));
if (sourceFile) {
promise = promise
.then(() => fm.downloadFile(sourceFile.url, project.upload))
.then(() => syntaxChecker.checkProject(project.root));
} else if (sourceArchive) {
promise = promise
.then(() => fm.cleanDirectory(project.upload))
.then(() => fm.downloadFile(sourceArchive.url, config.tempDir))
.then(() => fm.unArchive(config.tempDir + sourceArchive.name, project.upload))
.then(() => syntaxChecker.checkProject(project.root));
} else {
// Shouldn't get here after attachment check
promise = promise
.then(() => Promise.reject('File type not supported'));
}
} else {
promise = promise
.then(() => fm.saveToFile(project.upload + 'upload.cpp', message.content))
.then(() => formatter.format(message.content))
.then(formattedCode => message.content = formattedCode)
.then(() => syntaxChecker.checkProject(project.root));
}
return reply(message, project, promise);
}
function reply(message, project, promise) {
const archivePath = config.tempDir + 'CodeBot.zip';
let buildStatus = 'failed';
let output = '';
return promise
.then(warnings => {
if (warnings) {
output = warnings;
fm.saveToFile(project.upload + 'warnings.txt', warnings);
buildStatus = 'warnings';
} else {
buildStatus = 'ok';
}
})
.catch(err => {
if (err) {
output = err.toString();
fm.saveToFile(project.upload + 'errors.txt', err.toString());
}
})
.then(() => message.content? fm.saveToFile(project.upload + 'message.txt',
message.content) : null)
.then(() => fm.archive(project.upload, archivePath))
.then(() => {
let messageText = '<@' + message.author.id + '>, your message:\n';
if (message.content.length < 1000) {
if (message.attachments.size) {
messageText += '"' + message.content + '"\n';
} else {
messageText += '```cpp\n' + message.content + '\n```\n';
}
} else {
messageText += '[too long, see archive]\n';
}
messageText += 'Has been built. Build results:\n';
switch (buildStatus) {
case 'failed':
messageText += ':no_entry: Build failed\n';
break;
case 'warnings':
messageText += ':warning: Build succeeded with warnings\n';
break;
case 'ok':
messageText += ':white_check_mark: Build succeeded, no warnings!\n';
break;
}
if (output.length) {
if (output.length < 500) {
messageText += 'Build output: "' + output + '"\n';
} else {
messageText += 'Build output too long, see archive.\n';
}
}
messageText += 'See attached archive for more details';
return message.channel.send(messageText,
new Discord.MessageAttachment(fs.createReadStream(archivePath)));
});
}
});