From c22dd92a3567df50b8f198a91bd7e44049b21679 Mon Sep 17 00:00:00 2001 From: "Jonny_Bro (Nikita)" Date: Sat, 10 Aug 2024 21:49:18 +0500 Subject: [PATCH] add test function to reminds --- helpers/checkReminds.js | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/helpers/checkReminds.js b/helpers/checkReminds.js index 66c92f53..b7e0adaa 100644 --- a/helpers/checkReminds.js +++ b/helpers/checkReminds.js @@ -57,3 +57,61 @@ module.exports.init = function (client) { }); }, 1000); }; + +/** + * + * @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: ``, + inline: true, + }, + { + name: client.translate("general/remindme:EMBED_TIME"), + value: ``, + 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); + } + } + }); +};