-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
124 lines (114 loc) · 3.23 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
require("dotenv").config()
const express=require("express"),app=express();app.get("/",function(e,p){p.send("Hello World")}),app.listen(3e3);
const { Client } = require("discord.js");
const { GCommands } = require("gcommands");
const Keyv = require("keyv");
const client = new Client({
disableMentions: "everyone"
});
client.db = new Keyv();
client.config = require("./config.json");
client.cachedInvites = {};
client.findDefChannel = (guild, type) => {
let finalChannel = null;
if(type == "system_channel") {
let allSystemChannels = client.config.allWelcomeChannels
finalChannel = guild.channels.cache.find(c => allSystemChannels.find(a => c.name.toLowerCase().includes(a.toLowerCase()) || a.toLowerCase().includes(c.name.toLowerCase())));
}
return finalChannel;
}
client.guildData = {
invites: [],
systemChannel: null
};
client.guildMemberData = {
userId: null,
invitedById: null,
regular: [],
bonus: 0,
fake: [],
leaves: []
}
client.cacheInvites = async () => {
let data = {};
let guilds = client.guilds.cache.array();
for (let i = 0; i < guilds.length; i++) {
let g = guilds[i];
if (g.me.hasPermission("MANAGE_GUILD")) {
let invites;
try {
invites = (await g.fetchInvites()).array();
} catch { /* ¯\_(ツ)_/¯ */ }
if (invites) {
data[g.id] = [];
for (let i = 0; i < invites.length; i++) {
let inv = invites[i];
data[g.id].push({
code: inv.code,
maxAge: inv.maxAge,
maxUses: inv.maxUses,
temporary: inv.temporary,
channel: inv.channel,
user: inv.inviter,
uses: inv.uses,
expiresAt: inv.expiresAt
});
}
}
}
}
return data;
}
client.calculateInvites = (invs) => {
let f = 0;
f += invs.regular.length;
f += parseInt(invs.bonus);
f -= invs.fake.length;
f -= invs.leaves.length;
if (f < 0) return 0;
return f;
}
client.updateDatabase = async () => {
let guilds = client.guilds.cache.array();
for (let i = 0; i < guilds.length; i++) {
let g = guilds[i];
let gdb = await client.db.get(`guild_${g.id}`);
let members = g.members.cache.array();
let forceSet = false;
if (!gdb) {
let data = JSON.parse(JSON.stringify(client.guildData));
data.systemChannel = client.findDefChannel(g, "system_channel");
gdb = data;
forceSet = true;
}
let addShit = [];
for (let j = 0; j < members.length; j++) {
if (!gdb.invites.find(a => a.userId === members[j].id) && !members[j].user.bot) {
let dt = JSON.parse(JSON.stringify(client.guildMemberData));
dt.userId = members[j].id;
addShit.push(dt);
}
}
if (addShit.length > 0 || forceSet) {
if (addShit.length > 0) gdb.invites.push(...addShit);
await client.db.set(`guild_${g.id}`, gdb);
}
}
}
client.on("ready", async () => {
new GCommands(client, {
cmdDir: "commands/",
eventDir: "events/",
language: "english",
unkownCommandMessage: false,
slash: {
slash: 'both',
prefix: '!',
},
defaultCooldown: "3s"
});
client.cachedInvites = await client.cacheInvites();
await client.updateDatabase();
});
client.login(process.env.token);
"Garlic-Team <3";