Last active
January 8, 2018 22:59
-
-
Save eritbh/5cea601e2244da2c69d5117b9337178c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function batchDeleteMyMessages (channel, limit, before = undefined, loops = 1) { | |
if (limit <= 0 || loops > 10) return console.log(limit, loops) // If there are no more messages or we've been here too long, leave | |
// Get 100 messages from the channel | |
channel.getMessages(100, before).then(messages => { | |
// Get a list of all the messages from the bot | |
let toDelete = messages.filter(m => m.author.id === (channel.guild.shard.client || channel._client).user.id) | |
// Also save the ID of the first message so we can give to the next iteration | |
const firstMessageId = messages[0].id | |
// If we have more messages from the bot than we need, only take the last ones | |
if (toDelete.length > limit) { | |
toDelete = toDelete.slice(0 - limit) | |
} | |
// Delete all the messages we need to and wait until it's done | |
Promise.all(toDelete.map(m => m.delete())).then(() => { | |
// We deleted all those messages, now we call this function again to | |
// process the next batch. We subtract the messages that we deleted here | |
// from the limit for the next batch, and we provide the first message | |
// in this batch so the next one knows how far back in the log to start. | |
// We also increment the loops argument so we can stop ourselves from | |
// making too many recursive calls to the API. | |
batchDeleteMyMessages(channel, limit - toDelete.length, firstMessageId, loops + 1) | |
}, err => { | |
// oh god fucking damn it | |
console.log(err) | |
}) | |
}) | |
})(channel, 50) | |
// ^ The arguments we pass to the function to start with: The channel that we're | |
// looking through, and the number of total messages to delete. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment