forked from aloshai/invite-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
140 lines (123 loc) · 5.9 KB
/
bot.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
const { Collection, Client } = require("discord.js");
const Database = require("./Helpers/Database");
const client = global.client;
//#region Invite Manager
const Invites = new Collection();
//#region Load
client.on("ready", () => {
client.guilds.cache.forEach(guild => {
guild.fetchInvites().then(_invites => {
Invites.set(guild.id, _invites);
}).catch(err => { });
});
});
client.on("inviteCreate", (invite) => {
var gi = Invites.get(invite.guild.id);
gi.set(invite.code, invite);
Invites.set(invite.guild.id, gi);
});
client.on("inviteDelete", (invite) => {
var gi = Invites.get(invite.guild.id);
gi.delete(invite.code);
Invites.set(invite.guild.id, gi);
});
//#endregion
//#region Continuity
client.on("guildCreate", (guild) => {
guild.fetchInvites().then(invites => {
Invites.set(guild.id, invites);
}).catch(e => {})
});
//#endregion
//#region Counter
client.on("guildMemberAdd", (member) => {
//const gi = new Collection().concat(Invites.get(member.guild.id));
const db = new Database("./Servers/" + member.guild.id, "Invites"), gi = (Invites.get(member.guild.id) || new Collection()).clone(), settings = new Database("./Servers/" + member.guild.id, "Settings").get("settings") || {};
var guild = member.guild, fake = (Date.now() - member.createdAt) / (1000 * 60 * 60 * 24) <= 3 ? true : false, channel = guild.channels.cache.get(settings.Channel);
guild.fetchInvites().then(invites => {
// var invite = invites.find(_i => gi.has(_i.code) && gi.get(_i.code).maxUses != 1 && gi.get(_i.code).uses < _i.uses) || gi.find(_i => !invites.has(_i.code)) || guild.vanityURLCode;
var invite = invites.find(_i => gi.has(_i.code) && gi.get(_i.code).uses < _i.uses) || gi.find(_i => !invites.has(_i.code)) || guild.vanityURLCode;
Invites.set(member.guild.id, invites);
var content = `${member} is joined the server.`, total = 0, regular = 0, _fake = 0, bonus = 0;
if(invite == guild.vanityURLCode) content = settings.defaultMessage ? settings.defaultMessage : `-member- is joined the server! But don't know that invitation he came up with. :tada:`;
else content = settings.welcomeMessage ? settings.welcomeMessage : `The -member-, joined the server using the invitation of the -target-.`;
if (invite.inviter) {
db.set(`invites.${member.id}.inviter`, invite.inviter.id);
if(fake){
total = db.add(`invites.${invite.inviter.id}.total`, 1);
_fake = db.add(`invites.${invite.inviter.id}.fake`, 1);
}
else{
total = db.add(`invites.${invite.inviter.id}.total`, 1);
regular = db.add(`invites.${invite.inviter.id}.regular`, 1);
}
var im = guild.member(invite.inviter.id);
bonus = db.get(`invites.${invite.inviter.id}.bonus`) || 0;
if(im) global.onUpdateInvite(im, guild.id, Number(total + Number(bonus)));
}
db.set(`invites.${member.id}.isfake`, fake);
if(channel){
content = content
.replace("-member-", `${member}`)
.replace("-target-", `${invite.inviter}`)
.replace("-total-", `${total + bonus}`)
.replace("-regular-", `${regular}`)
.replace("-fakecount-", `${_fake}`)
.replace("-invite-", `${invite && invite.code != undefined ? invite.code : "what is that?"}`)
.replace("-fake-", `${fake}`);
channel.send(content);
}
}).catch();
});
client.on("guildMemberRemove", (member) => {
const db = new Database("./Servers/" + member.guild.id, "Invites"), settings = new Database("./Servers/" + member.guild.id, "Settings").get("settings") || {};
var total = 0, bonus = 0, regular = 0, fakecount = 0, channel = member.guild.channels.cache.get(settings.Channel), content = settings.leaveMessage ? settings.leaveMessage : `${member} is left the server.`, data = db.get(`invites.${member.id}`);
if(!data){
if(channel){
content = content
.replace("-member-", `${member}`);
channel.send(content);
}
return;
}
if(data.isfake && data.inviter){
fakecount = db.sub(`invites.${data.inviter}.fake`, 1);
total = db.sub(`invites.${data.inviter}.total`, 1);
}
else if(data.inviter){
regular = db.sub(`invites.${data.inviter}.regular`, 1);
total = db.sub(`invites.${data.inviter}.total`, 1);
}
if(data.inviter) bonus = db.get(`invites.${data.inviter}.bonus`) || 0;
var im = member.guild.member(data.inviter)
if(im) global.onUpdateInvite(im, member.guild.id, Number(total) + Number(bonus));
db.add(`invites.${data.inviter}.leave`, 1);
if(channel){
content = content
.replace("-member-", `${member}`)
.replace("-target-", `${im ? im : data.inviter}`)
.replace("-total-", `${Number(total) + Number(bonus)}`)
.replace("-regular-", `${regular}`)
.replace("-fakecount-", `${fakecount}`)
.replace("-fake-", `${data.isfake}`);
channel.send(content);
}
});
//#endregion
//#region Reward
global.onUpdateInvite = (guildMember, guild, total) => {
if(!guildMember.manageable) return;
const rewards = new Database("./Servers/" + guild, "Rewards").get("rewards") || [];
if(rewards.length <= 0) return;
var taken = rewards.filter(reward => reward.Invite > total && guildMember.roles.cache.has(reward.Id));
taken.forEach(take => {
guildMember.roles.remove(take.Id);
});
var possible = rewards.filter(reward => reward.Invite <= total && !guildMember.roles.cache.has(reward.Id));
possible.forEach(pos =>{
guildMember.roles.add(pos.Id);
});
}
//#endregion
//#endregion
client.login(global.Settings.Token);