From 717e04478c55dbad461891f73e4440ba9d90c72e Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sun, 8 Sep 2024 10:35:46 -0400 Subject: [PATCH] Update delete.js --- commands/delete.js | 55 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/commands/delete.js b/commands/delete.js index e90504b..2378e07 100644 --- a/commands/delete.js +++ b/commands/delete.js @@ -1,19 +1,56 @@ module.exports = { name: 'delete', - description: 'Delete a specified number of your messages.', + description: 'Delete a specified number of your messages with human-like delays.', async execute(message, args, deleteTimeout) { const deleteCount = parseInt(args[0], 10); - if (isNaN(deleteCount)) { - const errorMsg = await message.channel.send('Please provide the number of messages to delete.'); + if (isNaN(deleteCount) || deleteCount <= 0) { + const errorMsg = await message.channel.send('Please provide a valid number of messages to delete.'); setTimeout(() => errorMsg.delete().catch(console.error), deleteTimeout); return; } - const messages = await message.channel.messages.fetch({ limit: deleteCount + 1 }); - const filtered = messages.filter(msg => msg.author.id === message.author.id); - filtered.forEach(msg => { - msg.delete().catch(console.error); - }); + const getRandomDelay = () => Math.floor(Math.random() * (3000 - 1000 + 1)) + 1000; + + const getBatchDelay = () => Math.floor(Math.random() * (10000 - 5000 + 1)) + 5000; + + const BATCH_SIZE = 10; + + try { + let remainingMessages = deleteCount; + + while (remainingMessages > 0) { + const fetchLimit = Math.min(remainingMessages, BATCH_SIZE); + const messages = await message.channel.messages.fetch({ limit: fetchLimit + 1 }); + + const filteredMessages = messages.filter(msg => msg.author.id === message.author.id); + for (const msg of filteredMessages.values()) { + try { + if (msg.deletable) { + const delay = getRandomDelay(); + await new Promise(resolve => setTimeout(resolve, delay)); + await msg.delete().catch(err => { + if (err.code !== 10008) { + console.error('Failed to delete message:', err); + } + }); + } + } catch (error) { + console.error('Error deleting message:', error); + } + } + + remainingMessages -= filteredMessages.size; + + if (remainingMessages > 0) { + const batchDelay = getBatchDelay(); + await new Promise(resolve => setTimeout(resolve, batchDelay)); + } + } + } catch (error) { + console.error('Failed to delete messages:', error); + const errorMsg = await message.channel.send('There was an error while trying to delete messages.'); + setTimeout(() => errorMsg.delete().catch(console.error), deleteTimeout); + } }, -}; +}; \ No newline at end of file