-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
110 lines (90 loc) · 2.43 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Imports
var database = require('./database.js');
var fs = require('fs');
// 'Class' imports
var Discord = require('discord.js');
var Response = require('./response.js')
// Constants
const MESSAGE_REGEX = /{(.*?)}/g
// Main
var config = global.config = {};
var addons = global.addons = {};
var bot = global.bot = {};
fs.readFile("./config.json", "utf8", function(err, data) {
if (err) {
console.log(err.message);
return;
}
config = global.config = JSON.parse(data);
bot = global.bot = new Discord.Client({
autoReconnect: true
});
bot.on("message", function(message) {
if (message.author) {
if (bot.user.id === message.author.id) {
return;
}
}
if (!config.allow_pm && !message.channel.server) {
if (config.debug) {
console.log("Received PM, ignoring...");
}
return;
}
if (config.server_limits && message.channel.server) {
var serverId = message.channel.server.id;
var channelId = message.channel.id
if (serverId in config.server_limits) {
var channels = config.server_limits[serverId];
if (channels.indexOf(channelId) == -1) {
if (config.debug) {
console.log("Receieved messge channel #" + channelId + ", ignoring...");
}
return;
}
}
}
var result = "";
var results = [];
while ((result = MESSAGE_REGEX.exec(message.content))) {
results.push(result);
}
if (results.length > config.card_limit_per_message) {
bot.reply(message, "Nyeh? are ya tryin' to kill me? (" + config.card_limit_per_message + " cards per message)");
return;
}
var searches = [];
var response = new Response(function(text) {
bot.reply(message, text);
}, results.length);
results.forEach(function(val) {
var cardName = val[1];
if (searches.indexOf(cardName) != -1) {
return;
}
searches.push(cardName);
console.log("Processing: " + cardName);
database.lookup(cardName, function(card) {
response.handle(card);
});
});
});
if (config.addons && config.addons.length > 0) {
var addonsArray = [];
config.addons.forEach(function(addon) {
var addonObj = require("./addons/" + addon + ".js");
addons[addon] = addonObj;
addonsArray.push(addonObj);
});
addonsArray.forEach(function(addon) {
if (typeof addon.onLoad == "function") {
addon.onLoad();
}
});
}
if (config.login.token) {
bot.loginWithToken(config.login.token);
} else {
bot.login(config.login.email, config.login.password);
}
});