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 () => {
const client = useClient();
client.adapter.find(UserModel, { reminds: { $gt: [] } }).then(users => {
for (const user of users) {
if (!client.users.cache.has(user.id)) client.users.fetch(user.id);
const users = await client.adapter.find(UserModel, { reminds: { $gt: [] } });
client.cacheReminds.set(user.id, {
id: user.id,
reminds: user.reminds,
});
for (const user of users) {
if (!client.users.cache.has(user.id)) {
client.users.fetch(user.id);
}
});
client.cacheReminds.set(user.id, {
id: user.id,
reminds: user.reminds,
});
}
client.cacheReminds.forEach(async ({ id, reminds }) => {
const cachedUser = client.users.cache.get(id);
@ -28,7 +30,7 @@ export const data: CronTaskData = {
if (!mustSent.length) return;
mustSent.forEach(r => {
mustSent.forEach(async r => {
const embed = createEmbed({
author: {
name: client.translate("general/remindme:EMBED_TITLE"),
@ -51,28 +53,30 @@ export const data: CronTaskData = {
],
});
cachedUser
.send({
embeds: [embed],
})
.then(() => {
client.adapter.updateOne(
UserModel,
{ id },
{
$pull: {
reminds: {
sendAt: r.sendAt,
},
},
await cachedUser.send({
embeds: [embed],
});
await client.adapter.updateOne(
UserModel,
{ id },
{
$pull: {
reminds: {
sendAt: r.sendAt,
},
);
});
},
},
);
});
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: "* * * * * *",