summaryrefslogtreecommitdiff
path: root/src/Events.js
blob: f848495c96160d6605485c0b608c531e9a295671 (plain)
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
module.exports = function Events(bot) {
    let r;

    bot.on('ready', async () => {
        console.log(`Successfully connected as user ${bot.user.username}#${bot.user.discriminator}`);
        r = new RegExp(`^(?:<@!?${bot.user.id}> +|-)\\b`);

        bot.editStatus('online', {
            name: `LaTeX.`,
            type: 3,
        });
    });

    bot.on('error', (err, id) => {
        console.error(`Error encountered on shard ${id}`);
        console.error(err);
    });

    bot.on('messageCreate', async (msg) => {
        if (!r) {
            console.error('Some real shit hapened : message matching regex hasn\'t been defined for some reason.');
            process.exit();
        }

        let content = msg.content.replace(r, '');

        if (content === msg.content) return;
        if (msg.author.bot) return;
        if (msg.author === bot.user) return;
        if (msg.channel.type !== 0) return;

        console.log(`Command '${msg.content}' issued`);

        let trimmedContent = content.trim();
        let result = bot.handler.apply(trimmedContent, msg);
        if (Array.isArray(result)) {
            bot.createMessage(msg.channel.id, `Missing permissions : ${result.join(', ')}`).catch(console.error);
        }
    });
};