-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
305 lines (261 loc) · 10.9 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
const Discord = require('discord.js');
var Command = require('./src/commandTemplate');
var commands = require('./src/commands');
var utils = require('./src/utils/utils');
var dbUtils = require('./src/db/dbUtils');
var dbUsers = require('./src/db/dbUsers');
var dbGuild = require('./src/db/dbGuild');
var discordUtils = require('./src/utils/discordUtils');
var moderationUtils = require('./src/utils/moderationUtils');
var helpers = require('./src/helpers.js');
const client = new Discord.Client({
fetchAllMembers: true,
disableEveryone: true
});
const token = require('./config.json').token;
const suf = require('./config.json').suffix;
const dev = require('./config.json').dev;
var logger = require('./src/utils/logger');
//Automatic membership processing
var checkMembershipStatus = require('./src/memberProcessor.js');
var NEWUSERTHRESHOLD = (7 * 24 * 3600 * 1000);
//Database module
const Connection = require('./src/db/dbConnection');
var time = Date.now();
/* On ready event */
client.on('ready', () => {
var interval = Date.now() - time;
logger.info(`Bot connected (${interval} ms)`);
//Load all the timers
helpers.loadTimers(client);
helpers.loadNewMembers(client, NEWUSERTHRESHOLD);
helpers.startBirthdayLoop(client);
});
client.on('disconnect', () => {
logger.info(`[${utils.unixToTime(Date.now())}] Bot disconnected!`);
})
/* Message event listener */
client.on('message', msg => {
var splitted = msg.content.split(" ");
//Log the message in the DB
if (!dev) {
dbUtils.storeMessage(msg);
}
if (msg.guild != null) {
checkMembershipStatus(client, msg.member);
}
//Prevent the bot from triggering its own commands
if (msg.author.id == client.user.id) {
return;
}
if (msg.guild != null) {
helpers.checkInvLink(msg);
helpers.processSuggestionChannel(msg);
}
//Remove suffix
var cmdName = splitted[0];
var suffix = msg.content.substr(cmdName.length + 1);
if (suffix != "") {
suffix = suffix.split(" ");
}
//Check if it's a command
if (cmdName.endsWith(suf)) {
cmdName = cmdName.substring(0, splitted[0].length - 1);
cmdName = cmdName.toLowerCase();
if (commands.hasOwnProperty(cmdName)) {
var location = msg.guild ? msg.guild.name : "DM";
if (suffix) {
logger.info(`[${utils.unixToTime(Date.now())}][${location}][${msg.author.username}] >${cmdName}. Parameters: ${suffix.join(" ")}`);
} else {
logger.info(`[${utils.unixToTime(Date.now())}][${location}][${msg.author.username}] >${cmdName}`);
}
commands[cmdName].run(client, msg, suffix);
}
} else if (msg.mentions.users.has(client.user.id)) {
/* TODO: Bot AI with Cleverbot? */
}
});
/* Member join and leave processing */
client.on('guildMemberAdd', (member) => {
//Console persist
logger.info(`[${utils.unixToTime(Date.now())}] ${member.user.username}#${member.user.discriminator} (${member.id}) joined ${member.guild.name}`);
var guild = member.guild;
if (!dev) {
dbUsers.updateUserJoined(guild.id, member.user.id, Date.now(), () => {});
}
dbGuild.fetchRoleID("warned", guild.id, warnedRole => {
dbGuild.fetchRoleID("muted", guild.id, mutedRole => {
retrieveMembers(warnedRole, mutedRole);
});
});
dbGuild.fetchGuild(guild.id, function(err, guildData) {
if (err) logger.error(err);
//Handling of newly created accounts
if (guildData != null && guildData.hasOwnProperty('isolatenewaccounts') && guildData.isolatenewaccounts) {
if (member.user.createdTimestamp > Date.now() - NEWUSERTHRESHOLD) {
//Check if the account is new, if it is we check that the user is not already in the database and add it
dbGuild.fetchNewAccounts(member.guild.id).then((arr) => {
for (var userData of arr) {
if (userData.user_id == member.user.id && userData.guild_id == member.guild.id) {
return;
}
}
dbGuild.storeNewAccount(guild.id, member.user.id).then(() => {
var role = guild.roles.find("name", "New Account");
if (role) {
logger.info(`[${utils.unixToTime(Date.now())}] and added to newly created accounts`);
member.send(`You have been locked in ${member.guild.name} due to the account being new, to be unlocked contact one of the moderators/adminstrators`).catch();
//TODO Change this
if (guild.id == "132490115137142784") {
var channel = guild.channels.get("184984832219152387");
if (channel) channel.send(`User ${member} joined the server and was added to New Accounts.`).catch();
}
setTimeout(() => {
member.addRole(role).catch((e) => {
logger.warn(discordUtils.missingPerms("Add Role", guild, member));
});
}, 1000);
}
}).catch((e) => {
logger.error(error);
});
});
} else {
//If the user is not a newly created account
helpers.logWelcomeOrLeft(guild, member, true);
}
} else {
//If the guild has not set up the limit of new users, just process the welcome
helpers.logWelcomeOrLeft(guild, member, true);
}
});
function retrieveMembers(warnedRole, mutedRole) {
dbUsers.fetchMember(guild.id, member.user.id, (err, memberData) => {
if (err) logger.error(err);
else {
if (memberData && memberData.last_left) {
if (memberData.roles && memberData.roles.length > 0) {
member.addRoles(memberData.roles).then((memb) => {
if ((warnedRole && (memberData.roles.indexOf(warnedRole) > -1)) || (mutedRole && (memberData.roles.indexOf(mutedRole) > -1))) {
member.user.send(`It looks like you have tried to circumvent a warning/mute in ${guild.name}. If you continue to do so, a ban will be issued.`);
}
}).catch((er) => {
logger.error(er);
});
}
}
}
});
}
});
client.on('guildMemberRemove', (member) => {
//Console persist
logger.info(`${member.user.username}#${member.user.discriminator} (${member.user.id}) left ${member.guild.name}`);
var guild = member.guild;
if (!dev) {
var roleInstances = member.roles.array();
var userRoles = [];
roleInstances.forEach(function(element) {
if (element.name != "@everyone" && element.name != "New Account") {
userRoles.push(element.id);
}
});
if (userRoles.length > 0) {
dbUsers.updateUserRoles(guild.id, member.user.id, userRoles, false, () => {});
} else {
dbUsers.updateUserRoles(guild.id, member.user.id, [], true, () => {});
}
dbUsers.updateUserLeft(guild.id, member.user.id, Date.now(), () => {});
}
dbGuild.fetchGuild(guild.id, function(err, guildData) {
if (err) logger.error(err);
//Handling of newly created accounts
if (guildData != null && guildData.hasOwnProperty('isolatenewaccounts') && guildData.isolatenewaccounts) {
dbGuild.fetchNewAccounts(guild.id).then((arr) => {
for (var userData of arr) {
if (userData.user_id == member.user.id && userData.guild_id == guild.id) {
dbGuild.deleteNewAccount(guild.id, member.user.id).catch((e) => {
logger.error(e);
});
return;
}
}
//If no user was found in the db
helpers.logWelcomeOrLeft(guild, member, false);
});
} else {
helpers.logWelcomeOrLeft(guild, member, false);
}
});
});
/* Username and nickname update handler */
client.on('userUpdate', (oldUser, newUser) => {
if (!dev && oldUser.username != newUser.username && newUser.username != null) {
//dbUtils.storeNameChange(oldUser.id, oldUser.username, newUser.username, false);
dbUsers.updateUsername(newUser.id, newUser.username, () => {
});
}
});
client.on('guildMemberUpdate', (oldMember, newMember) => {
if (!dev && oldMember.nickname != newMember.nickname) {
//dbUtils.storeNameChange(newMember.user.id, oldMember.nickname, newMember.nickname, true, oldMember.guild.id);
dbUsers.updateNickname(newMember.guild.id, newMember.user.id, newMember.nickname, () => {
});
}
});
/* Database persist of deleted/edited messages */
client.on('messageDelete', (message) => {
if (!dev) {
dbUtils.tagMessageAs(message.id, false);
}
});
client.on('messageUpdate', (oldMessage, newMessage) => {
if (!dev) {
dbUtils.tagMessageAs(oldMessage.id, true, newMessage.content);
if (newMessage.guild != null) {
helpers.checkInvLink(newMessage);
}
}
});
/* Handling of server bans */
client.on('guildBanAdd', (guild, user) => {
//Console persist
logger.info(`${user.username}#${user.discriminator} (${user.id}) banned from ${guild.name}`);
//Timeout to dectect the softban message
setTimeout(() => {
discordUtils.findLogsChannel(guild, (logChannel) => {
if (logChannel) {
var foundSoftBan = false;
logChannel.fetchMessages({
limit: 5
})
.then(messages => {
var messageFound = messages.find(m => {
var embed = m.embeds[0];
if (embed) {
if (embed.title != "SOFTBAN") return false;
for (var field of embed.fields) {
if (field.name == "User" && field.value.includes(user.id)) {
return true;
}
}
}
return false;
});
if (messageFound == null) {
moderationUtils.logPlaceholder(user, logChannel);
}
});
}
});
}, 2000);
});
/* Starts the bot */
function startBot() {
//Try to connect to DB and to log the client
Connection((err, db) => {
if (err) logger.error(err);
client.login(token);
});
}
startBot();