fixed Birthday Event, Renamed DB Colls
This commit is contained in:
+51
-31
@@ -5,7 +5,9 @@ module.exports = {
|
||||
name: Events.ClientReady,
|
||||
once: true,
|
||||
async execute(client) {
|
||||
console.log(`Ready! Logged in as ${client.user.tag}`)
|
||||
console.log(`Ready! Logged in as ${client.user.tag}`);
|
||||
|
||||
// Set presence
|
||||
client.user.setPresence({
|
||||
activities: [{
|
||||
name: 'Red Dead Depression',
|
||||
@@ -13,40 +15,58 @@ module.exports = {
|
||||
url: 'https://twitch.tv/desq_blocki'
|
||||
}],
|
||||
status: 'online'
|
||||
})
|
||||
});
|
||||
|
||||
// if today is birthday
|
||||
function isToday(day, month) {
|
||||
const currentYear = new Date().getFullYear();
|
||||
const inputDate = new Date(currentYear, month - 1, day);
|
||||
// Check if a date matches today
|
||||
const isToday = (day, month) => {
|
||||
const today = new Date();
|
||||
return (
|
||||
inputDate.getDate() === today.getDate() &&
|
||||
inputDate.getMonth() === today.getMonth() &&
|
||||
inputDate.getFullYear() === today.getFullYear()
|
||||
);
|
||||
}
|
||||
async function isBirthday() {
|
||||
const db = mClient.db(process.env.M_DB)
|
||||
const bdayColl = db.collection('birthdays')
|
||||
const allBirthdays = await bdayColl.find().toArray()
|
||||
for (let index = 0; index < allBirthdays.length; index++) {
|
||||
if (isToday(allBirthdays[index].day, allBirthdays[index].month)) {
|
||||
let guild = client.guilds.cache.get(process.env.D_GuildID)
|
||||
let member = guild.members.cache.get(allBirthdays[index].userID)
|
||||
client.emit('Birthday', member)
|
||||
return today.getDate() === day && today.getMonth() === month - 1;
|
||||
};
|
||||
|
||||
// Handle birthdays
|
||||
const handleBirthdays = async () => {
|
||||
const guild = await client.guilds.fetch(process.env.D_GuildID);
|
||||
const db = mClient.db(process.env.M_DB);
|
||||
|
||||
const bdayColl = db.collection('items_birthdays');
|
||||
const allBirthdays = await bdayColl.find().toArray();
|
||||
const bdayMap = new Map(allBirthdays.map(b => [b.userID, b]));
|
||||
|
||||
// Fetch birthday role
|
||||
const bdayRoleColl = db.collection('config_roles');
|
||||
const bdayRoleEntry = await bdayRoleColl.findOne({ guildID: guild.id });
|
||||
const birthdayRoleID = bdayRoleEntry.roleID;
|
||||
|
||||
if (!birthdayRoleID) {
|
||||
console.warn("No birthday role configured in DB");
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch all guild members
|
||||
const members = await guild.members.fetch();
|
||||
|
||||
for (const member of members.values()) {
|
||||
const dbEntry = bdayMap.get(member.id);
|
||||
|
||||
if (dbEntry && isToday(dbEntry.day, dbEntry.month)) {
|
||||
// Today is their birthday
|
||||
await member.roles.add(birthdayRoleID).catch(err =>
|
||||
console.error(`Failed to add birthday role to ${member.user.tag}:`, err)
|
||||
);
|
||||
console.log(`Added birthday role to ${member.user.tag}`);
|
||||
client.emit('Birthday', member);
|
||||
} else {
|
||||
// remove any residual birthday roles
|
||||
try {
|
||||
let guild = client.guilds.cache.get(process.env.D_GuildID)
|
||||
let member = guild.members.cache.get(allBirthdays[index].userID)
|
||||
await member.roles.remove('702877228857557002')
|
||||
} catch (error) {
|
||||
console.error('Could not remove role', error)
|
||||
// Default fallback: remove birthday role if present
|
||||
if (member.roles.cache.has(birthdayRoleID)) {
|
||||
await member.roles.remove(birthdayRoleID).catch(err =>
|
||||
console.error(`Failed to remove birthday role from ${member.user.tag}:`, err)
|
||||
);
|
||||
console.log(`Removed birthday role from ${member.user.tag} (not today or not in DB)`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
isBirthday()
|
||||
};
|
||||
|
||||
await handleBirthdays();
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user