Discord DM Sender with Input Box

Send DM messages via Discord API

当前为 2024-12-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Discord DM Sender with Input Box
  3. // @namespace http://tampermonkey.net/
  4. // @version 1
  5. // @description Send DM messages via Discord API
  6. // @author Your Name
  7. // @match https://discord.com/*
  8. // @grant GM_xmlhttpRequest
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @license You can modify as long as you credit me
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17.  
  18. const channelId = '1312547211074211943';
  19.  
  20.  
  21. const initialWidth = '280px';
  22. const initialHeight = '200px';
  23.  
  24.  
  25. const container = document.createElement('div');
  26. container.style.position = 'fixed';
  27. container.style.bottom = '10px';
  28. container.style.left = '10px';
  29. container.style.backgroundColor = '#2f3136';
  30. container.style.color = '#ffffff';
  31. container.style.padding = '10px';
  32. container.style.borderRadius = '5px';
  33. container.style.zIndex = '1000';
  34. container.style.width = initialWidth;
  35. container.style.height = initialHeight;
  36. document.body.appendChild(container);
  37.  
  38. makeElementDraggable(container);
  39.  
  40.  
  41. const tokenBox = document.createElement('textarea');
  42. tokenBox.placeholder = 'Enter your token';
  43. tokenBox.style.width = '100%';
  44. tokenBox.style.height = '40px';
  45. tokenBox.style.resize = 'none';
  46. tokenBox.style.backgroundColor = '#000000';
  47. tokenBox.style.color = '#00FF00';
  48. container.appendChild(tokenBox);
  49.  
  50.  
  51. const inputBox = document.createElement('textarea');
  52. inputBox.placeholder = 'Enter your secret message to aarr';
  53. inputBox.style.width = '100%';
  54. inputBox.style.height = '100px';
  55. inputBox.style.resize = 'none';
  56. inputBox.style.backgroundColor = '#000000';
  57. inputBox.style.color = '#00FF00';
  58. container.appendChild(inputBox);
  59.  
  60. const button = document.createElement('button');
  61. button.innerText = 'Send DM';
  62. button.style.marginTop = '10px';
  63. button.style.width = '100%';
  64. button.style.backgroundColor = '#7289da';
  65. button.style.color = '#ffffff';
  66. button.style.border = 'none';
  67. button.style.borderRadius = '3px';
  68. button.style.cursor = 'pointer';
  69. container.appendChild(button);
  70.  
  71.  
  72. inputBox.value = GM_getValue('inputBoxValue', '');
  73. tokenBox.value = GM_getValue('tokenBoxValue', '');
  74.  
  75.  
  76. async function sendDM() {
  77. const token = tokenBox.value.trim();
  78. if (!token) {
  79. alert('Token is required');
  80. return;
  81. }
  82.  
  83. const message = inputBox.value.trim();
  84. if (!message) {
  85. alert('Message cannot be empty');
  86. return;
  87. }
  88.  
  89.  
  90. tokenBox.style.display = 'none';
  91. GM_setValue('tokenBoxValue', token);
  92.  
  93. const success = await sendMessage(channelId, message, token);
  94. if (success) {
  95. inputBox.value = '';
  96. GM_setValue('inputBoxValue', '');
  97. } else {
  98. alert('Failed to send message');
  99. }
  100. }
  101.  
  102. button.addEventListener('click', sendDM);
  103.  
  104.  
  105. inputBox.addEventListener('keydown', (event) => {
  106. if (event.key === 'Enter' && !event.shiftKey) {
  107. event.preventDefault();
  108. sendDM();
  109. } else if (event.key === 'Enter' && event.shiftKey) {
  110. event.preventDefault();
  111. const cursorPos = inputBox.selectionStart;
  112. const textBefore = inputBox.value.substring(0, cursorPos);
  113. const textAfter = inputBox.value.substring(cursorPos);
  114. inputBox.value = `${textBefore}\n${textAfter}`;
  115. inputBox.selectionStart = cursorPos + 1;
  116. inputBox.selectionEnd = cursorPos + 1;
  117. }
  118. });
  119.  
  120.  
  121. inputBox.addEventListener('input', () => {
  122. GM_setValue('inputBoxValue', inputBox.value);
  123. });
  124.  
  125. tokenBox.addEventListener('input', () => {
  126. GM_setValue('tokenBoxValue', tokenBox.value);
  127. });
  128.  
  129.  
  130. function makeElementDraggable(el) {
  131. el.onmousedown = function(event) {
  132.  
  133. if (event.target === inputBox || event.target === tokenBox) {
  134. return;
  135. }
  136.  
  137. event.preventDefault();
  138.  
  139. let shiftX = event.clientX - el.getBoundingClientRect().left;
  140. let shiftY = event.clientY - el.getBoundingClientRect().top;
  141.  
  142. function moveAt(pageX, pageY) {
  143. el.style.left = pageX - shiftX + 'px';
  144. el.style.top = pageY - shiftY + 'px';
  145. }
  146.  
  147. function onMouseMove(event) {
  148. moveAt(event.pageX, event.pageY);
  149. }
  150.  
  151. document.addEventListener('mousemove', onMouseMove);
  152.  
  153. el.onmouseup = function() {
  154. document.removeEventListener('mousemove', onMouseMove);
  155. el.onmouseup = null;
  156. };
  157. };
  158.  
  159. el.ondragstart = function() {
  160. return false;
  161. };
  162. }
  163.  
  164.  
  165. async function sendMessage(channelId, message, token) {
  166. const nonce = generateNonce();
  167. return new Promise((resolve) => {
  168. GM_xmlhttpRequest({
  169. method: 'POST',
  170. url: `https://discord.com/api/v9/channels/${channelId}/messages`,
  171. headers: {
  172. 'Content-Type': 'application/json',
  173. 'Authorization': token
  174. },
  175. data: JSON.stringify({
  176. content: message,
  177. flags: 0,
  178. mobile_network_type: "unknown",
  179. nonce: nonce,
  180. tts: false
  181. }),
  182. onload: (response) => {
  183. resolve(response.status === 200);
  184. },
  185. onerror: () => resolve(false)
  186. });
  187. });
  188. }
  189.  
  190.  
  191. function generateNonce() {
  192. const now = Date.now();
  193. return `${now}${Math.floor(Math.random() * 1000)}`;
  194. }
  195. })();