Skip to content

Instantly share code, notes, and snippets.

@dchem
Forked from orion-v/how-to.md
Created November 21, 2018 21:34
Show Gist options
  • Save dchem/bc0dc81c2c04a748e3e64a3f10ca071f to your computer and use it in GitHub Desktop.
Save dchem/bc0dc81c2c04a748e3e64a3f10ca071f to your computer and use it in GitHub Desktop.
Delete all messages of an user in a Discord channel or server

Delete discord user message history

This script allow for user specific message deletion from an entire server or a single channel using the browser console. This script uses discord search API and it will only delete messages of a chosen user.

How to use

1. Enable developer mode in discord

Go to user settings > appearance in discord and enable Developer mode.

2. Copy and paste the script to a file so you can change the server and author ids.

3. Replace the server and author ids with your own.

Open discord and right click on the server icon and click copy id. Replace the server id in the script with your server id. Do the same process for the author id by right clicking the avatar image.

4. Run script in console

Press F12 in Chrome or Firefox to open the console. Paste the modified script in the console and press enter.

The more messages the longer it takes. You can check if the messages have been deleted by using the search.


Notes

I think there are some channels that this script won't work with. I think they may be NSFW channels but I haven't tested enough.

Use this script at your own risk

This script was based on the following scripts https://gist.github.com/niahoo/c99284a8908cd33d59b4aff802179e9b#gistcomment-2397287 https://gist.github.com/IMcPwn/0c838a6248772c6fea1339ddad503cce

async function clearMessages() {
const server = "000000000000000000"; // server id number
const author = "000000000000000000"; // user id number
const channel = window.location.href.split('/').pop(); // remove this line to delete all messages of an user from a server
const authToken = document.body.appendChild(document.createElement`iframe`).contentWindow.localStorage.token.replace(/"/g, "");
const headers = { 'Authorization': authToken, 'Content-Type': 'application/json' };
const baseURL = `https://discordapp.com/api/v6/channels`;
let searchURL = `https://discordapp.com/api/v6/guilds/${server}/messages/search?author_id=${author}`;
if (typeof channel !== 'undefined') searchURL = searchURL + `&channel_id=${channel}`;
let clock = 0;
let interval = 500;
function delay(duration) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), duration);
});
}
const response = await fetch(searchURL, {headers});
const json = await response.json();
console.log("There are " + json.total_results + " messages left to delete.");
await Array.from(json.messages).map(message => {
message.forEach(async function(item) {
if(item.hit == true) {
await delay(clock += interval);
await fetch(`${baseURL}/${item.channel_id}/messages/${item.id}`, { headers, method: 'DELETE' });
}
});
});
if (json.total_results > 0) {
delay(clock += interval).then(() => { clearMessages(); });
} else {
console.log("Finished deleting messages")
};
}
clearMessages();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment