JaBa/helpers/cleanup.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-10-09 14:26:22 +05:00
// 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);
}
2022-08-30 14:26:56 +05:00
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
2022-08-30 14:26:56 +05:00
*/
module.exports.init = async function (client) {
2022-10-09 14:26:22 +05:00
setDaysTimeout(async () => {
2023-07-05 00:58:06 +05:00
const timestamp = Date.now() + 29 * 24 * 60 * 60 * 1000; // 29 days
const members = client.membersData.find({ transactions: { $gt: [] } });
2022-07-02 01:17:00 +05:00
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);
2023-07-03 19:30:47 +05:00
2023-10-30 21:45:10 +05:00
member.markModified("transactions");
await member.save();
2022-07-02 01:17:00 +05:00
}
}
}
2022-10-09 14:26:22 +05:00
}, 14);
2022-08-30 14:26:56 +05:00
2022-10-09 14:26:22 +05:00
setDaysTimeout(async () => {
2022-08-30 14:26:56 +05:00
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}`);
2023-10-21 21:15:25 +05:00
2023-10-30 21:45:10 +05:00
client.usersData.markModified();
2023-10-21 21:15:25 +05:00
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}`);
2023-10-30 21:45:10 +05:00
client.membersData.markModified();
2023-10-21 21:15:25 +05:00
client.membersData.save();
2022-08-30 14:26:56 +05:00
}
});
}
});
2022-10-09 14:26:22 +05:00
}, 30);
2023-07-05 00:58:06 +05:00
};