This works best in Firefox, I haven't tested it in Chrome. On the desktop client, it may have 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
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 your name 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.
This is now automated thanks to @cntVertex, despite Discord "hiding" the token from localStorage and the DOM. If it isn't able to automatically grab it then you need to get it from a network request. Follow these easy steps:
- Open the inspector (Ctrl-Shift-I)
- Open the Network tab of your inspector
- Open any request to Discord's servers
- Look for the "Authorization" request header. Copy that into the line
var authToken = ""
. For example it might look likevar authToken = "mfa.a9ushg92ru9gh9ufhsgjuidfhsgiojfhgjishefrgih3er9u3rg_asdfu9ghauisdfhguiahsdguiahsdigua"
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.
Strike the Return key on your keyboard...come on, this is not rocket science man.
Also if it does not get your auth token successfully the first time just try running it again.
- How do I use this to delete DMs?
Replace
searchURL
withhttps://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/v8/guilds/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
with
const searchURL = `https://discordapp.com/api/v8/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
The guild_id
however isn't the ID of the person who you are DMing. It is actually the ID of the channel of the DM which is a subtle but important distinction. I'm not aware of any easy way to grab this other than to just look at the network inspector under the developer tools.
- How to delete by oldest messages first?
Add &sort_by=timestamp&sort_order=asc
to searchURL
- How to only delete messages containing some text "abcd"?
Add &content=whatever
into the searchURL
. For example, https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true&content=badword
to delete only messages containing "badword".
- (By @ivmirx) If you want to delete messages before a specific date, do the following:
- Open any server to make the search field visible.
- Open the "Network" tab in the inspector and clean it from old requests with the trash can icon.
- Click in Discord's search field to show the dropdown, then click the "before" option.
- Select the date in the calendar.
- In the "Network" tab look for the request that contains
?max_id=...
. - Click on it and copy the
max_id=...
part from the URL on the right. - Append it to the script's request in the Line 6 by using
&
.
So now you should have something like:
https://discordapp.com/api/v6/guilds/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true&max_id=540727993958400000
(this example is for 2019-02-01). Also, by manually picking searchURL, you can delete messages matching a certain query, like mentioning someone, containing certain words, etc.
- Is there a python version of this script so I can run it without opening Discord
I'm trying to use this script in a very long (300k total!) messages DM channel, and it worked very well at first, deleting many messages. I've now been stuck, every single attempt at message deletion is met with a 403 error. I'm not really a programmer, but I have two suspicions:
Is there any merit to these guesses? If so, anything to be done? If it means anything, I've been running the script directly in the Discord windows client.
I apologize if any of this is formatted incorrectly, I'm a first time github commenter.
Edit: the user is also blocked, that may be important.
Edit 2: I'm pretty sure it was the block of calls. I found a workaround using Joecow's edits on this script:
https://gist.github.com/victornpb/135f5b346dea4decfc8f63ad7d9cc182#file-deletediscordmessages-js
I'm not sure if this will work forever- if it does, I still hope my comment can help improve this script.