-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
371 lines (332 loc) · 11.1 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
const {
Client,
Partials,
GatewayIntentBits,
PermissionsBitField,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ModalBuilder,
TextInputBuilder,
EmbedBuilder,
TextInputStyle,
TeamMemberMembershipState,
} = require("discord.js");
const {
loadCommands,
getUserData,
getSystemData,
connectDatabase,
formatMessage,
} = require("./Utils/Functions");
const config = require("./Utils/Config");
const InvitesSchema = require("./Database/InvitesSchema");
const GuildSchema = require("./Database/GuildSchema");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildScheduledEvents,
],
partials: [
Partials.Message,
Partials.User,
Partials.GuildScheduledEvent,
Partials.GuildMember,
],
});
const { inviteTracker } = require("discord-inviter");
const tracker = new inviteTracker(client);
client.on("ready", async () => {
console.log(`Logged in as ${client.user.tag} (${client.user.id})`);
await connectDatabase(config.MongoDB);
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.channel.isTextBased()) return;
if (interaction.user.bot) return;
if (interaction.isChatInputCommand()) {
await interaction
.deferReply({ fetchReply: true })
.catch((err) => console.log(err));
const { options, commandName, member, guild } = interaction;
if (
!interaction.member.permissions.any(PermissionsBitField.Flags.ManageGuild)
)
return interaction.editReply({
content: "❌ You don't have permissions to run this command",
});
if (commandName == "invite-settings") {
let channel = options.getChannel("channel");
let msg = await interaction.editReply({
content: `Click the button bellow and send the message you want\n\nModifiers Are:\`\`\`\n[inviter] = To Mention the inviter\n[inviterTag] = To get the inviter tag (${client.user.tag})\n[newMember] = To mention the new member\n[newMemberTag] = To get the new member tag (${client.user.tag})`,
components: [
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("invite-setup-button")
.setEmoji("🪄")
.setLabel("Setup Logger Msg")
.setStyle(ButtonStyle.Success)
),
],
});
let filter = (i) =>
i.user.id == interaction.user.id && i.customId == "invite-setup-button";
const collector = msg.createMessageComponentCollector({
filter,
time: 60000,
});
collector.on("collect", async (i) => {
const modal = new ModalBuilder()
.setTitle("Message Setup")
.setCustomId("message-setup-modal");
const msgInput = new TextInputBuilder()
.setCustomId("message-setup-modal-input")
.setLabel("What message do you want for logging?")
.setStyle(TextInputStyle.Paragraph)
.setMaxLength(100)
.setMinLength(5)
.setPlaceholder("Enter some text!")
.setRequired(true);
const row = new ActionRowBuilder().addComponents(msgInput);
modal.addComponents(row);
await i.showModal(modal);
const filter = (m) => m.customId === "message-setup-modal";
i.awaitModalSubmit({ filter, time: 60000 })
.then(async (modalInteraction) => {
if (modalInteraction.customId == "message-setup-modal") {
console.log("hi 2");
let input = modalInteraction.fields.getTextInputValue(
"message-setup-modal-input"
);
modalInteraction.reply({
content: `Done setup has been setted\nChannel: ${channel}\nMsg:\n\`\`\`\n${input}\n\`\`\``,
});
let data = await GuildSchema.findOne({ guildId: guild.id });
if (data) {
data.channelId = channel.id;
data.message = input;
data.status = true;
data.save;
} else {
await GuildSchema.create({
guildId: guild.id,
channelId: channel.id,
message: input,
});
}
}
})
.catch(console.error);
});
}
if (commandName == "remove-channel") {
let data = await GuildSchema.findOne({ guildId: guild.id });
if (!data)
return interaction.editReply(
"❌ There is already no data for this server"
);
if (data.status == true) {
data.status = false;
data.save();
await interaction.editReply("✅ Done logging system has been disabled");
} else {
data.status = true;
data.save();
await interaction.editReply("✅ Done logging system has been enabled");
}
}
if (commandName == "invites") {
let memberTo = options.getMember("user") || member;
if (memberTo.user.bot)
return interaction.editReply("❌ Bot's don't have invites");
let userData = await InvitesSchema.findOne({
guildId: guild.id,
inviterId: memberTo.id,
});
if (!userData)
return interaction.editReply("❌ This user don't have invites");
let invites = userData.inviters;
let invitesArray = [];
if (invites.length < 1)
return interaction.editReply("❌ This user don't have invites");
let leavedUsers = invites.filter((invite) => invite.isLeft == true);
let originals = Math.floor(invites.length - leavedUsers.length);
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: memberTo.user.tag,
iconURL: memberTo.user.displayAvatarURL(),
})
.setColor("Random")
.setTitle(`${memberTo.user.username} Invites Informations:`)
.addFields(
{
name: "✅ Total Invites",
value: `**${invites.length}** invite(s)`,
inline: true,
},
{
name: "❌ Lefted Invites",
value: `**${leavedUsers.length}** invite(s)`,
inline: true,
},
{
name: "🪄 True invites",
value: `**${originals}** invite(s)`,
inline: true,
}
)
.setTimestamp()
.setThumbnail(guild.iconURL()),
// .setDescription(`${invitesArray.join("\n")}\n`),
],
});
}
if (commandName == "check") {
let memberTo = options.getMember("user") || member;
if (memberTo.user.bot)
return interaction.editReply("❌ Bot's don't have invites");
let userData = await InvitesSchema.findOne({
guildId: guild.id,
inviterId: memberTo.id,
});
if (!userData)
return interaction.editReply("❌ This user don't have invites");
let invites = userData.inviters;
invites = invites.slice(-10);
let invitesArray = [];
if (invites.length < 1)
return interaction.editReply("❌ This user don't have invites");
invites.forEach((invite, i) => {
i++;
invitesArray.push(
`${i} - ${invite.isLeft ? "❌" : "✅"} <@!${invite.inviteeId}> **(${
invite.inviteeId
})** At (<t:${Math.floor(invite.invitedAt / 1000)}:R>)`
);
});
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: memberTo.user.tag,
iconURL: memberTo.user.displayAvatarURL(),
})
.setColor("Random")
.setTitle(`${memberTo.user.username} Invites Informations:`)
.setTimestamp()
.setThumbnail(guild.iconURL())
.setDescription(`${invitesArray.join("\n")}\n`)
.setFooter({ text: "❌ = Left | ✅ = Exist" }),
],
});
}
if (commandName == "resetinvites") {
let subCommand = interaction.options.getSubcommand();
if (subCommand == "user") {
let memberToReset = options.getMember("member") || member;
let data = await InvitesSchema.deleteOne({
guildId: guild.id,
inviterId: memberToReset.user.id,
});
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(`✅ ${memberToReset} invites has been resetted.`)
.setFooter({
text: `${client.user.tag} • Asked by ${interaction.user.tag}`,
})
.setTimestamp(),
],
});
}
if (subCommand == "all") {
let memberToReset = options.getMember("member") || member;
let data = await InvitesSchema.deleteMany({
guildId: guild.id,
});
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(`✅ Server invites has been resetted.`)
.setFooter({
text: `${client.user.tag} • Asked by ${interaction.user.tag}`,
})
.setTimestamp(),
],
});
}
}
}
});
tracker.on("guildMemberAdd", async (member, inviter, invite, error) => {
if (error) return console.error(error);
if (member.user.bot) return;
let systemData = await getSystemData(member.guild.id);
if (!systemData) return;
let welcomeChannel = await member.guild.channels
.fetch(systemData.channelId)
.catch((err) => console.log(err));
if (!welcomeChannel) return;
let data = await InvitesSchema.findOne({
guildId: member.guild.id,
inviterId: inviter.id,
});
if (data) {
data.inviters.push({
inviteeId: member.id,
invitedAt: Date.now(),
});
data.save();
} else {
await InvitesSchema.create({
guildId: member.guild.id,
inviterId: inviter.id,
inviters: [
{
inviteeId: member.id,
invitedAt: Date.now(),
},
],
});
}
let msg = formatMessage({
str: systemData.message,
words: [
{ replacement: "[inviter]", replacer: ` <@${inviter.id}>` },
{ replacement: "[inviterTag]", replacer: `${inviter.tag}` },
{ replacement: "[newMember]", replacer: `<@${member.user.id}>` },
{ replacement: "[newMemberTag]", replacer: `${member.user.tag}` },
],
});
if (systemData.status == true)
welcomeChannel.send(msg).catch((err) => console.log(err));
});
client.on("guildMemberRemove", async (member) => {
if (member.user.bot) return;
await InvitesSchema.updateMany(
{
guildId: member.guild.id,
"inviters.inviteeId": member.id,
},
{ $set: { "inviters.$.isLeft": true } }
);
});
client.login(config.botToken).catch((err) => console.log(err));
loadCommands({
token: config.botToken,
commands: require("./Commands").commandsArray,
clientId: config.clientId,
guildId: config.guildId,
public: false,
}); // guild commands
/*
for public commands use this
loadCommands({
token: config.token,
commands: commandsArray,
clientId: config.clientId,
}); // public commands
*/