Skip to content

Instantly share code, notes, and snippets.

@Rmlyy
Last active November 28, 2022 10:04
Show Gist options
  • Save Rmlyy/3b996e82b999a8a3949c8ccd5e95030f to your computer and use it in GitHub Desktop.
Save Rmlyy/3b996e82b999a8a3949c8ccd5e95030f to your computer and use it in GitHub Desktop.
Discord Self-Bot to delete all messages from a channel
const { Client } = require('discord.js-selfbot-v13')
const client = new Client({
checkUpdate: false,
})
const channelId = process.env.CHANNEL_ID
async function fetchAllMessages(channelId) {
const channel = client.channels.cache.get(channelId)
let messages = []
// Create message pointer
let message = await channel.messages
.fetch({ limit: 1 })
.then((messagePage) => (messagePage.size === 1 ? messagePage.at(0) : null))
while (message) {
await channel.messages.fetch({ limit: 100, before: message.id }).then((messagePage) => {
messagePage.forEach((msg) => messages.push(msg))
// Update our message pointer to be last message in page of messages
message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null
})
}
return messages
}
client.on('ready', async () => {
console.log(`Logged in as ${client.user.username}`)
console.log('getting ahold of the channel')
const channel = client.channels.cache.get(channelId)
console.log('fetching all messages...')
const messages = await fetchAllMessages(channelId)
const ourMessages = []
console.log('looping through all messages...')
// could've used .filter ?
messages.forEach((message) => {
if (message.author.id === client.user.id) {
ourMessages.push(message)
}
})
console.log('looping through our messages...')
ourMessages.forEach((message, index) => {
setTimeout(() => {
console.log('deleting message id', message.id)
channel.messages.delete(message.id)
}, index * 10000)
})
console.log('done')
})
client.login(process.env.TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment