Skip to content

Instantly share code, notes, and snippets.

@hdeshev
Forked from rcx/delete-all-messages.js
Last active May 2, 2020 07:07
Show Gist options
  • Save hdeshev/22b326b79b2e7c21d68906095b399a1b to your computer and use it in GitHub Desktop.
Save hdeshev/22b326b79b2e7c21d68906095b399a1b to your computer and use it in GitHub Desktop.
Delete all your messages in a Discord channel
clearMessages = function (guild_id, authToken, deleted = new Set(), interval = 1000) {
/*
* Discord: Don't copy stuff into this box
* Me: dOn'T COpy sTuFf iNtO tHIs bOx
*/
const searchURL = `https://discordapp.com/api/v6/guilds/${guild_id}/messages/search?include_nsfw=true`
const headers = { Authorization: authToken }
let clock = 0
function delay(duration) {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration)
})
}
function loadMessages() {
return fetch(searchURL, { headers })
}
function tryDeleteMessage(message) {
// RAce coNDItiOn
if (!deleted.has(message.id)) { // skip already deleted messages
console.log(`Deleting message ${message.id} from ${message.author.username} (${message.content}...)`)
return fetch(`https://discordapp.com/api/v6/channels/${message.channel_id}/messages/${message.id}`, { headers, method: 'DELETE' })
}
}
let messagesStore = []
loadMessages()
.then(resp => resp.json())
.then(messages => {
messages = messages.messages
if (messages === undefined || messages === null || messages.length == 0) {
console.log(`Couldn't load messages. Check guild id, author id, and auth token.`)
return
}
messages = messages.filter(m => m) // clean undefined
messages = [].concat.apply([], messages); // flatten
messages = messages.filter(m => m) // clean undefined
if (messages.length === 0) {
console.log(`Couldn't load messages. Check guild id, author id, and auth token.`)
return
}
// unique by id
messages = messages.filter((e, i) => messages.findIndex(a => a.id === e.id) === i);
beforeId = messages[messages.length-1].id
messagesStore = messagesStore.concat(messages)
return Promise.all(messagesStore.map(message => {
return delay(clock += interval)
.then(() => tryDeleteMessage(message))
.then(resp => {
if (resp) {
if (resp.status == 429) {
interval += 10
console.log(`Too fast; bumping interval to ${interval}`)
} else if (resp.status === 204) {
deleted.add(message.id) // mark deleted
return resp.text()
}
}
})
}))
})
.then(function() {
if (messagesStore.length !== 0) {
clearMessages(guild_id, authToken, deleted, Math.min(interval, 1500))
} else {
console.log(`We have loaded all messages in this chat.`)
}
})
}
var authToken = ""
if (authToken.length === 0) {
var localToken = document.body.appendChild(document.createElement(`iframe`)).contentWindow.localStorage.token
if (localToken === undefined) {
console.log(`Getting the auth token from localStorage isn't supported on Chrome or the desktop client. Use Firefox or grab it from a network request's headers.`)
console.log(`To do that go to the Network tab of your inspector and copy the Authorization header of a request. There are detailed instructions in the tutorial.`)
} else {
authToken = JSON.parse(localToken)
}
}
if (authToken.length !== 0) {
clearMessages('____________guild_id_here_______________', authToken)
}

Delete all messages in a Discord channel

This works best in Firefox, I haven't tested it in Chrome. On the desktop client, it has trouble getting the Auth token from LocalStorage so you need to grab it from a request's headers and replace the parameter "authToken" on the last line with it. There's instructions on how to do that below.

Preqrequisites:

  • computer
  • general knowledge of your browser's inspector/developer tools
  • possibly even a brain

1. Grab guild id

RUN AS ADMIN ONLY! WILL DELETE ALL MESSAGES!!!

The script uses the internal id values Discord refers to the selected server and user by. The easy way to do this is to enable Developer Mode in your Discord settings, after which you can simply right click the server's icon and click "Copy ID". Otherwise, you can grab the server's id from the URL, and you can get your own id using the network tab of your browser's inspector.

2. Get your authorization token

This is now automated on Firefox, despite Discord "hiding" the localStorage variable from the DOM. On Chrome and the desktop client it seems to not be in localStorage (???) so you need to get it from a network request. Follow these easy steps:

  1. Open the inspector (Ctrl-Shift-I)
  2. Open the Network tab of your inspector
  3. Open any request to Discord's servers
  4. Look for the "Authorization" request header. Copy that into the line var authToken = "". For example it might look like var authToken = "your_auth_token_goes_here"

3. Get the script

Copy-paste the script into the javascript console, and replace ____________guild_id_here_______________ and ____________author_id_here______________ with the respective ids copied in Step 1. If you're on Chrome or Desktop Client enter your auth token as well (see above).

4. Launch

Strike the Return key on your keyboard...come on, this is not rocket science man.

FAQ

How do I use this to delete DMs? Replace searchURL with https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true. For example, replace this line:

const searchURL = `https://discordapp.com/api/v6/guilds/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`

with

const searchURL = `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
@Wulgaren
Copy link

Wulgaren commented May 2, 2020

hello, how can i make this delete messeges with a specific word? &content= doesnt work for some reason

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment