JaBa/helpers/checkReminds.js

118 lines
2.9 KiB
JavaScript
Raw Normal View History

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 = 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) {
2024-02-17 21:34:26 +05:00
const dateNow = Math.floor(Date.now() / 1000),
2023-10-10 20:44:42 +05:00
reminds = user.reminds,
mustSent = reminds.filter(r => r.sendAt < dateNow);
if (mustSent.length > 0) {
mustSent.forEach(r => {
const embed = client.embed({
author: client.translate("general/remindme:EMBED_TITLE"),
fields: [
2024-01-27 16:31:34 +05:00
{
name: client.translate("general/remindme:EMBED_CREATED"),
2024-02-09 23:26:57 +05:00
value: `<t:${r.createdAt}:f>`,
2024-01-27 16:31:34 +05:00
inline: true,
},
{
name: client.translate("general/remindme:EMBED_TIME"),
2024-02-09 23:26:57 +05:00
value: `<t:${r.sendAt}:f>`,
2024-01-27 16:31:34 +05:00
inline: true,
},
{
name: client.translate("common:MESSAGE"),
value: r.message,
},
],
});
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-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
};
2024-08-10 21:49:18 +05:00
/**
*
* @param {import("../base/Client")} client
*/
module.exports.run = function (client) {
client.usersData.find({ reminds: { $gt: [] } }).then(users => {
for (const user of users) {
if (!client.users.cache.has(user.id)) client.users.fetch(user.id);
client.databaseCache.usersReminds.set(user.id, user);
}
});
client.databaseCache.usersReminds.forEach(async user => {
const cachedUser = client.users.cache.get(user.id);
if (cachedUser) {
const dateNow = Math.floor(Date.now() / 1000),
reminds = user.reminds,
mustSent = reminds.filter(r => r.sendAt < dateNow);
if (mustSent.length > 0) {
mustSent.forEach(r => {
const embed = client.embed({
author: client.translate("general/remindme:EMBED_TITLE"),
fields: [
{
name: client.translate("general/remindme:EMBED_CREATED"),
value: `<t:${r.createdAt}:f>`,
inline: true,
},
{
name: client.translate("general/remindme:EMBED_TIME"),
value: `<t:${r.sendAt}:f>`,
inline: true,
},
{
name: client.translate("common:MESSAGE"),
value: r.message,
},
],
});
cachedUser.send({
embeds: [embed],
});
});
user.reminds = user.reminds.filter(r => r.sendAt >= dateNow);
await user.save();
if (user.reminds.length === 0) client.databaseCache.usersReminds.delete(user.id);
}
}
});
};