forked from cremetv/Jabstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (170 loc) · 6.54 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
const botsettings = require('./botsettings.json');
const Discord = require('discord.js');
const fs = require('fs');
const config = require('./utility/config');
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 prefix = botsettings.prefix;
const serverId = botsettings.serverId;
const contestChat = botsettings.contestChat;
const client = new Discord.Client({disableEveryone: true});
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);
if (props.help.alias) client.commands.set(props.help.alias, props);
});
});
let server;
client.on('ready', async () => {
server = client.guilds.get(serverId);
const contestChannel = client.channels.get(contestChat);
contestChannel.fetchMessages().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); // if no channel is given as argument => update all
// for example functions.logChannels(server, channel);
functions.logMemberCount(server);
functions.logMembers(server); // functions.logMembers(server, member);
// log daily userCount
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();
/****************
* 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
if (hour < 0) hour += 3600000;
// if (hour < 0) hour += 60000; // minutes
setTimeout(() => {
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 => {
if (message.guild === null || message.channel.type === 'dm') return;
let found = message.content.match(/<a?:([^:]*):([^>]*)>/g);
if (found != null) {
for (let i = 0; i < found.length; i++) {
let emote = found[i].match(/<a?:([^:]*):([^>]*)>/i);
functions.logEmote(message, emote);
// emote[0].startsWith('<a') ? functions.logEmote(message, emote, true) : functions.logEmote(message, emote);
}
}
functions.logMembers(server, message.member);
functions.logMessages(message);
if (message.channel.name.toLowerCase() === 'rules' && message.member.roles.find(r => r.name === 'newcomer')) functions.checkAgree(server, message);
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);
if (command === '!pruneNewcomers') functions.insertPrune();
if (command === 'checkStarttimes') contestFunctions.checkStarttimes(client);
if (command === 'checkDeadlines') contestFunctions.checkDeadlines(client);
if (command === 'checkEndVoting') contestFunctions.checkEndVoting(client);
if (!command.startsWith(prefix)) return;
let cmd = client.commands.get(command.slice(prefix.length));
if (cmd) cmd.run(client, message, args, db);
});
client.on('guildMemberAdd', member => {
functions.logMembers(server, member, false);
functions.logMemberCount(server);
});
client.on('guildMemberRemove', member => {
functions.logMembers(server, member, true);
functions.logMemberCount(server);
});
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);
});
client.on('channelCreate', channel => {
functions.logChannels(server, channel);
});
client.on('channelDelete', channel => {
functions.logChannels(server, channel, true);
});
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);