mirror of
https://github.com/JonnyBro/JaBa.git
synced 2024-11-25 22:55:00 +05:00
71 lines
1.9 KiB
JavaScript
71 lines
1.9 KiB
JavaScript
// Thanks Stackoverflow <3
|
|
function setDaysTimeout(callback, days) {
|
|
// 86400 seconds in a day
|
|
const msInDay = 86400 * 1000;
|
|
|
|
let dayCount = 0;
|
|
const timer = setInterval(function () {
|
|
dayCount++; // a day has passed
|
|
|
|
if (dayCount === days) {
|
|
clearInterval(timer);
|
|
callback.apply(this, []);
|
|
}
|
|
}, msInDay);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {import("../base/Client")} client
|
|
*/
|
|
module.exports.init = async function (client) {
|
|
setDaysTimeout(async () => {
|
|
const timestamp = Date.now() + 29 * 24 * 60 * 60 * 1000; // 29 days
|
|
const members = client.membersData.find({ transactions: { $gt: [] } });
|
|
|
|
for (const member of members) {
|
|
const transactions = member.transactions;
|
|
for await (const transaction of transactions) {
|
|
if (transaction.date < timestamp) {
|
|
const index = transactions.indexOf(transaction);
|
|
transactions.splice(index, 1);
|
|
|
|
member.markModified("transactions");
|
|
await member.save();
|
|
}
|
|
}
|
|
}
|
|
}, 14);
|
|
|
|
setDaysTimeout(async () => {
|
|
client.usersData.find({}, function (err, res) {
|
|
for (const user of res) {
|
|
client.users.fetch(user.id).then(u => {
|
|
if (u.username.match(/.*Deleted User.* [A-z0-9]+/g)) {
|
|
client.databaseCache.users.delete(u.id);
|
|
client.usersData.deleteOne({ id: u.id });
|
|
client.logger.log(`Removed from database deleted user - ID: ${u.id} Username: ${u.username}`);
|
|
|
|
client.usersData.markModified();
|
|
client.usersData.save();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
client.membersData.find({}, function (err, res) {
|
|
for (const user of res) {
|
|
client.users.fetch(user.id).then(u => {
|
|
if (u.username.match(/.*Deleted User.* [A-z0-9]+/g)) {
|
|
client.databaseCache.members.delete(u.id);
|
|
client.membersData.deleteOne({ id: u.id });
|
|
client.logger.log(`Removed from database deleted user - ID: ${u.id} Username: ${u.username}`);
|
|
|
|
client.membersData.markModified();
|
|
client.membersData.save();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}, 30);
|
|
};
|