-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
267 lines (211 loc) · 7.81 KB
/
index.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
const botsettings = require('./botsettings.json');
const config = require('./utility/config');
const Discord = require('discord.js');
const fs = require('fs');
const db = require('./utility/databaseconnection');
const logger = require('./utility/logger');
const logColor = require('./utility/logcolors');
const getDate = require('./utility/date');
// const bcrypt = require('bcryptjs');
// const password = botsettings.password;
// const salt = botsettings.salt;
// const hash = bcrypt.hashSync(password, salt);
const functions = require('./utility/functions');
const contestFunctions = require('./utility/contest');
const cheese = require('./apis/cheese');
const prefix = botsettings.prefix;
// get id's
const serverId = botsettings.serverId;
const contestChat = botsettings.contestChat;
const client = new Discord.Client({disableMentions: 'everyone'});
client.commands = new Discord.Collection();
/****************
* Web
****************/
const web = require('./utility/web');
/****************
* Load Events
****************/
fs.readdir("./events/", (err, files) => {
if (err) {
logger.error(err, {logType: 'error', time: Date.now()});
console.error(err);
return;
}
files.forEach(file => {
const event = require(`./events/${file}`);
let eventName = file.split(".")[0];
client.on(eventName, event.bind(null, client));
});
});
/****************
* Load Commands
****************/
fs.readdir('./cmds/', (err, files) => {
if (err) {
logger.error(err, {logType: 'error', time: Date.now()});
console.error(err);
return;
}
let jsfiles = files.filter(f => f.split('.').pop() === 'js');
if (jsfiles.length <= 0) {
logger.info('No commands to load!', {logType: 'warning', time: Date.now()});
return;
}
logger.info(`Loading ${jsfiles.length} commands!`, {logType: 'log', time: Date.now()});
jsfiles.forEach((f, i) => {
let props = require(`./cmds/${f}`);
logger.info(`${i + 1}: ${f} loaded!`, {logType: 'log', time: Date.now()});
client.commands.set(props.help.name, props);
// get aliases for commands
if (props.help.alias) client.commands.set(props.help.alias, props);
});
});
let server;
client.on('ready', async () => {
server = client.guilds.cache.get(serverId);
// fetch old messages in contest chat
const contestChannel = client.channels.cache.get(contestChat);
contestChannel.messages.fetch().then(msg => console.log('fetched old messages')).catch(err => {
logger.error(err, {logType: 'error', time: Date.now()});
throw err;
});
functions.logServerInfo(server);
functions.logChannels(server);
functions.logMemberCount(server);
functions.logMembers(server);
// log daily userCount at 23:59
const dailyLog = () => {
let now = new Date();
let millisTill23 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 0) - now;
if (millisTill23 < 0) millisTill23 += 86400000;
setTimeout(() => {
logger.info(`it\'s 23:59`);
functions.logMemberCount(server);
// functions.logMembers(server);
dailyLog();
}, millisTill23);
}
dailyLog();
const dailyCheese = () => {
let now = new Date();
let millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 22, 00, 00, 0) - now;
if (millisTill10 < 0) millisTill10 += 86400000;
setTimeout(() => {
logger.info(`it\'s 10:00`);
cheese.postCheeseOfTheDay(client);
}, millisTill10);
}
dailyCheese();
/****************
* Check for contest Deadlines
****************/
const checkContests = () => {
let now = new Date();
let hour = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), 0, 01, 0) - now;
// let hour = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), 01, 0) - now; // minutes (for testing)
if (hour < 0) hour += 3600000; // every hour
// if (hour < 0) hour += 60000; // every minute (for testing)
setTimeout(() => {
console.log('check contest stuff');
contestFunctions.checkStarttimes(client);
contestFunctions.checkDeadlines(client);
setTimeout(() => {
contestFunctions.checkEndVoting(client);
}, 5000);
checkContests();
}, hour);
}
checkContests();
});
client.on('messageReactionAdd', async (messageReaction, user) => {
if (user.bot) return;
console.log('manage reactions');
contestFunctions.manageReactions(client, messageReaction, user);
});
client.on('message', async message => {
// don't do anything in dm's
if (message.guild === null || message.channel.type === 'dm') return;
// search for emotes in the message
let found = message.content.match(/<a?:([^:]*):([^>]*)>/g);
if (found != null) {
let emotesInMessage = [];
// if the message contains multiple of one emote log all of them
for (let i = 0; i < found.length; i++) {
let emote = found[i].match(/<a?:([^:]*):([^>]*)>/i);
emotesInMessage.push(emote);
// emote[0].startsWith('<a') ? functions.logEmote(message, emote, true) : functions.logEmote(message, emote);
}
const uniqueEmotesInMessage = emotesInMessage.reduce((acc, current) => {
const x = acc.find(item => item.input === current.input);
if (!x) {
return acc.concat([current]);
} else {
return acc
};
}, []);
uniqueEmotesInMessage.forEach(e => {
functions.logEmote(message, e);
});
}
functions.logMembers(server, message.member);
functions.logMessages(message);
// check for agree's and log them
if (message.channel.name.toLowerCase() === 'rules' && message.member.roles.find(r => r.name === 'newcomer')) functions.checkAgree(server, message);
// check for introduction messages
if (message.channel.name.toLowerCase() === 'introduce-yourself') functions.checkIntroduction(server, message);
let messageArray = message.content.split(/\s+/g);
let command = messageArray[0];
let args = messageArray.slice(1);
// log the pruning by TOS Bot
if (command === '!pruneNewcomers') functions.insertPrune();
// manual commands for contest testing
if (command === 'checkStarttimes') contestFunctions.checkStarttimes(client);
if (command === 'checkDeadlines') contestFunctions.checkDeadlines(client);
if (command === 'checkEndVoting') contestFunctions.checkEndVoting(client);
// only run commands if the prefix is right
if (!command.startsWith(prefix)) return;
// search for the given command and run it
let cmd = client.commands.get(command.slice(prefix.length));
if (cmd) cmd.run(client, message, args, db);
});
// log new members
client.on('guildMemberAdd', member => {
functions.logMembers(server, member, false);
functions.logMemberCount(server);
});
// log leaving members
client.on('guildMemberRemove', member => {
functions.logMembers(server, member, true);
functions.logMemberCount(server);
});
// log members if something updates
client.on('guildMemberUpdate', (oldMember, newMember) => {
functions.logMembers(server, newMember, false);
functions.logRolechange(server, oldMember, newMember);
});
client.on('guildBanAdd', (guild, user) => {
functions.logMemberBan(user, true);
});
client.on('guildBanRemove', (guild, user) => {
functions.logMemberBan(user, false);
});
// log new channels
client.on('channelCreate', channel => {
functions.logChannels(server, channel);
});
// log deleted channels
client.on('channelDelete', channel => {
functions.logChannels(server, channel, true);
});
// log updated channels
client.on('channelUpdate', (oldChannel, newChannel) => {
if (newChannel.id === '581980129182613505' || newChannel.id === '581980094373822484' || newChannel.id === '598887260607217675' || newChannel.id === '581980156244131856') return;
functions.logChannels(server, newChannel);
});
client.on('error', err => {
logger.error(err, {logType: 'error', time: Date.now()});
console.error(err);
});
// login
client.login(botsettings.token);