-
Notifications
You must be signed in to change notification settings - Fork 0
/
replbot.js
executable file
·161 lines (148 loc) · 5.66 KB
/
replbot.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
#!/usr/bin/node
'use strict';
/*
* REPL Bot is a Discord bot that acts as a frontend to read-eval-print loop shells.
* Copyright (C) 2017-2018 Jeffrey Thomas Piercy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const FIREJAIL_OPTIONS = '--profile=firejail.profile';
const Discord = require('discord.js');
const { spawn } = require('child_process');
const filesystem = require('fs');
const commandExists = require('command-exists');
function inputCommand(repl, input, message)
{
//console.log(command);
repl.message = message;
repl.shell.stdin.write(`${input}\n`);
console.log(`${message.author.username} ${repl.name}: <${input}>`);
}
function spawnREPL(name, command, prompt)
{
commandExists(command[0], (err, exists) => {
if(exists)
{
repls[name] = [];
const repl = repls[name];
repl.buffer = '';
repl.shell = spawn('firejail', [FIREJAIL_OPTIONS].concat(command));
repl.name = command[0];
const say = (data) => {
const dataStr = data.toString();
if(dataStr === prompt)
return;
if('message' in repl)
{
repl.buffer += dataStr;
if(dataStr.includes('\n'))
{
const parsedBuffer = repl.buffer.replace(RegExp('[\n]' + prompt, 'g'), '');
repl.buffer = '';
repl.message.channel.fetchMessage(repl.message.channel.lastMessageID).then((lastMessage) => {
if(lastMessage.author.id === client.user.id)
lastMessage.edit('```\n' + (lastMessage.content.replace(RegExp('```', 'g'), '') + '\n' + parsedBuffer) + '```');
else
repl.message.channel.send('```' + parsedBuffer + '```');
});
console.log(`${repl.name}: <${parsedBuffer}`.replace(/\n$/, '') + '>');
}
//else
// console.log(dataStr);
}
else
console.log(`${command[0]}: message is undefined`);
};
repl.shell.stdout.on('data', say);
repl.shell.stderr.on('data', say);
repl.shell.on('close', () => {
if('message' in repl)
repl.message.channel.send(`*${command[0]} is restarting*`);
spawnREPL(name, command, prompt);
});
repl.shell.on('error', (err) => {
console.log(`${command[0]}: ${err}`);
});
}
else
console.log(`failed to start ${command}: ${err}`);
});
}
const repls = [];
const aliases = [];
//spawnREPL('sc', ['scala', '-i'], '\nscala> ');
//spawnREPL('sc', ['amm', '--color false'], '@ ');
spawnREPL('js', ['node', '-i'], '> ');
spawnREPL('py', ['pypy3', '-i'], '>>>> ');
spawnREPL('sql', ['sqlite3', '-interactive'], 'sqlite> ');
//spawnREPL('hs', ['ghc', '--interactive'], 'Prelude> ');
spawnREPL('go', ['gore'], 'gore> ');
aliases['!sc'] = { repl: 'sc', macro: '' };
aliases['!js'] = { repl: 'js', macro: '' };
aliases['!py'] = { repl: 'py', macro: '' };
aliases['!sql'] = { repl: 'sql', macro: '' };
aliases['!hs'] = { repl: 'hs', macro: '' };
aliases['!go'] = { repl: 'go', macro: '' };
const client = new Discord.Client();
client.on('ready', () => console.log('I am ready!'));
client.on('message', (message) => {
const params = message.content.split(' ');
if(params.length === 0)
return;
if(params[0] === '!repl')
{
if(params.length === 1)
message.reply('Hi! I\'m REPL Bot.');
else if(params[1] === 'alias')
{
if(params.length > 3)
{
if(params[3] in repls)
{
aliases[`!${params[2]}`] = {
repl: params[3],
macro: params.slice(4).join(' ') + ' '
};
}
else
message.reply(`invalid command '${params[3]}'`);
}
else
message.reply('invalid command');
}
else if(params[1] === 'restart')
{
if(params.length === 3 && params[2] in repls)
{
repls[params[2]].message = message;
repls[params[2]].shell.kill();
}
else
message.reply('invalid command');
}
else if(params[1] in repls)
{
const repl = repls[params[1]];
const command = params.slice(2).join(' ');
inputCommand(repl, command, message);
}
}
else if(params[0] in aliases)
{
const alias = aliases[params[0]];
inputCommand(repls[alias.repl], `${alias.macro}${params.slice(1).join(' ')}`, message);
console.log(`[${alias.macro}${params.slice(1).join(' ')}]`);
}
});
client.login(filesystem.readFileSync('token', 'utf8'));