/* * Discord: Don't copy stuff into this box * Me: dOn'T COpy sTuFf iNtO tHIs bOx */ clearMessages = function (guild_id, author_id, authToken, deleted = new Set()) { if (guild_id[0] == "_" && guild_id[guild_id.length - 1] == "_") { alert("Oops! You forgot to set the guild_id. Please fill it in.") return; } if (author_id[0] == "_" && author_id[author_id.length - 1] == "_") { alert("Oops! You forgot to set the author_id. Please fill it in.") return; } const searchURL = `https://discordapp.com/api/v9/guilds/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true&sort_by=timestamp&sort_order=asc` const headers = { Authorization: authToken } let clock = 0 interval = 3000 function delay(duration) { return new Promise((resolve, reject) => { setTimeout(resolve, duration) }) } function loadMessages() { return fetch(searchURL, { headers }) } function tryDeleteMessage(message) { // RAce coNDItiOn if (!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/v9/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 } // only delete hits messages = messages.filter(m => m.hit == true) // 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 += 100 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.`) } }) } // If the script is having trouble automatically grabbing auth token, please fill it in manually here. Check README for instructions. authToken = ""; guild_id = '____________guild_id_here_______________' author_id = '____________author_id_here______________' function grabAuthTokenFast() { var localToken = document.body.appendChild(document.createElement(`iframe`)).contentWindow.localStorage.token; if (localToken !== undefined) { authToken = JSON.parse(localToken); return true; } return false; } function grabAuthToken() { if (authToken.length === 0) { // Lmao bypass token security measures window.onbeforeunload = grabAuthToken; window.location.reload(); // yoink var localToken = document.body.appendChild(document.createElement(`iframe`)).contentWindow.localStorage.token; if (localToken !== undefined) { authToken = JSON.parse(localToken); setTimeout(main, 100, authToken); } } return false; } function main(authToken) { if (authToken.length !== 0) { console.log(`Successfully grabbed your auth token: ` + authToken) console.log(`Don't share this token with anyone!`) clearMessages(guild_id, author_id, authToken); } else { console.log(`Getting the auth token from localStorage isn't supported on your 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 README.`) } } if (grabAuthTokenFast()) { console.log("Successfully grabbed auth token using easy method"); main(authToken); } else { window.alert("Grabbing your auth token. If a confirm window shows up, please click 'Cancel' in Chrome / 'Stay on Page' in Firefox."); if (authToken.length !== 0) { main(authToken); } else { grabAuthToken(); } }