Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. //Browser Script to Delete All Your Messages in a Discord Channel
  2. //Updated by z861gz6wb2 ~ Working as of March 20th, 2019.
  3. //Credit to: Altoids1 (Original code), GotEnouth (Updated code), TheOutride (Updated code).
  4. (function() {
  5. 'use strict';
  6.  
  7. /*INSTRUCTIONS FOR THIS SCRIPT ARE BELOW*/
  8.  
  9. let messageID = "000000000000000000"; //The ID of the message you would like to delete.
  10. //Note: you can leave the messageID set to "000000000000000000",
  11. // if you want to delete every single message in the channel.
  12.  
  13. let authToken = "YOUR_AUTH_TOKEN"; //Your Discord account's auth token.
  14. //Note: this script will attempt to grab your auth token automatically,
  15. //but it may fail depending on the browser you are using,
  16. //in which case you should paste it in manually into the line above,
  17. //by using the script from the Q&A linked below to grab it.
  18.  
  19. //HOW TO USE THIS SCRIPT:
  20. //Step 1: Login to the web browser version of Discord,
  21. // and find the most recent message you want to delete.
  22. //Step 2: Copy the ID of that message and paste it into the line above.
  23. // All messages sent prior to that message will be deleted.
  24. //Step 3: Paste this script into your browser's console.
  25. //Step 4: ???
  26. //Step 5: Profit.
  27.  
  28. //Q: Where can I find my Message ID?
  29. //A: Visit: https://support.discordapp.com/hc/en-us/articles/206346498
  30. //Q: Where can I find my Auth Token?
  31. //A: Use this: https://github.com/FOCI-DEV/Get-Discord-Token
  32. //Q: How can I turn this script into a bookmark?
  33. //A: Paste it into this tool: https://chriszarate.github.io/bookmarkleter/
  34.  
  35. /*DO NOT MODIFY ANYTHING BELOW THIS LINE! (unless you know what you're doing)*/
  36. const channelID = window.location.href.split('/').pop(); //Get our channel ID from the current window's URL
  37. const frame = document.body.appendChild(document.createElement("iframe")); //Create an iframe to get the IDs we need from localStorage
  38. const cloneLS = JSON.parse(JSON.stringify(frame.contentWindow.localStorage)); //Make a copy of the iframe's localStorage
  39. frame.parentNode.removeChild(frame); //Remove the iframe now that we no longer need it
  40. const userID = cloneLS.user_id_cache.replace(/"/g, ""); //Get our user ID from localStorage
  41.  
  42. if (authToken === "YOUR_AUTH_TOKEN") { //Check if the auth token was filled in
  43. if (!cloneLS.hasOwnProperty('token')) { //Check if our auth token exists in localStorage
  44. window.alert("Failed to retrieve your auth token from localStorage, try pasting it into this script manually.\nInstructions to find your auth token are provided in this script.");
  45. return; //We don't have any auth token to use so we end the process.
  46. }
  47. authToken = cloneLS.token.replace(/"/g, ""); //Get our auth token from localStorage
  48. }
  49. console.log("Your Discord account's auth token is:\n" + authToken);
  50.  
  51. let msgCount = 0; //Keeps track of how many messages we find
  52. const interval = 500; //The amount of time to wait in-between deleting messages (default "safe" value is 500)
  53.  
  54. let delay = (duration) => {
  55. return new Promise((resolve, reject) => {
  56. setTimeout(() => resolve(), duration);
  57. });
  58. }
  59.  
  60. let clearMessages = () => {
  61. const baseURL = "https://discordapp.com/api/channels/" + channelID + "/messages";
  62. const headers = {
  63. "Authorization": authToken
  64. };
  65.  
  66. let clock = 0;
  67.  
  68. window.fetch(baseURL + "?before=" + messageID, {
  69. headers,
  70. method: 'GET'
  71. }) //Fetch the message data from discord
  72. .then((resp) => resp.json()) //Make it into a json
  73. .then((messages) => { //Call that json "messages" and do this function with it as the parameter:
  74. if (typeof messages === "undefined" || !messages.hasOwnProperty('length')) {
  75. window.alert("Failed to retrieve messages! Try refreshing the page, then running the script again.");
  76. return true;
  77. } else if (messages.length === 0) {
  78. window.alert("All your messages have been deleted!\nTotal Messages Deleted: " + msgCount);
  79. return true;
  80. }
  81. return Promise.all(messages.map(
  82. (message) => { //Call this function for all messages we have
  83. messageID = message.id; //Update our message ID
  84. if (message.author.id === userID) { //Checks to see if message is yours
  85. msgCount++;
  86. const msgNumber = msgCount; //Remember the count for this message for logging purposes
  87. console.log("Found message #" + msgNumber);
  88. return delay(clock += interval)
  89. .then(() => {
  90. console.log("Deleting message " + msgNumber + "/" + msgCount);
  91. fetch(`${baseURL}/${message.id}`, {
  92. headers,
  93. method: 'DELETE'
  94. });
  95. });
  96. } else { //If the message is not yours, we skip it.
  97. console.log("Skipped message from other user.");
  98. return;
  99. //Chrome's console groups up repeated logs. If this prints out 3 times, it'll say:
  100. //"(3) Skipped message from other user". You can add a variable to track how many
  101. //messages it skips and log the count, but beware it will spam your console log.
  102. }
  103. }));
  104. })
  105. .then((isFinished) => {
  106. if (isFinished === true) { //Check to see if we are finished deleting messages.
  107. return; //We finished deleting all our messages, so we end the process!
  108. }
  109. clearMessages(); //Once we've deleted all the messages we can see, we ask for more!
  110. });
  111. }
  112. clearMessages();
  113. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement