JaBa/helpers/birthdays.js

66 lines
2.2 KiB
JavaScript
Raw Permalink 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
*/
2024-11-18 19:19:24 +05:00
async function checkBirthdays(client) {
for (const guild of client.guilds.cache.values()) {
try {
const guildData = await client.getGuildData(guild.id);
const channel = guildData.plugins.birthdays ? await client.channels.fetch(guildData.plugins.birthdays) : null;
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 } });
for (const user of users) {
if (!guild.members.cache.has(user.id)) continue;
2024-11-20 12:01:41 +05:00
const userDate = new Date(user.birthdate).getFullYear() <= 1970 ? new Date(user.birthdate * 1000) : new Date(user.birthdate);
2024-11-18 19:19:24 +05:00
const day = userDate.getDate();
const month = userDate.getMonth() + 1;
const year = userDate.getFullYear();
const age = currentYear - year;
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",
2024-07-09 21:56:10 +05:00
{
2024-11-18 19:19:24 +05:00
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),
)}`,
2024-07-09 21:56:10 +05:00
},
2024-11-18 19:19:24 +05:00
guildData.language,
),
},
],
});
2024-07-09 21:56:10 +05:00
2024-11-18 19:19:24 +05:00
await channel.send({ embeds: [embed] }).then(m => m.react("🎉"));
2024-10-03 11:07:02 +05:00
}
}
2024-10-03 11:07:02 +05:00
}
2024-11-18 19:19:24 +05:00
} catch (e) {
if (e.code === 10003) console.log(`No channel found for ${guild.name}`);
else console.error(`Error processing birthdays for guild "${guild.name}":`, e);
2024-01-15 23:19:59 +05:00
}
2024-10-03 11:07:02 +05:00
}
2024-11-18 19:19:24 +05:00
}
module.exports.init = async client => new CronJob("0 5 * * *", checkBirthdays(client), null, true, "Europe/Moscow");
module.exports.run = async client => await checkBirthdays(client);