-
Star
(240)
You must be signed in to star a gist -
Fork
(35)
You must be signed in to fork a gist
-
-
Save victornpb/135f5b346dea4decfc8f63ad7d9cc182 to your computer and use it in GitHub Desktop.
| //Paste this function in DevTools console inside Discord | |
| function deleteMessages(authToken, authorId, channelId, firstMessageId) { | |
| const start = new Date(); | |
| const delayDelete = 500; | |
| const delaySearch = 1000; | |
| let delCount = 0; | |
| let failCount = 0; | |
| let estimatedPing = 220; | |
| let grandTotal; | |
| const history = []; | |
| const wait = async (ms) => new Promise(done => setTimeout(done, ms)); | |
| const msToHMS = (s) => `${s / 3.6e6 | 0}h ${(s % 3.6e6) / 6e4 | 0}m ${(s % 6e4) / 1000 | 0}s`; | |
| function log() { history.push(arguments); console.log.apply(console, arguments); pp.insertAdjacentHTML('beforeend', `<div style='color:black;'>${Array.from(arguments).map(o => typeof o === 'object' ? JSON.stringify(o) : o).join('\t')}</div>`); popup.scrollBy(0, 1e10); }; | |
| function error() { history.push(arguments); console.error.apply(console, arguments); pp.insertAdjacentHTML('beforeend', `<div style='color:red;'>${Array.from(arguments).map(o => typeof o === 'object' ? JSON.stringify(o) : o).join('\t')}</div>`); popup.scrollBy(0, 1e10); }; | |
| var popup = window.open('', '', 'width=800,height=1000,top=0,left=0'); | |
| popup.document.body.innerHTML = '<pre></pre>'; | |
| const pp = popup.document.getElementsByTagName('pre')[0]; | |
| if (!popup) console.error('Popup blocked!'); | |
| log(`---- You can abort by setting STOP=1 on the console ----`); | |
| log(`Starting at ${start}, channelId=${channelId} authorId=${authorId} firstMessageId=${firstMessageId}`); | |
| recurse(); | |
| async function recurse() { | |
| const headers = { | |
| "Authorization": authToken | |
| }; | |
| const deleteAfter = `search?author_id=${authorId}` + (firstMessageId ? `&min_id=${firstMessageId}` : ''); | |
| const baseURL = `https://discordapp.com/api/v6/channels/${channelId}/messages/`; | |
| let resp; | |
| try { | |
| resp = await fetch(baseURL + deleteAfter, { | |
| headers | |
| }); | |
| } | |
| catch (err) { | |
| error('Something went wrong!', err); | |
| return; | |
| } | |
| const result = await resp.json(); | |
| const total = result.total_results; | |
| if (!grandTotal) grandTotal = total; | |
| log(`Messages to delete: ${result.total_results}`, `Time remaining: ${msToHMS((delaySearch * Math.round(total / 25)) + ((delayDelete + estimatedPing) * total))} (ping: ${estimatedPing << 0}ms)`); | |
| if (result.total_results > 0) { | |
| for (let i = 0; i < result.messages.length; i++) { | |
| const element = result.messages[i]; | |
| for (let j = 0; j < element.length; j++) { | |
| const message = element[j]; | |
| if (window.STOP) return error('STOPPED! (If you want to continue set STOP=0 and run again!'); | |
| if (message.type === 3) { | |
| log('System message?', message); | |
| } | |
| else if (message.author.id == authorId && message.hit == true) { | |
| log(`${((delCount + 1) / grandTotal * 100).toFixed(2)}% (${delCount + 1}/${grandTotal}) Deleting ID:${message.id}`, | |
| `[${new Date(message.timestamp).toLocaleString()}] ${message.author.username}#${message.author.discriminator}: ${message.content}`, | |
| message.attachments.length ? message.attachments : ''); | |
| const s = Date.now(); | |
| try { | |
| await fetch(baseURL + message.id, { | |
| headers, | |
| method: "DELETE" | |
| }); | |
| delCount++; | |
| } catch (err) { | |
| error('Failed to delete message:', message, 'Error:', err); | |
| failCount++; | |
| } | |
| estimatedPing = (estimatedPing + (Date.now() - s)) / 2; | |
| await wait(delayDelete); | |
| } | |
| } | |
| } | |
| log('Getting next messages...'); | |
| await wait(delaySearch); | |
| return recurse(); | |
| } | |
| else { | |
| const msg = `Everything is clear!\n Messages deleted: ${delCount} Failed: ${failCount} \n Total time: ${msToHMS(Date.now() - start.getTime())}`; | |
| log(msg); | |
| return result; | |
| } | |
| } | |
| } |
| //Your token, you can find it in other requests headers | |
| var authToken = "NabcdefghiU4NDY5ODI2NTY.KXx1ab.aU8FUz9fOABxdsi1LYORJuv666f"; | |
| //Right click your avatar in any chat, Copy ID | |
| var authorId = "112233445566778899"; | |
| //Right click a channel or DM, Copy ID | |
| var channelId = "112233445566778899"; | |
| //Delete all messages after this one, (leave blank to delete all your messages in a channel) | |
| var firstMessageId = ""; | |
| //start | |
| deleteMessages(authToken, authorId, channelId, firstMessageId); |
Iโll try to handle this edge case
@JoeCow same issues for me. It just kinda stopped working, with the system file errors, and same thing when I tried your update as well. It's been doing this each time.
I've used STOP=0 & STOP=1 to abort, and restart, with both scripts, but I'm still stuck.

