-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (75 loc) · 2.55 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
const Discord = require('discord.js');
const bot = new Discord.Client();
const { token } = require('./token');
const { spawn } = require('child_process');
let prefixes;
bot.on('ready', () => {
prefixes = [`<@${bot.user.id}> `, '@giac '];
bot.user.setGame('@CAS help')
console.log('ready !');
});
bot.on('message', msg => {
// don't respond to self
if (msg.author.bot) return;
// check if prefix is correct or that we are in a DM channel
if (
prefixes.every(p => msg.content.indexOf(p) !== 0) &&
!(msg.channel instanceof Discord.DMChannel)
)
return;
// Run !
msg.channel.startTyping();
const commands = prefixes.reduce((s, p) => s.replace(p, ''), msg.content);
if(commands == 'help') sendHelp(msg);
else giacCommands(msg, commands);
});
async function giacCommands(msg, commands) {
// --rm for cleanup (no need to keep fs after the image is run)
// --network none because there is no need for network
// -i because we send the data via stdin
const giacrenderer = spawn('docker', [
'run',
'--rm',
'--network',
'none',
'-i',
'giac-renderer'
]);
let output = '';
giacrenderer.stderr.on('data', data => {
process.stdout.write(`\x1b[32m${data}\x1b[39m`);
});
giacrenderer.stdout.on('data', data => {
output += data;
// process.stdout.write(data);
if (output.indexOf('\n') >= 0) {
const pictures = output.split('\n');
output = pictures[pictures.length - 1];
pictures.slice(0, -1).forEach(el => {
sendPicture(msg, Buffer.from(el, 'base64'));
});
}
});
giacrenderer.on('close', code => {
console.log(`giac-renderer exited with code ${code}`);
});
giacrenderer.stdin.write(commands, 'utf8');
giacrenderer.stdin.end();
giacrenderer.stdout.setEncoding('utf8');
// sendLatex(msg, latex);
}
async function sendPicture(msg, buffer) {
if(buffer.length <= 20)
await msg.channel.send('Error while processing input !');
else
await msg.channel.send('', new Discord.Attachment(buffer, 'giac.png'));
msg.channel.stopTyping();
}
function sendHelp(msg) {
msg.author.send("Please see http://www-fourier.ujf-grenoble.fr/~parisse/giac/doc/en/cascmd_en/ for full reference. Version française: http://www-fourier.ujf-grenoble.fr/~parisse/giac/doc/fr/cascmd_fr/");
}
bot.login(token);
process.on('uncaughtException', e => {
console.log('Uncaught Exception...');
console.trace(e);
});