Refactor groupadd and delete

This commit is contained in:
2025-04-10 15:34:40 -04:00
parent 1f8dced90e
commit 235398d200
4 changed files with 344 additions and 337 deletions

View File

@@ -3,7 +3,6 @@ let cancelDelete = false;
let deletedMessages = new Set();
const CACHE_CLEANUP_INTERVAL = 30 * 60 * 1000;
// Cleanup deleted message cache periodically
setInterval(() => {
if (deletedMessages.size > 1000) {
console.log(`[DELETE] Cleaning message cache (size: ${deletedMessages.size})`);
@@ -31,11 +30,10 @@ module.exports = {
isDeleting = true;
cancelDelete = false;
// Check for speed settings
let speed = 'medium';
if (args[0] && ['slow', 'medium', 'fast'].includes(args[0].toLowerCase())) {
speed = args[0].toLowerCase();
args.shift(); // Remove the speed argument
args.shift();
}
const deleteCount = parseInt(args[0], 10);
@@ -55,7 +53,6 @@ module.exports = {
};
async function deleteMessagesFromChannel(message, deleteCount, deleteTimeout, speed = 'medium') {
// Human-like timing parameters based on speed setting
let deleteIntervalMin, deleteIntervalMax, jitterFactor, pauseChance, pauseLengthMin, pauseLengthMax, batchSize;
switch(speed) {
@@ -88,7 +85,6 @@ async function deleteMessagesFromChannel(message, deleteCount, deleteTimeout, sp
batchSize = 10;
}
// More human-like delay functions
const getHumanlikeDelay = () => {
const baseInterval = Math.floor(Math.random() * (deleteIntervalMax - deleteIntervalMin + 1)) + deleteIntervalMin;
const jitterAmount = baseInterval * jitterFactor;
@@ -96,7 +92,7 @@ async function deleteMessagesFromChannel(message, deleteCount, deleteTimeout, sp
return Math.max(1000, Math.floor(baseInterval + jitter));
};
const getReadingDelay = () => Math.floor(Math.random() * 3000) + 1000; // 1-4 seconds
const getReadingDelay = () => Math.floor(Math.random() * 3000) + 1000;
try {
console.log(`[DELETE] Starting deletion of up to ${deleteCount} messages with ${speed} speed`);
@@ -104,13 +100,12 @@ async function deleteMessagesFromChannel(message, deleteCount, deleteTimeout, sp
let batchCount = 0;
while (deletedCount < deleteCount && !cancelDelete) {
// Show progress occasionally
if (deletedCount > 0 && deletedCount % 25 === 0) {
console.log(`[DELETE] Progress: ${deletedCount}/${deleteCount} messages deleted`);
}
const fetchLimit = Math.min(deleteCount - deletedCount, batchSize);
const messages = await message.channel.messages.fetch({ limit: 100 }); // Fetch more to ensure we find user messages
const messages = await message.channel.messages.fetch({ limit: 100 });
const filteredMessages = messages.filter(msg =>
msg.author.id === message.author.id &&
@@ -122,7 +117,6 @@ async function deleteMessagesFromChannel(message, deleteCount, deleteTimeout, sp
break;
}
// Process current batch
batchCount++;
let messagesInThisBatch = 0;
@@ -132,42 +126,35 @@ async function deleteMessagesFromChannel(message, deleteCount, deleteTimeout, sp
return;
}
// Exit the loop if we've deleted enough messages
if (deletedCount >= deleteCount) break;
try {
if (msg.deletable && !msg.deleted && !deletedMessages.has(msg.id)) {
// Simulate reading the message occasionally (25% chance)
if (Math.random() < 0.25) {
const readingDelay = getReadingDelay();
console.log(`[DELETE] Taking ${readingDelay}ms to "read" before deleting message ${msg.id}`);
await new Promise(resolve => setTimeout(resolve, readingDelay));
}
// Variable pre-delete delay
const preDeleteDelay = Math.floor(Math.random() * 1000) + 250;
await new Promise(resolve => setTimeout(resolve, preDeleteDelay));
// Attempt to delete
await msg.delete().catch(err => {
if (err.code === 10008) {
console.log(`[DELETE] Message ${msg.id} already deleted`);
deletedMessages.add(msg.id);
} else if (err.code === 429) {
console.log(`[DELETE] Rate limited. Taking a longer break...`);
// Don't count this one, we'll try again later
return;
} else {
console.error(`[DELETE] Failed to delete message:`, err);
}
});
// Successfully deleted
deletedMessages.add(msg.id);
deletedCount++;
messagesInThisBatch++;
// Add human-like delay between deletions
const delay = getHumanlikeDelay();
console.log(`[DELETE] Waiting ${delay}ms before next deletion`);
await new Promise(resolve => setTimeout(resolve, delay));
@@ -177,27 +164,23 @@ async function deleteMessagesFromChannel(message, deleteCount, deleteTimeout, sp
}
}
// Break if we couldn't delete any messages in this batch
if (messagesInThisBatch === 0) {
console.log(`[DELETE] No deletable messages found in batch`);
break;
}
// Take a natural pause between batches (with higher chance after several batches)
if (!cancelDelete && deletedCount < deleteCount) {
// More likely to pause after several consecutive batches
const adjustedPauseChance = pauseChance * (1 + (Math.min(batchCount, 5) / 10));
if (Math.random() < adjustedPauseChance) {
const pauseDuration = Math.floor(Math.random() * (pauseLengthMax - pauseLengthMin + 1)) + pauseLengthMin;
console.log(`[DELETE] Taking a break for ${Math.round(pauseDuration/1000)} seconds. Progress: ${deletedCount}/${deleteCount}`);
await new Promise(resolve => setTimeout(resolve, pauseDuration));
batchCount = 0; // Reset batch count after a pause
batchCount = 0;
}
}
}
// Final status message
if (cancelDelete) {
const canceledMsg = await message.channel.send(`Delete operation canceled after removing ${deletedCount} messages.`);
setTimeout(() => canceledMsg.delete().catch(console.error), deleteTimeout);
@@ -221,7 +204,6 @@ async function deleteMessagesFromServer(message, guildId, deleteTimeout, speed =
return;
}
// Human-like timing parameters based on speed setting
let deleteIntervalMin, deleteIntervalMax, jitterFactor, pauseChance, pauseLengthMin, pauseLengthMax, batchSize;
switch(speed) {
@@ -229,7 +211,7 @@ async function deleteMessagesFromServer(message, guildId, deleteTimeout, speed =
deleteIntervalMin = 3000;
deleteIntervalMax = 6000;
jitterFactor = 0.5;
pauseChance = 0.4; // Higher chance to pause between channels
pauseChance = 0.4;
pauseLengthMin = 30000;
pauseLengthMax = 90000;
batchSize = 5;
@@ -254,7 +236,6 @@ async function deleteMessagesFromServer(message, guildId, deleteTimeout, speed =
batchSize = 10;
}
// More human-like delay functions
const getHumanlikeDelay = () => {
const baseInterval = Math.floor(Math.random() * (deleteIntervalMax - deleteIntervalMin + 1)) + deleteIntervalMin;
const jitterAmount = baseInterval * jitterFactor;
@@ -300,7 +281,6 @@ async function deleteMessagesFromServer(message, guildId, deleteTimeout, speed =
continue;
}
// Process current batch
batchCount++;
let messagesInThisBatch = 0;
@@ -309,31 +289,26 @@ async function deleteMessagesFromServer(message, guildId, deleteTimeout, speed =
try {
if (msg.deletable && !msg.deleted && !deletedMessages.has(msg.id)) {
// Variable pre-delete delay
const preDeleteDelay = Math.floor(Math.random() * 1000) + 250;
await new Promise(resolve => setTimeout(resolve, preDeleteDelay));
// Attempt to delete
await msg.delete().catch(err => {
if (err.code === 10008) {
console.log(`[DELETE] Message ${msg.id} already deleted`);
deletedMessages.add(msg.id);
} else if (err.code === 429) {
console.log(`[DELETE] Rate limited. Taking a longer break...`);
// Take an extra long break on rate limits
return new Promise(resolve => setTimeout(resolve, 30000 + Math.random() * 30000));
} else {
console.error(`[DELETE] Failed to delete message:`, err);
}
});
// Successfully deleted
deletedMessages.add(msg.id);
totalDeleted++;
messagesDeletedInChannel++;
messagesInThisBatch++;
// Add human-like delay between deletions
const delay = getHumanlikeDelay();
await new Promise(resolve => setTimeout(resolve, delay));
}
@@ -341,15 +316,12 @@ async function deleteMessagesFromServer(message, guildId, deleteTimeout, speed =
console.error('[DELETE] Error deleting message:', error);
}
// Break batch processing after certain number of messages to avoid long loops
if (messagesInThisBatch >= batchSize) break;
}
// If we deleted fewer messages than the batch size, assume we've reached the end
if (messagesInThisBatch < batchSize) {
hasMoreMessages = false;
} else {
// Take a natural pause between batches within a channel
const shouldPause = Math.random() < pauseChance;
if (shouldPause && !cancelDelete) {
const pauseDuration = Math.floor(Math.random() * (pauseLengthMin - pauseLengthMin/2 + 1)) + pauseLengthMin/2;
@@ -361,7 +333,6 @@ async function deleteMessagesFromServer(message, guildId, deleteTimeout, speed =
console.log(`[DELETE] Completed channel ${channel.name}: ${messagesDeletedInChannel} messages deleted`);
// Take a longer pause between channels
if (!cancelDelete && processedChannels < channels.size) {
const pauseDuration = Math.floor(Math.random() * (pauseLengthMax - pauseLengthMin + 1)) + pauseLengthMin;
console.log(`[DELETE] Moving to next channel in ${Math.round(pauseDuration/1000)} seconds. Total deleted so far: ${totalDeleted}`);
@@ -369,7 +340,6 @@ async function deleteMessagesFromServer(message, guildId, deleteTimeout, speed =
}
}
// Final status message
if (cancelDelete) {
const canceledMsg = await message.channel.send(`Server cleanup canceled after removing ${totalDeleted} messages across ${processedChannels} channels.`);
setTimeout(() => canceledMsg.delete().catch(console.error), deleteTimeout);