aboutsummaryrefslogtreecommitdiff
path: root/bot/src/plugins/eval.js
blob: c667dca83087a8b067ae38a80b6dbf01a54ec478 (plain) (blame)
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
import { inspect } from 'util';

export async function Evaluation(msg) {
    if (!['242775871059001344'].includes(msg.author.id)) return await msg.reply('Nope! You need the person who created this bot to use this command.');
    await msg.reply('You have entered evaluation mode. Enter the code for AleeBot to evaluate.\nType in `exit` to exit evaluation mode.');

    let evaled;
    let remove;

    const filter = (i) => i.author.id === msg.author.id;

    const collector = msg.channel.createMessageCollector({
        filter,
        time: 1000 * 600
    });

    collector.on('collect', async (msg) => {
        if (msg.content.toLowerCase() === 'exit') {
            return collector.stop();
        }

        try {
            remove = (text) => {
                if (typeof(text) === 'string') {
                    return text.replace(/`/g, '`' + String.fromCharCode(8203)).replace(/@/g, '@' + String.fromCharCode(8203));
                } else {
                    return text;
                }
            };

            evaled = eval(msg.content);

            if (typeof evaled !== 'string') {
                evaled = inspect(evaled);
            }

        } catch (err) {
            return await msg.reply(`**Error:**\n\`\`\`\n${err.stack}\n\`\`\``);
        }

        try {
            return await msg.reply(`**Output:**\n\`\`\`js\n${remove(evaled)}\n\`\`\``);
        } catch (err) {
            return await msg.reply(`**Error:**\n\`\`\`\n${err.stack}\n\`\`\``);
        }

    });

    collector.on('end', async () => {
        return await msg.reply('Exiting evaluation mode.');
    });
}