-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathethanbot.js
222 lines (193 loc) · 7.07 KB
/
ethanbot.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Created by Peter on 07/06/2017.
*/
const config = require('config'),
Discord = require('discord.io'),
dateFormat = require('dateformat'),
colors = require('colors'),
caller_id = require('caller-id'),
path = require('path'),
async = require('async');
var bot = new Discord.Client({
autorun: false,
token: config.get("Discord.token")
});
function initBot(cb){
bot.wasConnected = false;
bot.errorCount = 0;
bot.commandCount = 0;
bot.lastCrash = new Date();
bot.PARSER = /[!<>@#&]/g;
bot.numberWithCommas = function(x){
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
bot.log = function log(message, caller, logged){
if(!caller)
caller = caller_id.getData();
var file = ["Nowhere"];
if(caller.filePath)
file = caller.filePath.split(path.sep);
var origin = `[${file[file.length-1]}${caller.functionName ? path.sep+caller.functionName : ""}] `.bold;
var output = origin+message;
console.log(`[${dateFormat(new Date(), "dd/mm/yy hh:MM")}]`+output);
if(bot.database && !logged){
bot.database.log(message,file[file.length-1]+(caller.functionName ? path.sep+caller.functionName : ""))
.catch(function(err){
console.error("ERROR LOGGING MESSAGE!!! "+err);
});
}
};
bot.error = function error(message){
var caller = caller_id.getData();
var file = ["Nowhere"];
if(caller.filePath)
file = caller.filePath.split(path.sep);
if(bot.database)
bot.database.log(message,file[file.length-1]+(caller.functionName ? path.sep+caller.functionName : ""), "ERROR")
.catch(function(err){
console.error("ERROR LOGGING ERROR!!! "+err);
});
bot.log(message.red, caller, true);
bot.errorCount++;
};
bot.warn = function warn(message){
var caller = caller_id.getData();
var file = ["Nowhere"];
if(caller.filePath)
file = caller.filePath.split(path.sep);
if(bot.database)
bot.database.log(message,file[file.length-1]+(caller.functionName ? path.sep+caller.functionName : ""), "WARN")
.catch(function(err){
console.error("ERROR LOGGING WARNING!!! "+err);
});
bot.log(message.yellow, caller, true);
};
bot.log("EthanBot Loading...");
bot.messageQueue = [];
bot.messageCount = 0;
bot.totalMessageTime = 0;
bot.isHandlingMessageQueue = false;
bot.sendMessageForReal = bot.sendMessage;
bot.handleMessageQueue = function handleMessageQueue(){
var messageParams = bot.messageQueue.pop();
if(messageParams){
bot.isHandlingMessageQueue = true;
bot.sendMessageForReal(messageParams.args, messageParams.cb);
bot.messageCount++;
bot.totalMessageTime = new Date() - messageParams.sentAt;
bot.log("-> "+messageParams.args.message);
setTimeout(bot.handleMessageQueue, parseInt(config.get("Discord.messageDelay")));
}else{
bot.isHandlingMessageQueue = false;
}
};
bot.sendMessage = function sendMessage(args, cb){
bot.messageQueue.push({
args: args,
cb: cb,
sentAt: new Date()
});
if(!bot.isHandlingMessageQueue)
bot.handleMessageQueue();
};
bot.loadBefore = config.get("Modules.LoadBefore");
bot.loadAfter = config.get("Modules.LoadAfter");
initModules(bot.loadBefore, function(){
startBot(function(){
initModules(bot.loadAfter, cb);
});
});
}
function initModules(modules, cb){
bot.log(`Loading ${modules.length} modules...`);
async.eachSeries(modules, function loadModule(module, callback){
var loadedModule = require('./modules/'+module)(bot);
bot.log(`Loading ${loadedModule.name}...`);
loadedModule.init(callback);
}, cb);
}
function startBot(cb){
bot.on("ready", function(){
bot.log("Bot connected");
if(cb && !bot.wasConnected){
bot.wasConnected = true;
cb();
}
});
bot.on("disconnect", function(err){
bot.warn("Disconnected... Reconnecting in 1 second "+err);
setTimeout(bot.connect, 1000);
});
bot.on("guildMemberAdd", function(member, event){
var username = bot.users[member.id].username+"#"+bot.users[member.id].discriminator;
var server = event.d.guild_id;
bot.database.getServer(server)
.then(function(result){
var serverInfo = result[0];
if(serverInfo.useServerCurrency){
return bot.database.createServerUser(server, member.id, username);
}
})
.then(function(result){
if(result)
bot.log(`Added server user ${member.id} ${username} ${server}`);
})
.catch(function(err){
if(err.message.indexOf("Duplicate") > -1){
bot.log(`User ${member.id} ${username} already exists on ${server}`);
}else
bot.error(`Error adding server user: ${member.id} ${username} ${server}: ${err}`)
});
bot.database.addUser(member.id, username)
.then(function(){
bot.log(`Added user ${member.id}: ${username}`);
})
.catch(function(err){
if(err.message.indexOf("Duplicate") > -1){
bot.log(`User ${member.id} ${username} already exists.`);
}else
bot.error(`Error adding user ${member.id}/${username}: ${err}`);
});
});
bot.on("guildCreate", function(server){
bot.log("Joined Server "+server.name);
bot.database.addAllUsers(server.id);
});
bot.log("Connecting to Discord...");
bot.connect();
}
initBot(function(){
bot.log("Ready");
});
process.on("uncaughtException", function(err){
console.error(err);
bot.database.log(err.stack, err.message, "CRASH")
.then(function(){
bot.knex.client.pool.drain(bot.knex.client.pool.destroyAllNow);
bot.disconnect();
setTimeout(function(){
process.exit(1);
}, 1000);
})
.catch(function(){
process.exit(1);
});
});
process.on('unhandledRejection', function(reason){
console.error("Unhandled rejection: "+reason);
bot.database.log("Unhandled Rejection", reason || "No Reason Given", "ERROR")
.catch(function(err){
console.error("Error logging unhandled rejection "+err);
});
});
process.on('beforeExit', function(){
bot.log("Exiting...");
bot.knex.client.pool.drain(bot.knex.client.pool.destroyAllNow);
bot.disconnect();
setTimeout(function(){
process.exit(1);
}, 1000);
});
process.on('warning', function(warning){
bot.warn("Node warning: "+warning.message);
});