@victornpb any ideas? To add, your original script worked great for one DM, but when I went to switch to another it went through a few hundred, and hit a wall.
I've used STOP=0 & STOP=1 to abort, and restart, with both scripts, but I'm still stuck
I never said to use STOP= I could be wrong but as far I can tell it simply pauses the script and once the script is started it wont check for updated variables aside from STOP itself, here's more explicit instructions:
- Follow victors instructions and run as normal (I recommend putting the start script in a text editor so that you don't need to fill in the variables again) if the output fills with system message continue to step 2
- Reload the webpage you ran the script from
- Edit the scripts as I instructed in my comment (Keep both scripts in a text editor as you may need them again depending on how many system messages you have)
- Paste in the edited main script and press enter
- Fill in the edited start script as you normally would or better yet it should already be filled in. Fill in afterMessageID in the edited start script by going into the dm and finding your most recent message (Discord's search feature is useful here) and copying the message id
- Paste in the edited and filled in start script and press enter
- Depending on how many system message you have it may eventually fill with them again, in that case reload the webpage and repeat from Step 4 being sure to update var afterMessageId = ""; with your latest message each time
Incredibly unoptimized, slow, and barely functional. Eats up massive amounts of RAM, like 3GB.
Would not recommend using this. Try out this guy's.
I will eventually incorporate the suggestions from the other comments once I get some free time.
@WeaponizedMeme I have no Idea how you came up with this number, Discord takes that amount of RAM not this. The code you recommended uses basically the same approach except it doesn't bother to check anything prior to deleting messages, it doesn't actually check to see if the chat is indexed, doesn't check if you're getting throttled. In fact that other code is slower as it blindly waits half a second between every message, while I implemented an algorithm that actually tries to delete messages as fast as discord allows it without getting rate limited...
The code you linked is shorter in length I'll give you that, but it is shorter for a reason.
Hi. I'm getting an issue where nothing is being deleted.
When I first ran this script it deleted about 20k of my messages, but ever since then the blue numbers on the side just keep increasing while the number of messages stays unchanged.
I only encounter this in a specific DM. The script works in other DMs/servers I've used it in. Any tips or ideas as to what is causing this?
BTW The small decrease in messages is from me manually deleting/clicking while I was hoping the script would start working
Worked really well for me. Was a very long DM history I was looking to delete though so did eventually get clogged up with system messages.
Used the recommended modifications here though and that fixed that issue.
Thanks victornpb for the great script and JoeCow for the modifications to combat the system messages.
Hi. I'm getting an issue where nothing is being deleted.
When I first ran this script it deleted about 20k of my messages, but ever since then the blue numbers on the side just keep increasing while the number of messages stays unchanged.
I only encounter this in a specific DM. The script works in other DMs/servers I've used it in. Any tips or ideas as to what is causing this?BTW The small decrease in messages is from me manually deleting/clicking while I was hoping the script would start working
Hello @mgcbufly
This is probably due to system messages.
Eventually they will fill up the queue and not be able to be deleted. So it will keep querying and only getting the same system messages in return.
Use JoeCow's modifications that he described here: https://gist.github.com/victornpb/135f5b346dea4decfc8f63ad7d9cc182#gistcomment-2934064
Hey, what can i do with?:
Started at 8/19/2019, 10:47:37 AM
channelId=544122824873476126 authorId=527227515706540053 firstMessageId=
---- You can abort by setting STOP=1 on the console ----
API respondend with non OK status! {"code":0,"message":"401: Unauthorized"}
OK, I finally put some time in handling system messages. It seems to be working now, but I'm limited to generating dummy messages to delete. Please let me know if you find any issue.
Thanks @JoeCow for the contribution. In the end I handled things differently, It should not leave messages behind anymore.
Also I made it simpler to use I implemented a minimal user interface, and I can fill the author and channel you're on automatically, so you only need to provide the authToken. That made the code quite bigger tho. But I don't want to make the code cryptic just to make it short because I want it to still be readable, so pleople can trust it before running it.

I'm migrating this Gist to a GitHub Repository. It will be easier for me to make changes and answer issues and questions individually using the the issues tab.
Please post your comments, questions, issues, ideas HERE
GIST MOVED TO
https://github.com/victornpb/undiscord
Error deleting message, API responded with status 403! {"message":"Cannot execute action on a DM channel","code":50003}
how fix it?
@SKN1 the comment just before yours tells you to ask questions on the GitHub repo, not this gist.
Can I have your discord? I need you with something
Isn't this just against discord TOS? ;) My suggestion it's pointless and awful to use! :D
Hi, I'm having some trouble using this and wanted to ask if it works for messages in banned servers? I'm filling in all the details, with the exception of GuildID because I can't get that (I think it's the server), but I'm just getting 404 errors.
Anyone wondering. yes. This is a TOS breaker. It WILL get you banned. I do not know if they will delete your account. But this is self botting. Your account will.100% be banned, as I found out.
If discord implemented a function to delete messages, however, it would resolve this issue. Seems they wont though.
I managed to delete a few thousand messages however, before getting banned. Say about in the region of 10,000.
Then I ended up getting banned.
This works fine when deleting messages from a public channel, but throws a 404 error for DMs. Tried with several different people. Can you see if you can reproduce that problem?
for DM
use guild id :@me
and channel id: text after @me/ in url
Same...404 error
for DM
use guild id :@me
and channel id: text after @me/ in url
Thanks #victornpb
I owe you one !
its not letting me do anything and ive entered all the ids and stuff
please help me nothings working
how do i find the auth token in firefox? "get" isn't doing anything and i cant manually find it in console
404 message on public channel. filled everything in. Doesn't work. Exit code `0```
DISCUSSION MOVED TO
๐ https://github.com/victornpb/undiscord/discussions
โ๏ธ DO NOT USE THIS COMMENT SECTION, YOU WILL NOT BE ANSWERED HERE!
Is there another way to delete messages from server am ban, but the mods wont do so, is not that i said somthing but they were a toxic community and i just want to delete the messages so i wont be affacted
Is there another way to delete messages from server am ban, but the mods wont do so, is not that i said somthing but they were a toxic community and i just want to delete the messages so i wont be affacted
If you're banned, no, there's no way to delete them.
Also can't you read?
Discussion has been moved to the new GitHub..
does this work on bot dms ?
i have one that's filled with so many messaages that lags me out whenever i enter the message history of it



I posted a workaround here. This should work still as looking through revisions there has been one minor change to the script since I posted which pushed the second edit down 2 lines but I have edited the linked comment to reflect that. Since I am not a coder and just used logic to edit the script a little it's not the best solution as I do believe you need to reload and paste in the edited scripts every time you fill up with system messages. Would have made my own fork with the changes already done but figure someone who actually knows code will/can come up with something more graceful.