Discord Status Animator (Manual edit/Non-UI)

Automatically changes your Discord status

目前為 2021-06-14 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Discord Status Animator (Manual edit/Non-UI)
  3. // @namespace https://github.com/Hakorr/status-animator
  4. // @run-at document-start
  5. // @version 1.1
  6. // @description Automatically changes your Discord status
  7. // @author HKR
  8. // @match https://discord.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. //Welcome! Don't be scared by the code, I was too lazy to do an UI for this.
  14. //All you have to edit is the statusanimation function's code (Around the line 40)
  15. var name = "Status Animator";
  16. var version = "V1.1";
  17. var run = true;
  18.  
  19. //A Cookie will be made with this name, feel free to edit it
  20. var cookie_name = "StatusToken";
  21. var delete_cookie_after_a_week = true;
  22. //Your status will be changed to these after you close the Discord tab
  23. var default_status_text = "";
  24. var default_status_emoji = "";
  25. var default_status_state = "online";
  26. //Animation blocks////////////////
  27. /*
  28. - status(emoji,text,state);
  29. -> state = invisible, dnd, idle, online
  30. - await delay(ms);
  31. */async function statusanimation() {
  32. ////////////////////////////////////
  33. status("👐","This","online");
  34. await delay(500);
  35. status("👀","Is","dnd");
  36. await delay(500);
  37. status("😶","A","idle");
  38. await delay(500);
  39. status("✨","Test","invisible");
  40. await delay(500);
  41. status("","");
  42. await delay(2000);
  43. /////////////////////////////
  44. if (run) statusanimation(); }
  45. //Do not edit after this line (If you don't know what you're doing)
  46. ///////////////////////////////////////////////////////////////////
  47. //Function to read the saved cookie
  48. window.getCookie = function(name) {
  49. var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
  50. if (match) return match[2];
  51. }
  52. //Set the Discord Token as a Cookie for future use of the script
  53. //If there is no Token cookie
  54. if(document.cookie.indexOf(cookie_name + "=") == -1) {
  55. //Ask user if they want to refresh the page to get the token
  56. if(confirm("\"" + cookie_name + "\" cookie not found. Refreshing Discord to get it.\n\n- " + name + " " + version)) {
  57. //Load the page again and create a new element which will have the token in its localStorage
  58. location.reload();
  59. var i = document.createElement('iframe');
  60. document.body.appendChild(i);
  61. //Get Token from localStorage
  62. var token = i.contentWindow.localStorage.token
  63. token = token.slice(1, -1);
  64. //Delete cookie after a week or not
  65. if(delete_cookie_after_a_week)
  66. document.cookie = cookie_name + "=" + token + "; secure=true; max-age=604800; path=/";
  67. else
  68. document.cookie = cookie_name + "=" + token + "; secure=true; path=/";
  69.  
  70. } else throw new Error("[Not an actually uncaught] User stopped the Status Animator. \n\nNo cookie was found and user decided not to continue.");
  71. }
  72. var status_text = "";
  73. var status_emoji = "";
  74. var status_state = "";
  75. //Function that changes the status variables (Saves up a bit space)
  76. function status(emoji,text,state) {
  77. if(run) {
  78. status_text = text;
  79. status_emoji = emoji;
  80. status_state = state;
  81. setstatus();
  82. }
  83. }
  84. //Get Discord Token from saved Cookie
  85. var token = getCookie(cookie_name);
  86.  
  87. //HTTP Request's URL address
  88. var url = "https://discord.com/api/v9/users/@me/settings";
  89. //Function that handles the HTTP request for the status change
  90. function setstatus() {
  91.  
  92. var request = new XMLHttpRequest();
  93. request.open("PATCH", url);
  94. request.setRequestHeader("Accept", "*/*" );
  95. request.setRequestHeader("Content-Type", "application/json");
  96. request.setRequestHeader("Authorization", token);
  97. request.send(JSON.stringify({"custom_status":{"text":status_text,"emoji_name":status_emoji}}));
  98.  
  99. //If the request failed
  100. request.onreadystatechange = () => {
  101. if (request.status != 200) {
  102. run = false;
  103. throw new Error("[Not an actually uncaught] Failed to update status. \n\nThe HTTP request failed. Most likely because the authorization token is incorrect.");
  104. }
  105. };
  106. if(status_state == "invisible" || status_state == "dnd" || status_state == "idle" || status_state == "online") {
  107. var request2 = new XMLHttpRequest();
  108. request2.open("PATCH", url);
  109. request2.setRequestHeader("Accept", "*/*" );
  110. request2.setRequestHeader("Content-Type", "application/json");
  111. request2.setRequestHeader("Authorization", token);
  112. request2.send(JSON.stringify({"status":status_state}));
  113. //If the request failed
  114. request2.onreadystatechange = () => {
  115. if (request2.status != 200) {
  116. run = false;
  117. throw new Error("[Not an actually uncaught] Failed to update status. \n\nThe HTTP request failed. Most likely because the authorization token is incorrect.");
  118. }
  119. };
  120. }
  121. }
  122. //Simple delay function for animation
  123. function delay(t) {
  124. return new Promise(function(resolve) {
  125. setTimeout(resolve, t)
  126. });
  127. }
  128. //Start the animation for the first time
  129. if (run) statusanimation();
  130. //Edit (Clear by default) status before exiting
  131. window.onbeforeunload = function () {
  132. run = false;
  133. status_text = default_status_text;
  134. status_emoji = default_status_emoji;
  135. if(status_state == "invisible" || status_state == "dnd" || status_state == "idle" || status_state == "online")
  136. status_state = default_status_state;
  137. setstatus();
  138. return "";
  139. };
  140. })();