-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
373 lines (347 loc) · 15.2 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const config = require(`./config.json`);
const prefix = config.prefix;
const chalk = require('chalk');
const Discord = require('discord.js');
const client = new Discord.Client();
const ddiff = require('return-deep-diff');
const fs = require('fs');
const moment = require('moment');
// Connect to Database before anything else
const sqlite3 = require('sqlite3').verbose();
client.db = new sqlite3.Database('./utils/users.db', sqlite3.OPEN_READWRITE, (err) => {
if(err){
console.error(err.message);
}
console.log(`Connected to DB`);
});
require('./utils/eventLoader')(client);
const log = (msg) => {
console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}] ${msg}`);
}
client.getInfoValues = async (client) => {
let emoji_fish = [];
emoji_fish.push(`:SmallFish:517449760349618206`);
emoji_fish.push(`:MediumFish:517449758940332053`);
emoji_fish.push(`:LargeFish:517449759372607507`);
emoji_fish.push(`:SuperFish:517449759963873302`);
emoji_fish.push(`:LegendaryFish:517449759313887283`);
emoji_fish.push(`:Magikarp:517449753970212875`);
client.fish_emoji = emoji_fish;
let emoji_mine = [];
emoji_mine.push(`:Copper:521464980336541699`);
emoji_mine.push(`:Nickel:521465405886169088`);
emoji_mine.push(`:Lead:521465783969251348`);
emoji_mine.push(`:Amethyst:521466117298978822`);
emoji_mine.push(`:Gold:521465161807167490`);
emoji_mine.push(`:Uranium:521464981968125963`);
client.mine_emoji = emoji_mine;
let emoji_chop = [];
emoji_chop.push(`:SmallFish:517449760349618206`);
emoji_chop.push(`:MediumFish:517449758940332053`);
emoji_chop.push(`:LargeFish:517449759372607507`);
emoji_chop.push(`:SuperFish:517449759963873302`); // https://cdn.discordapp.com/emojis/517449760349618206.png
emoji_chop.push(`:LegendaryFish:517449759313887283`);
emoji_chop.push(`:Magikarp:517449753970212875`);
client.chop_emoji = emoji_chop;
let sql_count_users = `SELECT count(*) AS count FROM users`;
let now = moment().format('x');
client.db.get(sql_count_users, async (err, row) => {
if(err) return console.error(`index.js getInfoValues ${err.message}`);
client.totalUsers = row.count;
client.timeInitiated = now;
});
}
client.set_up_server = async (client, guild) => {
console.log(`Setting up server`);
let role_name = `DiscordLoved`;
const role_check = guild.roles.find(role => role.name === 'DiscordLoved');
if(role_check){
role = role_check;
client.create_channels(role, client, guild);
} else {
client.create_role(guild).then(role => {
client.create_channels(role, client, guild);
});
}
}
client.check_channels = async (client, guild_object, guild) => {
let channel_missing = false;
let setup_channel = guild_object.channels.find(channel => channel.name === `discord-love-setup`)
let main_channel = guild_object.channels.find(channel => channel.name === `discord-love`)
let discordlove_category = guild_object.channels.find(channel => channel.name === `DiscordLove` && channel.type == `category`);
let setup_channel_database = guild_object.channels.find(channel => channel.id === guild.channel_setup)
let main_channel_database = guild_object.channels.find(channel => channel.id === guild.channel_main)
if(!setup_channel) channel_missing = true;
if(!main_channel) channel_missing = true;
if(!discordlove_category) channel_missing = true;
if(!setup_channel_database) channel_missing = true;
if(!main_channel_database) channel_missing = true;
if(channel_missing) client.set_up_server(client, guild_object);
}
client.remove_old_channels = async (guild) => {
let setup_channel = guild.channels.find(channel => channel.name === `discord-love-setup`)
let main_channel = guild.channels.find(channel => channel.name === `discord-love`)
let discordlove_category = guild.channels.find(channel => channel.name === `DiscordLove` && channel.type == `category`);
let discordlove_test = guild.channels.find(channel => channel.name === `discord-love-test`);
if(setup_channel) setup_channel.delete();
if(main_channel) main_channel.delete();
if(discordlove_category) discordlove_category.delete()
if(discordlove_test) discordlove_test.delete();
}
client.create_role = async (guild) => {
guild.createRole({
name: `DiscordLoved`
}).then(role => {
return role;
})
}
client.create_channels = async (role, client, guild) => {
await client.remove_old_channels(guild);
guild.createChannel(`DiscordLove`, `category`, [{
id: guild.id,
}])
.then(category => {
// Create Setup channel
guild.createChannel(`discord-love-setup`, `text`)
.then(channel => {
channel.setParent(category.id);
channel.overwritePermissions(role, {
VIEW_CHANNEL: false
});
channel.overwritePermissions(guild.id, {
VIEW_CHANNEL: true
});
// Create default channel
guild.createChannel(`discord-love`, `text`)
.then(channel_default => {
channel_default.setParent(category.id);
channel_default.overwritePermissions(role, {
VIEW_CHANNEL: true
});
channel_default.overwritePermissions(guild.id, {
VIEW_CHANNEL: false
});
// Check for old rows
let sql_check = `SELECT * FROM guilds WHERE guild_identifier = ${guild.id}`;
client.db.get(sql_check, (err, row) => {
if(err) return console.error(`index.js create_channels ${err.message}`);
let sql_guild = ``;
let new_guild = true;
if(row){
// Guild is already in the database, lets update the channels
sql = `UPDATE guilds SET channel_setup = ${channel.id}, channel_main = ${channel_default.id} WHERE guild_identifier = ${guild.id}`;
new_guild = false;
} else {
// New guild is being added
sql = `INSERT INTO guilds (guild_identifier, guild_owner, channel_setup, channel_main) VALUES(${guild.id},${guild.owner.id},${channel.id},${channel_default.id})`;
}
client.db.run(sql, (err) => {
if(err) return console.error(err.message);
if(new_guild){
embed = new Discord.RichEmbed()
.setColor("#00A30D")
.setAuthor(`${guild.name} (${guild.memberCount.format(0)} members)`, guild.iconURL)
.setFooter(`Owner: ${guild.owner.user.username}#${guild.owner.user.discriminator}`)
.setTimestamp();
client.channels.get(config.log_guild).send(embed);
}
client.user.setActivity(`on ${client.guilds.size.format(0)} servers`);
});
})
})
})
})
}
client.guild_info = async (guild, extras, callback) => {
try {
let sql = `SELECT * FROM guilds WHERE guild_identifier = ${guild}`;
if(extras != '') sql = `SELECT * FROM guilds WHERE guild_identifier = ${user} ${extras}`;
await client.db.get(sql, (err, row) => {
if(err) return console.error(`index.js - ${err.message}`);
// console.log(chalk.bold.red(`client.guild_info index.js`));
// console.log(row);
return callback(row);
});
} catch(e) {
console.error(e);
}
}
client.user_info = async (user, extras, callback) => {
try{
let sql = `SELECT * FROM users WHERE user_discord = ${user}`;
if(extras != '') sql = `SELECT * FROM users WHERE user_discord = ${user} ${extras}`;
await client.db.get(sql, (err, row) => {
if(err) return console.error(`index.js - ${err.message}`);
// console.log(chalk.bold.red(`client.user_info index.js`));
// console.log(row);
return callback(row);
});
} catch(e) {
console.error(e);
}
}
client.title_info = async (title, extras, callback) => {
try{
let sql = `SELECT * FROM titles WHERE title_id = ${title}`;
if(extras != '') sql = `SELECT * FROM titles WHERE title_id = ${title} ${extras}`;
await client.db.get(sql, (err, row) => {
if(err) return console.error(`index.js - ${err.message}`);
// console.log(chalk.bold.red(`client.user_info index.js`));
// console.log(row);
return callback(row);
});
} catch(e) {
console.error(e);
}
}
client.update_money = async (message, user_id, callback) => {
try {
await client.check_premium_status(user_id, async () => {
await client.check_reputation_status(user_id, async () => {
let now = moment().format('x');
await client.user_info(user_id, '', async (user) => {
let crate_chance = 0;
if(user){
let time_difference = now - user.ts_message;
if(user.premium_status > 0) time_difference *= 2;
let money_to_add = Math.floor(time_difference/1000) * user.user_cps;
let sql = ``;
// Has their history never been logged?
if(user.user_money_history == 0){
sql = `UPDATE users SET user_money = ${user.user_money + money_to_add}, user_money_history = ${user.user_money + money_to_add} WHERE user_discord=${user.user_discord}`;
} else {
sql = `UPDATE users SET user_money = ${user.user_money + money_to_add}, user_money_history = ${user.user_money_history + money_to_add} WHERE user_discord=${user.user_discord}`;
}
await client.db.run(sql, (err) => {
if(err) return console.error(`index.js update_money ${err.message}`);
// Update message counter
if(time_difference > 60 * 1000 && !message.content.startsWith(config.prefix)){
let sql_update_message;
let level_up = false;
let experience_random = ((user.premium_status > 0 ? 2 : 1) * (Math.floor(Math.random() * 10))+1);
if(message.content.length >= 250) experience_random *= 2;
if(message.content.length >= 1000) experience_random *= 2;
if(message.content.length >= 1500) experience_random *= 1.5;
let next_level_requirement = Math.floor(Math.pow(user.user_level+1, 1.8)*100);
if(user.user_level >= ((user.prestige_level+1)*20) && user.prestige_level < 5){
level_up = false;
experience_random = 0;
}
if(user.user_experience + experience_random >= next_level_requirement){
sql_update_message = `UPDATE users SET user_experience = ${user.user_experience + experience_random}, user_level = ${user.user_level + 1}, counter_messages = ${user.counter_messages+1}, ts_message = ${now} WHERE user_discord = ${user.user_discord}`;
level_up = true;
} else {
sql_update_message = `UPDATE users SET user_experience = ${user.user_experience + experience_random}, counter_messages = ${user.counter_messages+1}, ts_message = ${now} WHERE user_discord = ${user.user_discord}`;
}
if(level_up){
let embed_colour = '#' + user.user_colour;
if(user.user_colour == 'RAND') embed_colour = '#' + Math.floor(Math.random()*16777215).toString(16);
let embed = new Discord.RichEmbed()
.setColor(embed_colour)
.setAuthor(`${message.member.displayName} has just reached level ${user.user_level + 1}`, message.author.avatarURL)
.setTimestamp();
if(user.user_level == ((user.prestige_level+1)*20)){
embed.setFooter(`This is the max level you can reach right now. You need to prestige.`);
}
message.channel.send(embed);
// Set users nickname on the Official DiscordLove Server
// if(message.guild.id == '513786798737195008' && message.author.id != config.botowner)
// message.member.setNickname(`[${user.user_level+1}] ${message.author.username}`);
}
client.db.run(sql_update_message, (err) => {
if(err) return console.error(`index.js update_counter_messages ${err.message}`);
});
// Random rare crate
let random_crate_number = Math.floor(Math.random() * (user.premium_status > 0 ? 80 : 100));
if(random_crate_number == 0) crate_chance = 1;
//if(user.user_id == 1) crate_chance = 1;
}
return callback(crate_chance);
})
}
});
});
});
} catch(e) {
console.log(e.stack);
}
}
client.check_premium_status = async (user_id, callback) => {
let discordlove_guild = client.guilds.find(guild => guild.id === '513786798737195008');
let discordlove_guild_member = discordlove_guild.members.find(member => member.id === user_id);
let now = moment().format('x');
await client.user_info(user_id, '', (user) => {
if(user){
if(user.premium_status == 1) {
let time_difference = now - user.premium_time;
if(time_difference > 0){
let sql = `UPDATE users SET premium_status = 0 WHERE user_discord = ${user_id}`;
client.db.run(sql, [], (err) => {
if(err) return console.error(`index.js check_premium_status ${err.message}`);
if(discordlove_guild_member){
discordlove_guild_member.removeRoles([
discordlove_guild.roles.find(role => role.name === "Lifetime Premium"),
discordlove_guild.roles.find(role => role.name === "Premium"),
])
}
});
}
}
}
return callback();
});
}
client.check_reputation_status = async (user_id, callback) => {
let now = moment().format('x');
await client.user_info(user_id, '', (user) => {
if(user){
if(now - (1000 * 60 * 60 * 24) >= user.ts_reputation){
let sql = `UPDATE users SET reputation_given_today = 0 WHERE user_discord = ${user.user_discord}`;
client.db.run(sql, [], (err) => {
if(err) return console.error(`index.js check_reputation_status ${err.message}`);
});
}
}
return callback();
})
}
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
fs.readdir(`./commands/`, (err, files) => {
if (err) console.error(err);
log(chalk.bold.cyan(`Loading ${files.length} Commands`));
client.totalCommands = files.length;
files.forEach(f => {
let props = require(`./commands/${f}`);
log(chalk.bold.cyan(`- ${props.help.name}`));
client.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name);
});
});
});
client.reload = function(command){
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(`./commands/${command}`)];
let cmd = require(`./commands/${command}`);
client.commands.delete(command);
client.aliases.forEach((cmd, alias) => {
if(cmd === command) client.aliases.delete(alias);
});
client.commands.set(command, cmd);
cmd.conf.aliases.forEach(alias => {
client.aliases.set(alias, cmd.help.name);
});
resolve();
} catch(e){
reject(e);
}
});
};
client.elevation = function(msg){
let perms = 0;
if(msg.author.id == config.botowner) perms = 4;
return perms;
}
client.login(config.token);