SHARE
TWEET

Untitled

a guest Apr 25th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clearMessages = function (guild_id, author_id, authToken, deleted = new Set()) {
  2.     /*
  3.      * Discord: Don't copy stuff into this box
  4.      * Me: dOn'T COpy sTuFf iNtO tHIs bOx
  5.      */
  6.     const searchURL = `https://discordapp.com/api/v6/guilds/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
  7.     const headers = { Authorization: authToken }
  8.     let clock = 0
  9.     interval = 500
  10.     function delay(duration) {
  11.         return new Promise((resolve, reject) => {
  12.             setTimeout(resolve, duration)
  13.         })
  14.     }
  15.     function loadMessages() {
  16.         return fetch(searchURL, { headers })
  17.     }
  18.     function tryDeleteMessage(message) {
  19.         // RAce coNDItiOn
  20.         if (message.author.id == author_id && !deleted.has(message.id)) { // skip already deleted messages
  21.             console.log(`Deleting message ${message.id} from ${message.author.username} (${message.content}...)`)
  22.             return fetch(`https://discordapp.com/api/v6/channels/${message.channel_id}/messages/${message.id}`, { headers, method: 'DELETE' })
  23.         }
  24.     }
  25.     let messagesStore = []
  26.  
  27.     loadMessages()
  28.         .then(resp => resp.json())
  29.         .then(messages => {
  30.             messages = messages.messages
  31.             if (messages === undefined || messages === null || messages.length == 0) {
  32.                 console.log(`Couldn't load messages. Check guild id, author id, and auth token.`)
  33.                return
  34.            }
  35.            messages = messages.filter(m => m) // clean undefined
  36.            messages = [].concat.apply([], messages); // flatten
  37.            messages = messages.filter(m => m) // clean undefined
  38.            if (messages.length === 0) {
  39.                console.log(`Couldn't load messages. Check guild id, author id, and auth token.`)
  40.                 return
  41.             }
  42.        
  43.             //filter by author
  44.             messages = messages.filter(m => m.author.id == author_id)
  45.             // unique by id
  46.             messages = messages.filter((e, i) => messages.findIndex(a => a.id === e.id) === i);
  47.  
  48.             beforeId = messages[messages.length-1].id
  49.             messagesStore = messagesStore.concat(messages)
  50.             return Promise.all(messagesStore.map(message => {
  51.                 return delay(clock += interval)
  52.                     .then(() => tryDeleteMessage(message))
  53.                     .then(resp => {
  54.                         if (resp) {
  55.                             if (resp.status == 429) {
  56.                                 interval += 10
  57.                                 console.log(`Too fast; bumping interval to ${interval}`)
  58.                             } else if (resp.status === 204) {
  59.                                 deleted.add(message.id) // mark deleted
  60.                                 return resp.text()
  61.                             }
  62.                         }
  63.                     })
  64.             }))
  65.         })
  66.         .then(function() {
  67.             if (messagesStore.length !== 0) {
  68.                 clearMessages(guild_id, author_id, authToken, deleted)
  69.             } else {
  70.                 console.log(`We have loaded all messages in this chat.`)
  71.             }
  72.         })
  73. }
  74. var authToken = ""
  75. if (authToken.length === 0) {
  76.     var localToken = document.body.appendChild(document.createElement(`iframe`)).contentWindow.localStorage.token
  77.     if (localToken === undefined) {
  78.         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.`)
  79.         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.`)
  80.     } else {
  81.         authToken = JSON.parse(localToken)
  82.     }
  83. }
  84.  
  85. authToken = '123123123123-rE';
  86.  
  87. if (authToken.length !== 0) {
  88.     console.log('starting...');
  89.     // guild id (1st one), author id
  90.     clearMessages('12341234123421341234', '123412341234123421341234', authToken)
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top