fix: many times sending reminds

This commit is contained in:
Slincnik 2025-01-14 22:20:22 +03:00
parent ac63acd993
commit 4c8c068245
No known key found for this signature in database

View file

@ -8,16 +8,18 @@ export const data: CronTaskData = {
task: async () => { task: async () => {
const client = useClient(); const client = useClient();
client.adapter.find(UserModel, { reminds: { $gt: [] } }).then(users => { const users = await client.adapter.find(UserModel, { reminds: { $gt: [] } });
for (const user of users) { for (const user of users) {
if (!client.users.cache.has(user.id)) client.users.fetch(user.id); if (!client.users.cache.has(user.id)) {
client.users.fetch(user.id);
}
client.cacheReminds.set(user.id, { client.cacheReminds.set(user.id, {
id: user.id, id: user.id,
reminds: user.reminds, reminds: user.reminds,
}); });
} }
});
client.cacheReminds.forEach(async ({ id, reminds }) => { client.cacheReminds.forEach(async ({ id, reminds }) => {
const cachedUser = client.users.cache.get(id); const cachedUser = client.users.cache.get(id);
@ -28,7 +30,7 @@ export const data: CronTaskData = {
if (!mustSent.length) return; if (!mustSent.length) return;
mustSent.forEach(r => { mustSent.forEach(async r => {
const embed = createEmbed({ const embed = createEmbed({
author: { author: {
name: client.translate("general/remindme:EMBED_TITLE"), name: client.translate("general/remindme:EMBED_TITLE"),
@ -51,12 +53,11 @@ export const data: CronTaskData = {
], ],
}); });
cachedUser await cachedUser.send({
.send({
embeds: [embed], embeds: [embed],
}) });
.then(() => {
client.adapter.updateOne( await client.adapter.updateOne(
UserModel, UserModel,
{ id }, { id },
{ {
@ -68,11 +69,14 @@ export const data: CronTaskData = {
}, },
); );
}); });
});
reminds = reminds.filter(r => r.sendAt >= Math.floor(Date.now() / 1000)); const updatedReminds = reminds.filter(r => r.sendAt >= Math.floor(Date.now() / 1000));
if (!reminds.length) client.cacheReminds.delete(id); if (!updatedReminds.length) {
client.cacheReminds.delete(id);
} else {
client.cacheReminds.set(id, { id, reminds: updatedReminds });
}
}); });
}, },
schedule: "* * * * * *", schedule: "* * * * * *",