-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
81 lines (61 loc) · 2.03 KB
/
bot.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
//
// Part of XJ-1812
// License: MITA
//
var Discord = require('discord.js');
var HelpMessage = require('./help-message.js');
module.exports = Bot;
function Bot(commandModules, config) {
var self = this;
self.run = run;
var auth;
var commands;
var rawCommands;
init();
function init() {
auth = config.auth;
config.auth = undefined;
HelpMessage.init(config, commandModules);
commands = new Map();
rawCommands = new Map();
commandModules.forEach(function (commandModule) {
if (commandModule.init) {
commandModule.init(config);
}
commandModule.commands.forEach(function (command) {
if (command.command) {
commands.set(command.command, command);
}
if (command.rawCommand) {
rawCommands.set(command.rawCommand, command);
}
});
});
}
function run() {
var bot = new Discord.Client();
bot.on('ready', function () {
console.log('Connected');
console.log('Logged in as: ');
console.log(bot.user.username + ' - (' + bot.user.tag + ')');
bot.user.setActivity(config.commandSequence + 'help');
});
bot.on('message', function (message) {
var firstWord = message.content.split(' ', 1)[0];
if (rawCommands.has(firstWord)) {
rawCommands.get(firstWord).handler(message);
}
else if (firstWord.substring(0, config.commandSequence.length) == config.commandSequence) {
var cmd = firstWord.substring(config.commandSequence.length);
if (cmd == 'help') {
HelpMessage.helpMessage(message);
}
else if (commands.has(cmd)) {
commands.get(cmd).handler(message);
}
}
});
bot.on('error', console.log);
bot.login(auth.token);
}
}