Last active
June 27, 2022 20:28
-
-
Save dshoreman/c30d38a746674caa51495b766c65d69d to your computer and use it in GitHub Desktop.
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
// Source: https://gist.github.com/emanzitti/a21c915a5a547aab40d4567d948aa3f6 | |
const GUILD_ID = '', | |
AUTHOR_ID = ''; | |
clearMessages = function (guild_id, author_id, authToken, deleted = new Set()) { | |
const searchURL = `https://discordapp.com/api/v6/guilds/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true` | |
const headers = { Authorization: authToken } | |
let clock = 0 | |
interval = 750 | |
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 | |
} | |
messages = messages.filter(m => m.author.id == author_id) | |
console.info(`SUCCESS! Found ${messages.length} messages!`) | |
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 += 1000 | |
console.log(`Too fast; bumping interval to ${interval}`) | |
} else if (resp.status === 204) { | |
deleted.add(message.id) // mark deleted | |
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.`) | |
} | |
}) | |
} | |
var authToken = "" | |
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(GUILD_ID, AUTHOR_ID, authToken) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment