JaBa/helpers/checkReminds.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

const { EmbedBuilder } = require("discord.js");
const moment = require("moment");
2022-01-04 02:18:28 +05:00
2022-08-30 14:26:56 +05:00
/**
*
* @param {import("../base/JaBa")} client
*/
module.exports.init = function (client) {
2023-07-05 00:58:06 +05:00
client.usersData.find({ reminds: { $gt: [] } }).then(users => {
for (const user of users) {
if (!client.users.cache.has(user.id)) client.users.fetch(user.id);
2023-10-10 20:44:42 +05:00
2023-07-05 00:58:06 +05:00
client.databaseCache.usersReminds.set(user.id, user);
}
});
setInterval(async function () {
client.databaseCache.usersReminds.forEach(async user => {
2023-10-10 20:44:42 +05:00
const cachedUser = client.users.cache.get(user.id);
2023-10-10 20:44:42 +05:00
if (cachedUser) {
const dateNow = Date.now(),
reminds = user.reminds,
mustSent = reminds.filter(r => r.sendAt < dateNow);
if (mustSent.length > 0) {
mustSent.forEach(r => {
const embed = new EmbedBuilder()
.setAuthor({
name: client.translate("general/remindme:TITLE"),
})
2023-07-05 00:58:06 +05:00
.setDescription(
client.translate("general/remindme:CREATED", {
2023-10-17 16:59:47 +05:00
time: moment(r.createdAt).locale(client.defaultLanguage).format("dddd, Do MMMM YYYY, HH:mm:ss"),
2023-07-05 00:58:06 +05:00
}),
)
.addFields([
{
name: client.translate("common:MESSAGE"),
value: r.message,
},
])
.setColor(client.config.embed.color)
.setFooter(client.config.embed.footer);
2023-10-10 20:44:42 +05:00
cachedUser.send({
embeds: [embed],
2022-01-04 02:18:28 +05:00
});
});
user.reminds = user.reminds.filter(r => r.sendAt >= dateNow);
2023-07-03 19:30:47 +05:00
user.markModified("reminds");
2023-07-05 00:58:06 +05:00
await user.save();
if (user.reminds.length === 0) client.databaseCache.usersReminds.delete(user.id);
2022-01-13 00:26:23 +05:00
}
}
});
}, 1000);
2023-07-05 00:58:06 +05:00
};