Skip to content

Instantly share code, notes, and snippets.

@steckel
Forked from IMcPwn/delete-all-messages.js
Last active January 18, 2019 07:31
Show Gist options
  • Save steckel/54e3dc1ef2531cc70f654560aef69b2e to your computer and use it in GitHub Desktop.
Save steckel/54e3dc1ef2531cc70f654560aef69b2e to your computer and use it in GitHub Desktop.
Delete all messages in a Discord channel
// Turn on Developer Mode under User Settings > Appearance > Developer Mode (at the bottom)
// Then open the channel you wish to delete all of the messages (could be a DM) and click the three dots on the far right.
// Copy/paste the below script into the JavaScript console.
// invoke the function `clearMessages("username", "messageId")`
// where username is optional username to filter off of and where messageID is an optional ID that you wish to retain, but delete everything before
function clearMessages(username, before) {
const authToken = document.body.appendChild(document.createElement`iframe`).contentWindow.localStorage.token.replace(/"/g, "");
const channel = window.location.href.split('/').pop();
const baseURL = `https://discordapp.com/api/channels/${channel}/messages`;
const headers = {"Authorization": authToken };
const delay = duration => new Promise((resolve, reject) => {
setTimeout(() => resolve(), duration);
});
function clearMessages(before) {
let clock = 0;
let interval = 500;
let beforeQuery = before == null ? '' : `&before=${before}`;
fetch(`${baseURL}?limit=100${beforeQuery}`, {headers})
.then(resp => resp.json())
.then(messages => {
before = messages[messages.length - 1].id;
return username == null
? messages
: messages.filter(msg => msg.author.username === username);
}).then(messages => Promise.all(messages.map(msg => {
return delay(clock += interval)
.then(() => fetch(`${baseURL}/${msg.id}`, {headers, method: 'DELETE'}));
})))
.then(() => clearMessages(before));
}
clearMessages(before);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment