Last active
February 28, 2019 01:56
-
-
Save untuned/ccdd4effba16d7d1bf56a824c3cb7f3f to your computer and use it in GitHub Desktop.
delete all message in a discord dm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clearMessages = function(guild_id, author_id, authToken, deleted = new Set()) { | |
const searchURL = `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true` | |
const headers = { | |
Authorization: authToken | |
} | |
let clock = 0 | |
interval = 500 | |
function delay(duration) { | |
return new Promise((resolve, reject) => { | |
setTimeout(resolve, duration) | |
}) | |
} | |
function loadMessages() { | |
return fetch(searchURL, { | |
headers | |
}) | |
} | |
function tryDeleteMessage(message) { | |
if (message.author.id == author_id && !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 | |
} | |
//filter by author | |
messages = messages.filter(m => m.author.id == author_id) | |
// 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() | |
} else if (resp.status === 403) { | |
deleted.add(message.id) // in case of call messages | |
return resp.text() | |
} | |
} | |
}) | |
})) | |
}) | |
.then(function() { | |
if (messagesStore.length !== 0) { | |
clearMessages(guild_id, author_id, authToken, deleted) | |
} else { | |
console.log(`We have loaded all messages in this chat.`) | |
} | |
}) | |
} | |
/* AUTH TOKEN HERE */ | |
/* AUTH TOKEN HERE */ | |
/* AUTH TOKEN HERE */ | |
var authToken = "" | |
/* AUTH TOKEN HERE */ | |
/* AUTH TOKEN HERE */ | |
/* AUTH TOKEN HERE */ | |
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('0000000000000000', '0000000000000000', authToken) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment