JaBa/helpers/birthdays.js

126 lines
4 KiB
JavaScript
Raw Normal View History

const { CronJob } = require("cron");
2022-01-04 02:18:28 +05:00
2022-08-30 14:26:56 +05:00
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
2022-08-30 14:26:56 +05:00
*/
module.exports.init = async client => {
2024-10-03 11:07:02 +05:00
const cronjob = new CronJob("0 5 * * *",
async function () {
// Iterate over all guilds the bot is in
for (const guild of client.guilds.cache.values()) {
try {
console.log(`Checking birthdays for "${guild.name}"`);
2024-06-17 17:00:03 +05:00
2024-10-03 11:07:02 +05:00
const guildData = await client.getGuildData(guild.id);
const channel = guildData.plugins.birthdays ? await client.channels.fetch(guildData.plugins.birthdays) : null;
2023-10-10 20:44:42 +05:00
2024-10-03 11:07:02 +05:00
if (channel) {
const date = new Date();
const currentDay = date.getDate();
const currentMonth = date.getMonth() + 1;
const currentYear = date.getFullYear();
const users = await client.usersData.find({ birthdate: { $gt: 1 } });
2024-01-15 23:19:59 +05:00
2024-07-09 21:56:10 +05:00
for (const user of users) {
2024-10-03 11:07:02 +05:00
// Check if the user is in the guild
if (!guild.members.cache.has(user.id)) continue;
2024-01-15 23:19:59 +05:00
2024-11-05 18:02:08 +05:00
const userDate = user.birthdate > 9999999999 ? new Date(user.birthdate * 1000) : new Date(user.birthdate);
2024-10-03 11:07:02 +05:00
const day = userDate.getDate();
const month = userDate.getMonth() + 1;
const year = userDate.getFullYear();
const age = currentYear - year;
2024-01-15 23:19:59 +05:00
2024-10-03 11:07:02 +05:00
// Check if it's the user's birthday
2024-07-09 21:56:10 +05:00
if (currentMonth === month && currentDay === day) {
const embed = client.embed({
author: client.user.getUsername(),
fields: [
{
name: client.translate("economy/birthdate:HAPPY_BIRTHDAY", null, guildData.language),
value: client.translate("economy/birthdate:HAPPY_BIRTHDAY_MESSAGE", {
user: user.id,
age: `**${age}** ${client.functions.getNoun(
age,
client.translate("misc:NOUNS:AGE:1", null, guildData.language),
client.translate("misc:NOUNS:AGE:2", null, guildData.language),
client.translate("misc:NOUNS:AGE:5", null, guildData.language),
)}`,
}, guildData.language),
},
],
});
2024-10-03 11:07:02 +05:00
await channel.send({ embeds: [embed] }).then(m => m.react("🎉"));
2024-07-09 21:56:10 +05:00
}
2024-01-15 23:19:59 +05:00
}
2024-10-03 11:07:02 +05:00
}
} catch (err) {
if (err.code === 10003) console.log(`No channel found for ${guild.name}`);
else console.error(`Error processing guild "${guild.name}":`, err);
2024-07-09 21:56:10 +05:00
}
2024-01-15 23:19:59 +05:00
}
2024-10-03 11:07:02 +05:00
},
null,
true,
);
cronjob.start();
};
/**
*
* @param {import("../base/Client")} client
*/
module.exports.run = async client => {
2024-10-03 11:07:02 +05:00
for (const guild of client.guilds.cache.values()) {
const guildData = await client.getGuildData(guild.id);
const channel = guildData.plugins.birthdays ? await client.channels.fetch(guildData.plugins.birthdays) : null;
2024-06-18 17:34:47 +05:00
if (channel) {
2024-10-03 11:07:02 +05:00
const date = new Date();
const currentDay = date.getDate();
const currentMonth = date.getMonth() + 1;
const currentYear = date.getFullYear();
const users = await client.usersData.find({ birthdate: { $gt: 1 } });
for (const user of users) {
// Check if the user is in the guild
if (!guild.members.cache.has(user.id)) continue;
const userDate = new Date(user.birthdate * 1000);
const day = userDate.getDate();
const month = userDate.getMonth() + 1;
const year = userDate.getFullYear();
const age = currentYear - year;
// Check if it's the user's birthday
if (currentMonth === month && currentDay === day) {
const embed = client.embed({
author: client.user.getUsername(),
fields: [
{
name: client.translate("economy/birthdate:HAPPY_BIRTHDAY", null, guildData.language),
value: client.translate("economy/birthdate:HAPPY_BIRTHDAY_MESSAGE", {
user: user.id,
age: `**${age}** ${client.functions.getNoun(
age,
client.translate("misc:NOUNS:AGE:1", null, guildData.language),
client.translate("misc:NOUNS:AGE:2", null, guildData.language),
client.translate("misc:NOUNS:AGE:5", null, guildData.language),
)}`,
}, guildData.language),
},
],
});
await channel.send({ embeds: [embed] }).then(m => m.react("🎉"));
}
2024-10-03 11:07:02 +05:00
}
2024-01-15 23:19:59 +05:00
}
2024-10-03 11:07:02 +05:00
}
};