-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreasebot.js
67 lines (55 loc) · 1.75 KB
/
greasebot.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
'use strict';
let Discord = require('discord.js'),
commands = require('./commands/all');
let bot = new Discord.Client(),
config = process.env;
try {
config = Object.assign(config, require('./config.json'));
} catch (e) {
console.log('config.json missing!');
}
bot.on('ready', () => {
console.log('GreaseBot online! Connected to servers:');
bot.servers.forEach(server => {
console.log(' ✓', server.name);
})
});
bot.on('disconnected', () => {
console.log('GreaseBot disconnected!');
process.exit(1);
});
bot.on('message', msg => {
// ignore our own messages
if (msg.author.bot && msg.author.id === bot.user.id) {
return;
}
let isBang = msg.content[0] === '!',
isMention = msg.content.indexOf(bot.user.mention()) === 0;
// you've got my attention...:
// !cmd [args]
// @Greasebot [!]cmd [args]
if (isBang || isMention) {
let msgParts = msg.content.split(' '),
cmd = isMention ?
// extracts cmd from '@Greasebot cmd' and '@Greasebot !cmd'
(msgParts[1] && msgParts[1][0] === '!' ? msgParts[1].substring(1) : msgParts[1])
// extracts cmd from '!cmd'
: msgParts[0].substring(1),
args = msgParts.slice(isMention ? 2 : 1);
// bot was mentioned without any additional text
if (!cmd) {
bot.sendMessage(msg.channel, 'What?!');
return;
}
// process the command (or do nothing for garbage commands)
if (commands[cmd]) {
commands[cmd].process(bot, msg, args);
}
}
});
// gre-he-easy!
bot.loginWithToken(config.BOT_TOKEN, err => {
if (err) {
console.log('GreaseBot could not login!', err);
}
});