Skip to content

Instantly share code, notes, and snippets.

@johnpyp
Forked from rcx/delete-all-messages.js
Created October 25, 2019 21:08
Show Gist options
  • Save johnpyp/013dd507746a5246b76c02c79bd1f028 to your computer and use it in GitHub Desktop.
Save johnpyp/013dd507746a5246b76c02c79bd1f028 to your computer and use it in GitHub Desktop.

Revisions

  1. @ecx86 ecx86 revised this gist Aug 28, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions delete-all-messages.js
    Original file line number Diff line number Diff line change
    @@ -85,6 +85,7 @@ if (authToken == "____________your_auth_token_here_______________") {
    if (localToken === undefined) {
    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.`)
    authToken = ""
    } else {
    authToken = JSON.parse(localToken)
    }
  2. @ecx86 ecx86 revised this gist Aug 25, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions delete-all-messages.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    /*
    * 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()) {
    /*
    * 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
  3. @ecx86 ecx86 revised this gist Aug 25, 2019. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions how-to.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Delete all messages in a Discord channel

    **This works best in Firefox, I haven't tested it in Chrome. On the desktop client, it has trouble getting the Auth token from LocalStorage so you need to grab it from a request's headers and replace the parameter "authToken" on the last line with it. There's instructions on how to do that below.**
    **This works best in Firefox, I haven't tested it in Chrome. On the desktop client, it may have trouble getting the Auth token from LocalStorage so you need to grab it from a request's headers and replace the parameter "authToken" on the last line with it. There's instructions on how to do that below.**

    Preqrequisites:
    - computer
    @@ -12,10 +12,10 @@ Preqrequisites:
    The script uses the internal id values Discord refers to the selected server and user by. The easy way to do this is to enable Developer Mode in your Discord settings, after which you can simply right click the server's icon and your name and click "Copy ID". Otherwise, you can grab the server's id from the URL, and you can get your own id using the network tab of your browser's inspector.


    ### 2. Get your authorization token
    ### 2. ~~Get your authorization token~~ (now automated)

    This is now automated on Firefox, despite Discord "hiding" the localStorage variable from the DOM.
    On Chrome and the desktop client it seems to not be in localStorage (???) so you need to get it from a network request.
    This is now automated thanks to @cntVertex, despite Discord "hiding" the token from localStorage and the DOM.
    If it isn't able to automatically grab it then you need to get it from a network request.
    Follow these easy steps:

    0. Open the inspector (Ctrl-Shift-I)
    @@ -45,6 +45,8 @@ with
    const searchURL = `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
    ```

    **The `guild_id` however isn't the ID of the person who you are DMing.** It is actually the ID of the *channel of the DM* which is a subtle but important distinction. I'm not aware of any easy way to grab this other than to just look at the network inspector under the developer tools.

    - How to only delete messages containing some text "abcd"?

    Add `&content=whatever` into the `searchURL`. For example, `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true&content=badword` to delete only messages containing "badword".
  4. @ecx86 ecx86 revised this gist Aug 25, 2019. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions delete-all-messages.js
    Original file line number Diff line number Diff line change
    @@ -73,10 +73,18 @@ clearMessages = function (guild_id, author_id, authToken, deleted = new Set()) {
    }
    var authToken = "____________your_auth_token_here_______________"
    if (authToken == "____________your_auth_token_here_______________") {
    // bypass token security measures (?)
    window.alert("Getting token. When confirm window showed up, please click 'Cancel' in Chrome / 'Stay on Page' in Firefox.");
    window.onbeforeunload = function () {
    return false;
    }
    window.location.reload();

    // yoink
    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.`)
    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.`)
    } else {
    authToken = JSON.parse(localToken)
    }
  5. @ecx86 ecx86 revised this gist Jul 22, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion how-to.md
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ const searchURL = `https://discordapp.com/api/v6/channels/${guild_id}/messages/s

    Add `&content=whatever` into the `searchURL`. For example, `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true&content=badword` to delete only messages containing "badword".

    - (By ivmirx) If you want to delete messages before a specific date, do the following:
    - (By @ivmirx) If you want to delete messages before a specific date, do the following:

    1. Open any server to make the search field visible.
    2. Open the "Network" tab in the inspector and clean it from old requests with the trash can icon.
  6. @ecx86 ecx86 revised this gist Jul 22, 2019. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions how-to.md
    Original file line number Diff line number Diff line change
    @@ -48,3 +48,17 @@ const searchURL = `https://discordapp.com/api/v6/channels/${guild_id}/messages/s
    - How to only delete messages containing some text "abcd"?

    Add `&content=whatever` into the `searchURL`. For example, `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true&content=badword` to delete only messages containing "badword".

    - (By ivmirx) If you want to delete messages before a specific date, do the following:

    1. Open any server to make the search field visible.
    2. Open the "Network" tab in the inspector and clean it from old requests with the trash can icon.
    3. Click in Discord's search field to show the dropdown, then click the "before" option.
    4. Select the date in the calendar.
    5. In the "Network" tab look for the request that contains `?max_id=...`.
    6. Click on it and copy the `max_id=...` part from the URL on the right.
    7. Append it to the script's request in the Line 6 by using `&`.

    So now you should have something like:
    `https://discordapp.com/api/v6/guilds/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true&max_id=540727993958400000`
    (this example is for 2019-02-01).
  7. @ecx86 ecx86 revised this gist Jul 22, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion how-to.md
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ Strike the Return key on your keyboard...come on, this is not rocket science man

    # FAQ

    How do I use this to delete DMs?
    - How do I use this to delete DMs?
    Replace `searchURL` with `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`. For example, replace this line:

    ```js
    @@ -44,3 +44,7 @@ with
    ```js
    const searchURL = `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
    ```

    - How to only delete messages containing some text "abcd"?

    Add `&content=whatever` into the `searchURL`. For example, `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true&content=badword` to delete only messages containing "badword".
  8. @ecx86 ecx86 revised this gist Jul 22, 2019. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions delete-all-messages.js
    Original file line number Diff line number Diff line change
    @@ -71,8 +71,8 @@ clearMessages = function (guild_id, author_id, authToken, deleted = new Set()) {
    }
    })
    }
    var authToken = ""
    if (authToken.length === 0) {
    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.`)
    2 changes: 1 addition & 1 deletion how-to.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ Follow these easy steps:
    0. Open the inspector (Ctrl-Shift-I)
    1. Open the Network tab of your inspector
    2. Open any request to Discord's servers
    3. Look for the "Authorization" request header. Copy that into the line `var authToken = ""`. For example it might look like `var authToken = "your_auth_token_goes_here"`
    3. Look for the "Authorization" request header. Copy that into the line `var authToken = "____________your_auth_token_here_______________"`. For example it might look like `var authToken = "mfa.a9ushg92ru9gh9ufhsgjuidfhsgiojfhgjishefrgih3er9u3rg_asdfu9ghauisdfhguiahsdguiahsdigua"`

    ### 3. Get the script

  9. @ecx86 ecx86 revised this gist Oct 12, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion how-to.md
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,6 @@ const searchURL = `https://discordapp.com/api/v6/guilds/${guild_id}/messages/sea
    ```

    with
    ```
    ```js
    const searchURL = `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
    ```
  10. @ecx86 ecx86 revised this gist Oct 12, 2018. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions how-to.md
    Original file line number Diff line number Diff line change
    @@ -30,3 +30,17 @@ Copy-paste the script into the javascript console, and replace `____________guil
    ### 4. Launch

    Strike the Return key on your keyboard...come on, this is not rocket science man.

    # FAQ

    How do I use this to delete DMs?
    Replace `searchURL` with `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`. For example, replace this line:

    ```js
    const searchURL = `https://discordapp.com/api/v6/guilds/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
    ```

    with
    ```
    const searchURL = `https://discordapp.com/api/v6/channels/${guild_id}/messages/search?author_id=${author_id}&include_nsfw=true`
    ```
  11. @ecx86 ecx86 revised this gist Sep 30, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion how-to.md
    Original file line number Diff line number Diff line change
    @@ -18,13 +18,14 @@ This is now automated on Firefox, despite Discord "hiding" the localStorage vari
    On Chrome and the desktop client it seems to not be in localStorage (???) so you need to get it from a network request.
    Follow these easy steps:

    0. Open the inspector (Ctrl-Shift-I)
    1. Open the Network tab of your inspector
    2. Open any request to Discord's servers
    3. Look for the "Authorization" request header. Copy that into the line `var authToken = ""`. For example it might look like `var authToken = "your_auth_token_goes_here"`

    ### 3. Get the script

    Copy-paste the script into the javascript console, and replace `____________guild_id_here_______________` and `____________author_id_here______________` with the respective ids copied in Step 1.
    Copy-paste the script into the javascript console, and replace `____________guild_id_here_______________` and `____________author_id_here______________` with the respective ids copied in Step 1. If you're on Chrome or Desktop Client enter your auth token as well (see above).

    ### 4. Launch

  12. @ecx86 ecx86 revised this gist Sep 30, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion how-to.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Delete all messages in a Discord channel

    **This works best in Firefox, I haven't tested it in Chrome. If it has trouble getting the Auth token from LocalStorage so you need to grab it from a request's headers and replace the parameter "authToken" on the last line with it.**
    **This works best in Firefox, I haven't tested it in Chrome. On the desktop client, it has trouble getting the Auth token from LocalStorage so you need to grab it from a request's headers and replace the parameter "authToken" on the last line with it. There's instructions on how to do that below.**

    Preqrequisites:
    - computer
  13. @ecx86 ecx86 revised this gist Sep 30, 2018. 2 changed files with 101 additions and 49 deletions.
    116 changes: 84 additions & 32 deletions delete-all-messages.js
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,86 @@
    (function(){
    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 = []

    // Paste your token between the quotes :
    var authToken = '________________________________________'

    // https://github.com/yanatan16/nanoajax
    !function(t,e){function n(t){return t&&e.XDomainRequest&&!/MSIE 1/.test(navigator.userAgent)?new XDomainRequest:e.XMLHttpRequest?new XMLHttpRequest:void 0}function o(t,e,n){t[e]=t[e]||n}var r=["responseType","withCredentials","timeout","onprogress"];t.ajax=function(t,a){function s(t,e){return function(){c||(a(void 0===f.status?t:f.status,0===f.status?"Error":f.response||f.responseText||e,f),c=!0)}}var u=t.headers||{},i=t.body,d=t.method||(i?"POST":"GET"),c=!1,f=n(t.cors);f.open(d,t.url,!0);var l=f.onload=s(200);f.onreadystatechange=function(){4===f.readyState&&l()},f.onerror=s(null,"Error"),f.ontimeout=s(null,"Timeout"),f.onabort=s(null,"Abort"),i&&(o(u,"X-Requested-With","XMLHttpRequest"),e.FormData&&i instanceof e.FormData||o(u,"Content-Type","application/x-www-form-urlencoded"));for(var p,m=0,v=r.length;v>m;m++)p=r[m],void 0!==t[p]&&(f[p]=t[p]);for(var p in u)f.setRequestHeader(p,u[p]);return f.send(i),f},e.nanoajax=t}({},function(){return this}());
    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);

    var regexReactId = /\$[0-9]+/g
    var ids = $$('[data-reactid*=":$"].message-text').map(function getMessageId(el) {
    var reactid = el.getAttribute('data-reactid')
    var match = reactid.match(regexReactId)
    var id = match.pop().substr(1)
    return id
    }).filter(function(id){
    return !!id
    })
    var channel = window.location.href.split('/').pop()
    var base_url = 'https://discordapp.com/api/channels/' + channel + '/messages/'
    var deleteLoop = function(){
    if (! ids.length) { return }
    var id = ids.pop()
    nanoajax.ajax({
    url: base_url + id,
    method: 'DELETE',
    headers: {
    authorization: authToken
    }
    }, function(){
    setTimeout(deleteLoop, 500)
    })
    }
    deleteLoop()
    }())
    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 = ""
    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_here_______________', '____________author_id_here______________', authToken)
    }
    34 changes: 17 additions & 17 deletions how-to.md
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,31 @@
    # Delete all messages in a Discord channel

    You have to know how to use your browser developer tools to use this thechnique.
    **This works best in Firefox, I haven't tested it in Chrome. If it has trouble getting the Auth token from LocalStorage so you need to grab it from a request's headers and replace the parameter "authToken" on the last line with it.**

    ### 1. Open your channel
    Preqrequisites:
    - computer
    - general knowledge of your browser's inspector/developer tools
    - possibly even a brain

    The URL must be like `https://discordapp.com/channels/XXXXXXX` and not `https://discordapp.com/channels/XXXXXXXXX/YYYYYYYY`. If so, change it manually. Once on the right page, you must not reload or navigate.
    ### 1. Grab guild and author ids

    The script uses the internal id values Discord refers to the selected server and user by. The easy way to do this is to enable Developer Mode in your Discord settings, after which you can simply right click the server's icon and your name and click "Copy ID". Otherwise, you can grab the server's id from the URL, and you can get your own id using the network tab of your browser's inspector.

    ### 2. Get your authorization token
    - Open the dev tools (F12), open the Network tab. (You should clear all requests for better readability if you see some.)
    - Delete one message manually. In the request log, you will see a request with a `DELETE` method.
    - Click on the request to open the details, and on the Headers tab, copy the 'authorization' thoken. It's a long text with dots like `MTX5MzQ1MjAyMjU0NjA2MzM2.ROFLMAO.UvqZqBMXLpDuOY3Z456J3JRIfbk`.

    ### 2. Get your authorization token

    ### 3. Get the script
    This is now automated on Firefox, despite Discord "hiding" the localStorage variable from the DOM.
    On Chrome and the desktop client it seems to not be in localStorage (???) so you need to get it from a network request.
    Follow these easy steps:

    Edit the javascript code from this tutorial in a text editor.
    1. Open the Network tab of your inspector
    2. Open any request to Discord's servers
    3. Look for the "Authorization" request header. Copy that into the line `var authToken = ""`. For example it might look like `var authToken = "your_auth_token_goes_here"`

    Find the line starting with with `var authToken = ` and paste the token. Pay attention to the quotes. The code should read like this :
    ### 3. Get the script

    ```javascript
    // Paste your token between the quotes :
    var authToken = 'MTX5MzQ1MjAyMjU0NjA2MzM2.ROFLMAO.UvqZqBMXLpDuOY3Z456J3JRIfbk'
    ```
    Copy-paste the script into the javascript console, and replace `____________guild_id_here_______________` and `____________author_id_here______________` with the respective ids copied in Step 1.

    ### 4. Launch

    This script will only delete the messages that are visible. So, if you want to delete more messages, you should scroll top to show more of them **before** launch. If you do not have the permissions to delete some messages, the script should still work with yours (not tested).

    Copy the full code that you have edited, paste it into the browser javascript console and watch your messages being deleted.
    Strike the Return key on your keyboard...come on, this is not rocket science man.
  14. niahoo osef revised this gist May 3, 2016. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions how-to.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,3 @@ var authToken = 'MTX5MzQ1MjAyMjU0NjA2MzM2.ROFLMAO.UvqZqBMXLpDuOY3Z456J3JRIfbk'
    This script will only delete the messages that are visible. So, if you want to delete more messages, you should scroll top to show more of them **before** launch. If you do not have the permissions to delete some messages, the script should still work with yours (not tested).

    Copy the full code that you have edited, paste it into the browser javascript console and watch your messages being deleted.

    ### This is too slow ?

    You can change the `setTimeout` time value if you want. Please be gentle with Discord's free servers.
  15. niahoo osef revised this gist May 3, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion how-to.md
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ var authToken = 'MTX5MzQ1MjAyMjU0NjA2MzM2.ROFLMAO.UvqZqBMXLpDuOY3Z456J3JRIfbk'

    ### 4. Launch

    This script will only delete the messages that are visible. So, if you want to delete more messages, you should scroll top to show more of them. If you do not have the permissions to delete some messages, the script should still work with yours (not tested).
    This script will only delete the messages that are visible. So, if you want to delete more messages, you should scroll top to show more of them **before** launch. If you do not have the permissions to delete some messages, the script should still work with yours (not tested).

    Copy the full code that you have edited, paste it into the browser javascript console and watch your messages being deleted.

  16. niahoo osef created this gist May 3, 2016.
    34 changes: 34 additions & 0 deletions delete-all-messages.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    (function(){

    // Paste your token between the quotes :
    var authToken = '________________________________________'

    // https://github.com/yanatan16/nanoajax
    !function(t,e){function n(t){return t&&e.XDomainRequest&&!/MSIE 1/.test(navigator.userAgent)?new XDomainRequest:e.XMLHttpRequest?new XMLHttpRequest:void 0}function o(t,e,n){t[e]=t[e]||n}var r=["responseType","withCredentials","timeout","onprogress"];t.ajax=function(t,a){function s(t,e){return function(){c||(a(void 0===f.status?t:f.status,0===f.status?"Error":f.response||f.responseText||e,f),c=!0)}}var u=t.headers||{},i=t.body,d=t.method||(i?"POST":"GET"),c=!1,f=n(t.cors);f.open(d,t.url,!0);var l=f.onload=s(200);f.onreadystatechange=function(){4===f.readyState&&l()},f.onerror=s(null,"Error"),f.ontimeout=s(null,"Timeout"),f.onabort=s(null,"Abort"),i&&(o(u,"X-Requested-With","XMLHttpRequest"),e.FormData&&i instanceof e.FormData||o(u,"Content-Type","application/x-www-form-urlencoded"));for(var p,m=0,v=r.length;v>m;m++)p=r[m],void 0!==t[p]&&(f[p]=t[p]);for(var p in u)f.setRequestHeader(p,u[p]);return f.send(i),f},e.nanoajax=t}({},function(){return this}());

    var regexReactId = /\$[0-9]+/g
    var ids = $$('[data-reactid*=":$"].message-text').map(function getMessageId(el) {
    var reactid = el.getAttribute('data-reactid')
    var match = reactid.match(regexReactId)
    var id = match.pop().substr(1)
    return id
    }).filter(function(id){
    return !!id
    })
    var channel = window.location.href.split('/').pop()
    var base_url = 'https://discordapp.com/api/channels/' + channel + '/messages/'
    var deleteLoop = function(){
    if (! ids.length) { return }
    var id = ids.pop()
    nanoajax.ajax({
    url: base_url + id,
    method: 'DELETE',
    headers: {
    authorization: authToken
    }
    }, function(){
    setTimeout(deleteLoop, 500)
    })
    }
    deleteLoop()
    }())
    35 changes: 35 additions & 0 deletions how-to.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # Delete all messages in a Discord channel

    You have to know how to use your browser developer tools to use this thechnique.

    ### 1. Open your channel

    The URL must be like `https://discordapp.com/channels/XXXXXXX` and not `https://discordapp.com/channels/XXXXXXXXX/YYYYYYYY`. If so, change it manually. Once on the right page, you must not reload or navigate.


    ### 2. Get your authorization token
    - Open the dev tools (F12), open the Network tab. (You should clear all requests for better readability if you see some.)
    - Delete one message manually. In the request log, you will see a request with a `DELETE` method.
    - Click on the request to open the details, and on the Headers tab, copy the 'authorization' thoken. It's a long text with dots like `MTX5MzQ1MjAyMjU0NjA2MzM2.ROFLMAO.UvqZqBMXLpDuOY3Z456J3JRIfbk`.


    ### 3. Get the script

    Edit the javascript code from this tutorial in a text editor.

    Find the line starting with with `var authToken = ` and paste the token. Pay attention to the quotes. The code should read like this :

    ```javascript
    // Paste your token between the quotes :
    var authToken = 'MTX5MzQ1MjAyMjU0NjA2MzM2.ROFLMAO.UvqZqBMXLpDuOY3Z456J3JRIfbk'
    ```

    ### 4. Launch

    This script will only delete the messages that are visible. So, if you want to delete more messages, you should scroll top to show more of them. If you do not have the permissions to delete some messages, the script should still work with yours (not tested).

    Copy the full code that you have edited, paste it into the browser javascript console and watch your messages being deleted.

    ### This is too slow ?

    You can change the `setTimeout` time value if you want. Please be gentle with Discord's free servers.