JaBa/helpers/birthdays.js

52 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-01-04 02:18:28 +05:00
const CronJob = require("cron").CronJob,
Discord = require("discord.js");
async function init(client) {
new CronJob("0 8 * * *", async function () {
client.guilds.cache.forEach(async (guild) => {
const date = new Date();
const currentDay = date.getDate();
const currentMonth = date.getMonth();
const currentYear = date.getFullYear();
const guildData = await client.findOrCreateGuild({
id: guild.id
});
if (guildData.plugins.birthdays) {
const channel = client.channels.cache.get(guildData.plugins.birthdays);
if (channel) {
client.usersData
.find({ birthdate: { $gt: 1 } })
.then(async (users) => {
for (const user of users) {
const userDate = new Date(user.birthdate);
const day = userDate.getDate();
const month = userDate.getMonth();
const year = userDate.getFullYear();
const age = currentYear - year;
if (currentMonth === month && currentDay === day) {
const embed = new Discord.MessageEmbed()
.setAuthor({ name: client.user.username, iconURL: client.user.displayAvatarURL({
2022-01-04 02:18:28 +05:00
size: 512,
dynamic: true,
format: "png"
})})
2022-01-04 02:18:28 +05:00
.setColor(client.config.embed.color)
.setFooter({ text: client.config.embed.footer })
2022-01-04 02:18:28 +05:00
.addField(client.translate("economy/birthdate:HAPPY_BIRTHDAY"), client.translate("economy/birthdate:HAPPY_BIRTHDAY_MESSAGE", {
user: user.id,
2022-01-13 00:26:23 +05:00
age: `${age} ${client.getNoun(age, client.translate("misc:NOUNS:AGE:1"), client.translate("misc:NOUNS:AGE:2"), client.translate("misc:NOUNS:AGE:5"))}`
2022-01-04 02:18:28 +05:00
}));
const msg = await channel.send({ content: "@here", embeds: [embed] });
2022-01-04 02:18:28 +05:00
await msg.react("🎉");
2022-01-13 00:26:23 +05:00
}
}
2022-01-04 02:18:28 +05:00
});
2022-01-13 00:26:23 +05:00
}
}
2022-01-04 02:18:28 +05:00
});
}, null, true, "Europe/Moscow");
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
module.exports = { init };