JaBa/helpers/birthdays.js

66 lines
2 KiB
JavaScript
Raw Normal View History

const { CronJob } = require("cron"),
{ EmbedBuilder } = require("discord.js");
2022-01-04 02:18:28 +05:00
2022-08-30 14:26:56 +05:00
/**
*
* @param {import("../base/JaBa")} client
*/
module.exports.init = async function (client) {
new CronJob("0 5 * * *", async function () {
const date = new Date(),
currentDay = date.getDate(),
currentMonth = date.getMonth(),
currentYear = date.getFullYear();
client.guilds.cache.forEach(async guild => {
2023-07-05 00:58:06 +05:00
const guildData = await client.findOrCreateGuild({ id: guild.id });
2022-01-04 02:18:28 +05:00
if (guildData.plugins.birthdays) {
const channel = client.channels.cache.get(guildData.plugins.birthdays);
if (channel) {
2023-07-05 00:58:06 +05:00
client.usersData.find({ birthdate: { $gt: 1 } }).then(async users => {
for (const user of users) {
if (guild.members.cache.find(m => m.id === user.id)) {
const userDate = new Date(user.birthdate);
const day = userDate.getDate();
const month = userDate.getMonth();
const year = userDate.getFullYear();
const age = currentYear - year;
2022-01-04 02:18:28 +05:00
2023-07-05 00:58:06 +05:00
if (currentMonth === month && currentDay === day) {
const embed = new EmbedBuilder()
.setAuthor({
2023-07-07 21:50:36 +05:00
name: client.user.getUsername(),
2023-07-05 00:58:06 +05:00
iconURL: client.user.displayAvatarURL(),
})
.setColor(client.config.embed.color)
.setFooter(client.config.embed.footer)
2023-07-05 00:58:06 +05:00
.addFields([
{
name: client.translate("economy/birthdate:HAPPY_BIRTHDAY"),
value: client.translate("economy/birthdate:HAPPY_BIRTHDAY_MESSAGE", {
name: user.username,
user: user.id,
age: `**${age}** ${client.functions.getNoun(age, client.translate("misc:NOUNS:AGE:1"), client.translate("misc:NOUNS:AGE:2"), client.translate("misc:NOUNS:AGE:5"))}`,
}),
2023-07-05 00:58:06 +05:00
},
]);
2023-07-05 00:58:06 +05:00
const msg = await channel.send({
embeds: [embed],
});
await msg.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,
"Europe/Moscow",
);
};