-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (77 loc) · 2.88 KB
/
index.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
const { Assyst } = require('./lib/Assyst.js');
const config = require('./config.json');
const client = new Assyst(config);
// eslint-disable-next-line no-shadow
const db = require('quick.db');
// eslint-disable-next-line new-cap
const timeout = new db.table('timeout');
console.log('Starting Assyst');
console.log('Loading commands');
client.loadCommands();
async function checkRepl(msg) {
if (!msg.content.startsWith('`') || !msg.content.endsWith('`') ) {
return;
} if (msg.content.startsWith('```') ) {
return;
} if (!client.repl.map(i => i.user).includes(msg.author.id) || !client.repl.find(i => i.channel === msg.channel.id) ) {
return;
}
const currentRepl = client.repl.find(j => j.user === msg.author.id && j.channel === msg.channel.id);
const contentToSend = msg.content.substr(1, msg.content.length - 2);
if (contentToSend === 'exit') {
client.repl.splice(client.repl.indexOf(currentRepl, 1) );
msg.channel.createMessage(`${client.emotes.success} The REPL session was ended.`);
return;
}
const response = await client.utils.requestAPI(require('./config.json').chiasmIP, 'POST', { 'content-type': 'application/json' }, { code: contentToSend, lang: currentRepl.lang, imports: [] } );
msg.channel.createMessage(`\`\`\`\n${response.text}\n\`\`\``);
}
function findCommand(command) {
const foundCommand = client.commands[command] || client.cmdAliases[command];
if (!foundCommand) {
return null;
}
return foundCommand;
}
function checkPermissions(msg) {
let authorPermLevel;
if (client.staff.owners.includes(msg.author.id) ) {
authorPermLevel = 2;
} else if (client.staff.admins.includes(msg.author.id) ) {
authorPermLevel = 1;
} else {
authorPermLevel = 0;
}
return authorPermLevel;
}
client.bot.on('messageCreate', async(msg) => {
checkRepl(msg);
if (!msg.content.startsWith(client.prefix) || msg.author.bot) {
return;
}
const args = msg.content.slice(config.client.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const foundCommand = findCommand(command);
if (!foundCommand) {
return;
}
const authorPermLevel = checkPermissions(msg);
if (foundCommand.permissions > authorPermLevel) {
return;
}
if (!timeout.get(msg.author.id) || (foundCommand.timeout + timeout.get(msg.author.id) ) < Date.now() ) {
timeout.set(msg.author.id, Date.now() );
} else {
const message = await foundCommand.sendMsg(msg.channel, `<@${msg.author.id}> You're using commands too quickly.`, 'error');
setTimeout( () => {
message.delete();
// eslint-disable-next-line no-magic-numbers
}, 3000);
return;
}
foundCommand.execute( { msg, args } );
} );
client.bot.on('ready', () => {
console.log('Ready');
} );
client.bot.connect();