Small fix
This commit is contained in:
+36
-24
@@ -1,5 +1,5 @@
|
|||||||
const { Events } = require('discord.js');
|
const {Events} = require('discord.js');
|
||||||
const { connectToDatabase } = require('../db.js');
|
const {connectToDatabase} = require('../db.js');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: Events.ClientReady,
|
name: Events.ClientReady,
|
||||||
@@ -7,38 +7,50 @@ module.exports = {
|
|||||||
const dbClient = await connectToDatabase();
|
const dbClient = await connectToDatabase();
|
||||||
const db = dbClient.db('discord');
|
const db = dbClient.db('discord');
|
||||||
const tempBanCollection = db.collection('tempban');
|
const tempBanCollection = db.collection('tempban');
|
||||||
|
const scheduled = new Map();
|
||||||
// 2) Define a function to schedule unbans
|
async function unbanNow(banData) {
|
||||||
async function scheduleUnban(banData) {
|
|
||||||
const delay = banData.banTime - Date.now();
|
|
||||||
|
|
||||||
if (delay <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setTimeout(async () => {
|
|
||||||
try {
|
try {
|
||||||
const guild = await client.guilds.fetch(banData.guildId);
|
const guild = await client.guilds.fetch(banData.guildId);
|
||||||
await guild.bans.remove(banData.userId);
|
await guild.bans.remove(banData.userId);
|
||||||
|
await tempBanCollection.deleteOne({_id: banData._id});
|
||||||
await tempBanCollection.deleteOne({ _id: banData._id });
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error unbanning member:', err);
|
console.error('Error unbanning member:', err);
|
||||||
|
} finally {
|
||||||
|
scheduled.delete(String(banData._id));
|
||||||
}
|
}
|
||||||
}, delay);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function scheduleUnban(banData) {
|
||||||
|
const id = String(banData._id);
|
||||||
|
if (scheduled.has(id)) return;
|
||||||
|
|
||||||
|
const delay = banData.banTime - Date.now(); // assumes banTime is a future timestamp (ms)
|
||||||
|
if (delay <= 0) {
|
||||||
|
// already expired -> unban immediately
|
||||||
|
void unbanNow(banData);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(() => void unbanNow(banData), delay);
|
||||||
|
scheduled.set(id, timeoutId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function scanAndSchedule() {
|
||||||
try {
|
try {
|
||||||
const allBans = await tempBanCollection.find({}).toArray();
|
const now = Date.now();
|
||||||
allBans.forEach(scheduleUnban);
|
const futureBans = await tempBanCollection.find({banTime: {$gt: now}}).toArray();
|
||||||
|
futureBans.forEach(scheduleUnban);
|
||||||
tempBanCollection.watch().on('change', (change) => {
|
const expired = await tempBanCollection.find({banTime: {$lte: now}}).toArray();
|
||||||
if (change.operationType === 'insert') {
|
expired.forEach((b) => void unbanNow(b));
|
||||||
const newBan = change.fullDocument;
|
|
||||||
scheduleUnban(newBan);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error setting up unban system:', err);
|
console.error('Error scanning tempbans:', err);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial load
|
||||||
|
await scanAndSchedule();
|
||||||
|
|
||||||
|
// Poll every 10 seconds for newly inserted bans
|
||||||
|
setInterval(scanAndSchedule, 10_000);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user