aboutsummaryrefslogtreecommitdiff
path: root/bot/src/commands/eval.js
blob: 598c7fde93c9b7604b962b8df3a436271da9d68d (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
53
54
55
56
57
58
import { SlashCommandBuilder } from 'discord.js';
import { inspect } from 'util';
import { userWhitelist } from '../storage/consts.js';


export default {
    data: new SlashCommandBuilder()
        .setName('eval')
        .setDescription('Evaluates code'),
    async execute(interaction) {
        if (!userWhitelist.includes(interaction.user.id)) return await interaction.reply('Nope! You don\'t have permission to use this command.');
        await interaction.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 === interaction.user.id;

        const collector = interaction.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 = await 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 interaction.followUp('Exiting evaluation mode.');
        });
    }
};