summaryrefslogtreecommitdiff
path: root/src/cmd/sudo.js
blob: cc590bf8d3e09a386ebee20dc3501a05d3097f3a (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
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
const { inspect } = require('util');
const vm = require('vm');

module.exports.loadModule = function loadModule(bot) {
    bot.handler.endpoint('^guilds$', [], (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        bot.createMessage(message.channel.id, `\`${bot.guilds.size}\``).catch(Logger.error);
    });
    bot.handler.endpoint('^is-sudoer$', [], (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        bot.createMessage(message.channel.id, 'true').catch(Logger.error);
    });
    bot.handler.endpoint('^sudoers$', [], (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        bot.createMessage(message.channel.id, `\`\`\`\n[ ${bot.config.sudoers.join(',\n  ')} ]\n\`\`\``).catch(Logger.error);
    });
    bot.handler.endpoint('^addsudo (.+)$', [], (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        bot.config.sudoers.push(match[1]);
        bot.createMessage(message.channel.id, `${match[1]} temporarily added to the sudoers list`).catch(Logger.error);
    });
    bot.handler.endpoint('^resetsudo$', [], (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        bot.config.sudoers = Array.from(bot._ds);
        bot.createMessage(message.channel.id, `Sudoers list reset`).catch(Logger.error);
    });
    bot.handler.endpoint('^removesudo (.+)$', [], (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        let i = bot.config.sudoers.indexOf(match[1]);
        if (i >= 0) {
            bot.config.sudoers.splice(i, 1);
            bot.createMessage(message.channel.id, `${match[1]} removed from sudoers list`).catch(Logger.error);
        }
        else {
            bot.createMessage(message.channel.id, `${match[1]} was not sudoer`).catch(Logger.error);
        }
    });
    bot.handler.endpoint('^e ?```(?:.*\n)?(.*)\n?```$', [], (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        try {
            let evaled = vm.runInNewContext(match[1], {
                ctx: {
                    bot: bot,
                    message: message,
                    match: match,
                },
                require: require,
            });

            if (typeof evaled !== 'string') {
                evaled = inspect(evaled);
            }
            evaled = evaled.replace(bot.config.token, 'ツ');
            bot.createMessage(message.channel.id, `\`\`\`${evaled}\`\`\``).catch(Logger.error);
        }
        catch (e) {
            bot.createMessage(message.channel.id, `Error:\n\`\`\`\n${e}\`\`\``).catch(Logger.error);
        }
    });
    bot.handler.endpoint('^aguilds$', [], async (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        let aguilds = await bot.db.Aguild.findAll({ attributes: ['guildId'] });
        let buff = '```\nAuthorized guilds:\n\n';
        aguilds.forEach(g => {
            buff += `- ${g.guildId}\n`;
        });
        buff += '```';
        bot.createMessage(message.channel.id, buff).catch(Logger.error);
    });
    bot.handler.endpoint('^ausers$', [], async (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        let ausers = await bot.db.Auser.findAll({ attributes: ['userId'] });
        let buff = '```\nAuthorized users:\n\n';
        ausers.forEach(u => {
            buff += `- ${u.userId}\n`;
        });
        buff += '```';
        bot.createMessage(message.channel.id, buff).catch(Logger.error);
    });
    bot.handler.endpoint('^aguild ([0-9]+)$', [], async (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        if (!match[1]) return;
        try {
            await bot.db.Aguild.create({
                guildId: match[1],
            });
            bot.createMessage(message.channel.id, `Guild \`${match[1]}\` successfully authorized!`).catch(Logger.error);
        }
        catch (e) {
            bot.createMessage(message.channel.id, 'Guild was already authorized.').catch(Logger.error);
        }
    });
    bot.handler.endpoint('^auser ([0-9]+)$', [], async (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        if (!match[1]) return;
        try {
            await bot.db.Auser.create({
                userId: match[1],
            });
            bot.createMessage(message.channel.id, `User \`${match[1]}\` successfully authorized!`).catch(Logger.error);
        }
        catch (e) {
            bot.createMessage(message.channel.id, 'User was already authorized.').catch(Logger.error);
        }
    });
    bot.handler.endpoint('^rguild ([0-9]+)$', [], async (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        if (!match[1]) return;
        await bot.db.Aguild.destroy({
            where: {
                guildId: match[1],
            }
        });
        bot.createMessage(message.channel.id, `Guild \`${match[1]}\` successfully removed!`).catch(Logger.error);
    });
    bot.handler.endpoint('^ruser ([0-9]+)$', [], async (match, message) => {
        if (!bot.config.sudoers) return;
        if (bot.config.sudoers.indexOf(message.author.id) <= -1) return;
        if (!match[1]) return;
        await bot.db.Auser.destroy({
            where: {
                userId: match[1],
            }
        });
        bot.createMessage(message.channel.id, `User \`${match[1]}\` successfully removed!`).catch(Logger.error);
    });
};