This part of the code should perform a cooldown, but it only works fine on the first two calls, and after that it simply does not find newer entries
history
.findOne({ sender: ctx.message.from.id, type: true })
.sort([["date", -1]])
.exec(async function (err, data) {
if (!data) {
let createLog = new history({
type: true,
title: `${Math.floor(Math.random() * (1 - 10 + 1) + 1)}`,
sender: ctx.message.from.id,
});
await createLog.save();
} else {
let dateOMG = new Date() - (await data.date);
let diffMins = Math.round(((dateOMG % 86400000) % 3600000) / 60000);
let diffHrs = Math.floor((dateOMG % 86400000) / 3600000);
let diffDays = Math.floor(dateOMG / 86400000);
if (diffMins >= 1 || diffHrs > 0 || diffDays > 0) {
console.log(data);
console.log(diffMins, diffHrs, diffDays);
console.log(new Date(), data.date);
let createLog = new history({
type: true,
title: `${Math.floor(Math.random() * (1 - 10 + 1) + 1)}`,
sender: ctx.message.from.id,
});
await createLog.save();
} else {
await ctx.reply(
"cooldown"
);
}
}
});
In console:
The first time the function is executed
{
_id: new ObjectId("61ec63530e6653456df4db58"),
type: true,
date: 2022-01-22T20:04:27.414Z,
title: '-3',
sender: 'omg',
__v: 0
}
33 1 0
2022-01-22T21:37:34.360Z 2022-01-22T20:04:27.414Z
Next and all after it, it just ignores new entries
{
_id: new ObjectId("61ec791ee1b0c54e66fd24d4"),
type: true,
date: 2022-01-22T21:37:29.147Z,
title: '-7',
sender: 'lol',
__v: 0
}
1 0 0
2022-01-22T21:38:44.429Z 2022-01-22T21:37:29.147Z