JaBa/helpers/birthdays.js

128 lines
3.9 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 => {
2023-10-15 15:05:10 +05:00
const cronjob = new CronJob("0 5 * * *", async function () {
client.guilds.cache.forEach(async guild => {
const guildData = await client.getGuildData(guild.id);
2024-06-17 17:00:03 +05:00
let channel;
try {
channel = guildData.plugins.birthdays ? client.channels.cache.get(guildData.plugins.birthdays) || (await client.channels.fetch(guildData.plugins.birthdays)) : null;
} catch (e) { /* Nothing */ }
2022-01-04 02:18:28 +05:00
2024-04-30 10:45:07 +05:00
if (guildData.plugins.birthdays && client.channels.cache.get(guildData.plugins.birthdays)) {
2024-04-30 10:50:52 +05:00
const date = new Date(),
2023-10-10 20:44:42 +05:00
currentDay = date.getDate(),
2024-04-10 17:45:25 +05:00
currentMonth = date.getMonth() + 1,
2023-10-10 20:44:42 +05:00
currentYear = date.getFullYear();
2022-01-04 02:18:28 +05:00
if (channel) {
2023-07-05 00:58:06 +05:00
client.usersData.find({ birthdate: { $gt: 1 } }).then(async users => {
for (const user of users) {
2023-10-10 20:44:42 +05:00
if (!guild.members.cache.find(m => m.id === user.id)) return;
const userDate = new Date(user.birthdate * 1000),
2023-10-10 20:44:42 +05:00
day = userDate.getDate(),
2024-04-10 17:45:25 +05:00
month = userDate.getMonth() + 1,
2023-10-10 20:44:42 +05:00
year = userDate.getFullYear(),
age = currentYear - year;
2022-01-04 02:18:28 +05:00
2023-10-10 20:44:42 +05:00
if (currentMonth === month && currentDay === day) {
const embed = client.embed({
author: client.user.getUsername(),
fields: [
2023-10-10 20:44:42 +05:00
{
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),
)}`,
2023-10-10 20:44:42 +05:00
}, guildData.language),
},
],
});
channel.send({
2023-10-10 20:44:42 +05:00
embeds: [embed],
}).then(m => m.react("🎉"));
2022-01-13 00:26:23 +05:00
}
2023-07-05 00:58:06 +05:00
}
});
2022-01-13 00:26:23 +05:00
}
}
2022-01-04 02:18:28 +05:00
});
2023-07-05 00:58:06 +05:00
},
null,
true,
);
2023-10-15 15:05:10 +05:00
cronjob.start();
2023-07-05 00:58:06 +05:00
};
2024-01-15 23:19:59 +05:00
2024-04-30 10:50:52 +05:00
/**
*
* @param {import("../base/Client")} client
*/
module.exports.run = async client => {
2024-01-15 23:19:59 +05:00
client.guilds.cache.forEach(async guild => {
const guildData = await client.getGuildData(guild.id);
2024-06-17 17:00:03 +05:00
let channel;
try {
channel = guildData.plugins.birthdays ? client.channels.cache.get(guildData.plugins.birthdays) || (await client.channels.fetch(guildData.plugins.birthdays)) : null;
} catch (e) { /* Nothing */ }
2024-01-15 23:19:59 +05:00
2024-04-30 10:50:52 +05:00
if (guildData.plugins.birthdays) {
const date = new Date(),
2024-01-15 23:19:59 +05:00
currentDay = date.getDate(),
2024-04-10 17:45:25 +05:00
currentMonth = date.getMonth() + 1,
2024-01-15 23:19:59 +05:00
currentYear = date.getFullYear();
if (channel) {
client.usersData.find({ birthdate: { $gt: 1 } }).then(async users => {
for (const user of users) {
if (!guild.members.cache.find(m => m.id === user.id)) return;
2024-04-10 17:45:25 +05:00
const userDate = new Date(user.birthdate * 1000),
2024-01-15 23:19:59 +05:00
day = userDate.getDate(),
2024-04-10 17:45:25 +05:00
month = userDate.getMonth() + 1,
2024-01-15 23:19:59 +05:00
year = userDate.getFullYear(),
age = currentYear - year;
if (currentMonth === month && currentDay === day) {
const embed = client.embed({
author: client.user.getUsername(),
fields: [
2024-01-15 23:19:59 +05:00
{
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),
)}`,
2024-01-15 23:19:59 +05:00
}, guildData.language),
},
],
});
2024-01-15 23:19:59 +05:00
channel.send({
2024-01-15 23:19:59 +05:00
embeds: [embed],
}).then(m => m.react("🎉"));
2024-01-15 23:19:59 +05:00
}
}
});
}
}
});
2024-01-23 16:11:10 +05:00
};