fixed Birthday Event, Renamed DB Colls
This commit is contained in:
+12
-10
@@ -8,11 +8,11 @@ module.exports = {
|
||||
once: false,
|
||||
async execute(member, client) {
|
||||
console.log(`${member.user.username} hat Geburtstag!`)
|
||||
let found = null
|
||||
|
||||
|
||||
const db = mClient.db(process.env.DB)
|
||||
const channelsColl = db.collection('channels')
|
||||
const found = await channelsColl.find(
|
||||
const db = mClient.db(process.env.M_DB)
|
||||
const channelsColl = db.collection('config_channels')
|
||||
found = await channelsColl.find(
|
||||
{
|
||||
$and: [
|
||||
{ guildID: member.guild.id },
|
||||
@@ -25,12 +25,13 @@ module.exports = {
|
||||
return console.log('Channel not yet set for birthday!')
|
||||
}
|
||||
|
||||
|
||||
const guild = client.guilds.cache.get(found[0].guildID);
|
||||
const channel = guild.channels.cache.get(found[0].channelID);
|
||||
|
||||
const roleID = '702877228857557002'
|
||||
const role = guild.roles.cache.get(roleID)
|
||||
const roleColl = db.collection('config_roles')
|
||||
|
||||
// Bot Role MUST BE above birthday Role, Birthday Role MUST BE above user roles
|
||||
found = await roleColl.findOne({guildID: member.guild.id})
|
||||
const role = guild.roles.cache.get(found.roleID)
|
||||
|
||||
try {
|
||||
await member.roles.add(role)
|
||||
@@ -79,8 +80,9 @@ module.exports = {
|
||||
ctx.strokeText(text, textX, 60 + pfp.height);
|
||||
ctx.fillText(text, textX, 60 + pfp.height);
|
||||
|
||||
ctx.font = '30px sans-serif';
|
||||
text = `🥳 ${member.user.globalName ? member.user.globalName : member.user.username} 🎉`;
|
||||
// requires apt-get install ttf-ancient-fonts
|
||||
ctx.font = '30px "Noto Emoji", "Segoe UI Emoji", "Apple Color Emoji", sans-serif';
|
||||
text = `🎉 ${member.user.globalName ? member.user.globalName : member.user.username} 🎉`;
|
||||
textWidth = ctx.measureText(text).width;
|
||||
textX = canvas.width / 2 - textWidth / 2;
|
||||
textHeight = 30; // Approximate height of the text
|
||||
|
||||
+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();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -9,8 +9,8 @@ module.exports = {
|
||||
once: false,
|
||||
async execute(member, client) {
|
||||
console.log(`${member.user.username} joined the Server`);
|
||||
const db = mClient.db(process.env.DB)
|
||||
const channelsColl = db.collection('channels')
|
||||
const db = mClient.db(process.env.M_DB)
|
||||
const channelsColl = db.collection('config_channels')
|
||||
const found = await channelsColl.find(
|
||||
{
|
||||
$and: [
|
||||
|
||||
Reference in New Issue
Block a user