Files
Arthonor-Neo/events/ClientReady.js
T

72 lines
2.8 KiB
JavaScript

const { Events, ActivityType } = require('discord.js');
const { mClient } = require('..');
module.exports = {
name: Events.ClientReady,
once: true,
async execute(client) {
console.log(`Ready! Logged in as ${client.user.tag}`);
// Set presence
client.user.setPresence({
activities: [{
name: 'Red Dead Depression',
type: ActivityType.Streaming,
url: 'https://twitch.tv/desq_blocki'
}],
status: 'online'
});
// Check if a date matches today
const isToday = (day, month) => {
const today = new Date();
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 {
// 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)`);
}
}
}
};
await handleBirthdays();
}
};