clearMessages = function (guild_id, author_id, authToken, deleted = new Set()) { /* * Discord: Don't copy stuff into this box * Me: dOn'T COpy sTuFf iNtO tHIs bOx */ 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 = 500 function delay(duration) { return new Promise((resolve, reject) => { setTimeout(resolve, duration) }) } function loadMessages() { return fetch(searchURL, { headers }) } function tryDeleteMessage(message) { // RAce coNDItiOn 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() } } }) })) }) .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 = "____________your_auth_token_here_______________" if (authToken == "____________your_auth_token_here_______________") { 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_here_______________', '____________author_id_here______________', authToken) }