Skip to content

Commit

Permalink
move gMemberAdd and Remove to new structure, make invite collection g…
Browse files Browse the repository at this point in the history
…lobal
  • Loading branch information
jvnipers committed Jan 15, 2025
1 parent 2d22962 commit bc7f9b2
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 192 deletions.
156 changes: 156 additions & 0 deletions src/events/guildMemberAdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
const { EmbedBuilder, Events } = require("discord.js");
require("dotenv").config();
const autoroles = require("../Schemas/autorole.js");
const mutes = require("../Schemas/moderation/mute.js");
const muteRoles = require("../Schemas/moderation/mute-role.js");
const schema = require("../Schemas/base-system.js");
const logs = require("../Schemas/logger/members.js");
const settings = require("../Schemas/logger/settings.js");

module.exports = {
name: Events.GuildMemberAdd,
async execute(member, client) {
if (!member || !client) {
return;
}

const autoroleData = await autoroles.findOne({ Guild: member.guild.id });
if (autoroleData) {
if (!autoroleData.Roles.length) return;
try {
for (const roleId of autoroleData.Roles) {
const role = member.guild.roles.cache.get(roleId);
if (role) {
await member.roles.add(role);
}
}
} catch (e) {
return console.log(e);
}
}

const muteData = await mutes.findOne({
Guild: member.guild.id,
User: member.user.id,
});
const muteRole = await muteRoles.findOne({
Guild: member.guild.id,
});
if (muteData) {
if (!muteRole) return;
try {
const role = member.guild.roles.cache.get(muteRole.Role);
if (role) {
await member.roles.add(role);
}
} catch (e) {
return console.log(e);
}
}

const settingsData = await settings.findOne({
Guild: member.guild.id,
});
if (settingsData.Members === false) return;
if (settingsData.Store === false && settingsData.Post === false) return;

const data = await schema.findOne({
Guild: member.guild.id,
ID: "audit-logs",
});
if (!data || !data.Channel) return;
const channel = client.channels.cache.get(data.Channel);
if (!channel) return;

const logData = await logs.findOne({
Guild: member.guild.id,
User: member.user.id,
});

const newInvites = await member.guild.invites.fetch();
const oldInvites = client.invites.get(member.guild.id) || new Map();

const invite = newInvites.find((i) => i.uses > oldInvites.get(i.code));

const date = new Date();

const embed = new EmbedBuilder()
.setColor("#ff00b3")
.setTimestamp()
.setTitle(`${member.user.username} Has Joined the Server!`)
.setFooter({ text: `FKZ • ID: ${member.user.id}` })
.addFields({
name: "User",
value: `<@${member.user.id}> - \`${member.user.username}\``,
inline: true,
});

if (logData && logData.Avatar) {
embed.setAuthor({
name: `Member Joined`,
iconURL: member.user.avatarURL({ size: 256 })
? logData.Avatar
: "https://files.femboy.kz/web/images/avatars/unknown.png",
});
} else {
embed.setAuthor({
name: `Member Joined`,
iconURL:
member.user.avatarURL({ size: 256 }) ||
"https://files.femboy.kz/web/images/avatars/unknown.png",
});
}

try {
if (!logData && settingsData.Store === true) {
await logs.create({
Guild: member.guild.id,
User: member.user.id,
Name: member.user.username,
Nickname: member.nickname || member.user.username,
Displayname: member.displayName,
Avatar: member.user.displayAvatarURL({ size: 128 }),
Banner: member.user.bannerURL({ size: 128 }) || null,
Roles: member.roles.cache.map((role) => role.id) || [],
Joined: member.joinedAt || date,
Created: member.user.createdAt,
});
}

if (settingsData.Post === true) {
if (!invite) {
embed.addFields(
{
name: "Inviter",
value: "None",
inline: false,
},
{
name: "Invite",
value: "Unknown / Vanity",
inline: false,
}
);
await channel.send({ embeds: [embed] });
} else {
const inviter = await client.users.fetch(invite.inviter.id);
embed.addFields(
{
name: "Inviter",
value: `${inviter}`,
inline: false,
},
{
name: "Invite",
value: `<https://discord.gg/${invite.code}>`,
inline: false,
}
);
await channel.send({ embeds: [embed] });
}
}
} catch (error) {
console.error("Error in guildMemberAdd event:", error);
}
},
};
69 changes: 69 additions & 0 deletions src/events/guildMemberRemove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { EmbedBuilder, Events } = require("discord.js");
const schema = require("../Schemas/base-system.js");
const logs = require("../Schemas/logger/members.js");
const settings = require("../Schemas/logger/settings.js");

module.exports = {
name: Events.GuildMemberRemove,
async execute(member, client) {
if (!member || !client) {
return;
}

const settingsData = await settings.findOne({
Guild: member.guild.id,
});
if (settingsData.Members === false) return;
if (settingsData.Store === false && settingsData.Post === false) return;

const data = await schema.findOne({
Guild: member.guild.id,
ID: "audit-logs",
});
if (!data || !data.Channel) return;
const channel = client.channels.cache.get(data.Channel);
if (!channel) return;

const logData = await logs.findOne({
Guild: member.guild.id,
User: member.user.id,
});

const embed = new EmbedBuilder()
.setColor("#ff00b3")
.setTimestamp()
.setFooter({ text: `FKZ • ID: ${member.user.id}` })
.setTitle(`${member.user.username} has left the server`)
.setDescription(`<@${member.user.id}> has left the Server`);

if (logData && logData.Avatar) {
embed.setAuthor({
name: `Member Left`,
iconURL: member.user.avatarURL({ size: 256 })
? logData.Avatar
: "https://files.femboy.kz/web/images/avatars/unknown.png",
});
} else {
embed.setAuthor({
name: `Member Left`,
iconURL:
member.user.avatarURL({ size: 256 }) ||
"https://files.femboy.kz/web/images/avatars/unknown.png",
});
}

try {
if (logData && settingsData.Store === true) {
await logs.deleteMany({
Guild: member.guild.id,
User: member.user.id,
});
}
if (settingsData.Post === true) {
await channel.send({ embeds: [embed] });
}
} catch (error) {
console.error(`Error in guildMemberRemove event:`, error);
}
},
};
16 changes: 15 additions & 1 deletion src/events/ready.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { Events } = require("discord.js");
const { Events, PermissionsBitField, Collection } = require("discord.js");
const mongoose = require("mongoose");
require("dotenv").config();
const mongodbURL = process.env.MONGODB_URL;

client.invites = new Collection();

mongoose.set("strictQuery", false);

module.exports = {
Expand All @@ -24,5 +26,17 @@ module.exports = {
} catch (error) {
console.error("Error connecting to MongoDB:", error);
}

client.guilds.cache.forEach(async (guild) => {
const clientMember = guild.members.cache.get(client.user.id);
if (!clientMember.permissions.has(PermissionsBitField.Flags.ManageGuild))
return;

const firstInvites = await guild.invites.fetch().catch(console.log);
client.invites.set(
guild.id,
new Collection(firstInvites.map((invite) => [invite.code, invite.uses]))
);
});
},
};
Loading

0 comments on commit bc7f9b2

Please sign in to comment.