Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. (function() {
  2. 'use strict';
  3.  
  4. let messageID = "000000000000000000";
  5.  
  6. let authToken = "";
  7. const channelID = window.location.href.spliat('/').pop(); //Get our channel ID from the current window's URL
  8. const frame = document.body.appendChild(document.createElement("iframe")); //Create an iframe to get the IDs we need from localStorage
  9. const cloneLS = JSON.parse(JSON.stringify(frame.contentWindow.localStorage)); //Make a copy of the iframe's localStorage
  10. frame.parentNode.removeChild(frame); //Remove the iframe now that we no longer need it
  11. const userID = cloneLS.user_id_cache.replace(/"/g, ""); //Get our user ID from localStorage
  12.  
  13. if (authToken === "YOUR_AUTH_TOKEN") { //Check if the auth token was filled in
  14. if (!cloneLS.hasOwnProperty('token')) { //Check if our auth token exists in localStorage
  15. 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.");
  16. return; //We don't have any auth token to use so we end the process.
  17. }
  18. authToken = cloneLS.token.replace(/"/g, ""); //Get our auth token from localStorage
  19. }
  20. console.log("Your Discord account's auth token is:\n" + authToken);
  21.  
  22. let msgCount = 0; //Keeps track of how many messages we find
  23. const interval = 500; //The amount of time to wait in-between deleting messages (default "safe" value is 500)
  24.  
  25. let delay = (duration) => {
  26. return new Promise((resolve, reject) => {
  27. setTimeout(() => resolve(), duration);
  28. });
  29. }
  30.  
  31. let clearMessages = () => {
  32. const baseURL = "https://discordapp.com/api/channels/" + channelID + "/messages";
  33. const headers = {
  34. "Authorization": authToken
  35. };
  36.  
  37. let clock = 0;
  38.  
  39. window.fetch(baseURL + "?before=" + messageID, {
  40. headers,
  41. method: 'GET'
  42. }) //Fetch the message data from discord
  43. .then((resp) => resp.json()) //Make it into a json
  44. .then((messages) => { //Call that json "messages" and do this function with it as the parameter:
  45. if (typeof messages === "undefined" || !messages.hasOwnProperty('length')) {
  46. window.alert("Failed to retrieve messages! Try refreshing the page, then running the script again.");
  47. return true;
  48. } else if (messages.length === 0) {
  49. window.alert("All your messages have been deleted!\nTotal Messages Deleted: " + msgCount);
  50. return true;
  51. }
  52. return Promise.all(messages.map(
  53. (message) => { //Call this function for all messages we have
  54. messageID = message.id; //Update our message ID
  55. if (message.author.id === userID) { //Checks to see if message is yours
  56. msgCount++;
  57. const msgNumber = msgCount; //Remember the count for this message for logging purposes
  58. console.log("Found message #" + msgNumber);
  59. return delay(clock += interval)
  60. .then(() => {
  61. console.log("Deleting message " + msgNumber + "/" + msgCount);
  62. fetch(`${baseURL}/${message.id}`, {
  63. headers,
  64. method: 'DELETE'
  65. });
  66. });
  67. } else { //If the message is not yours, we skip it.
  68. console.log("Skipped message from other user.");
  69. return;
  70. //Chrome's console groups up repeated logs. If this prints out 3 times, it'll say:
  71. //"(3) Skipped message from other user". You can add a variable to track how many
  72. //messages it skips and log the count, but beware it will spam your console log.
  73. }
  74. }));
  75. })
  76. .then((isFinished) => {
  77. if (isFinished === true) { //Check to see if we are finished deleting messages.
  78. return; //We finished deleting all our messages, so we end the process!
  79. }
  80. clearMessages(); //Once we've deleted all the messages we can see, we ask for more!
  81. });
  82. }
  83. clearMessages();
  84. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement