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.2
  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. let channelId = '1320740083552358410'; // 初期チャンネルID
  18.  
  19. const initialWidth = '280px';
  20. const initialHeight = '250px';
  21.  
  22. const container = document.createElement('div');
  23. container.style.position = 'fixed';
  24. container.style.bottom = '10px';
  25. container.style.left = '10px';
  26. container.style.backgroundColor = '#2f3136';
  27. container.style.color = '#ffffff';
  28. container.style.padding = '10px';
  29. container.style.borderRadius = '5px';
  30. container.style.zIndex = '1000';
  31. container.style.width = initialWidth;
  32. container.style.height = initialHeight;
  33. document.body.appendChild(container);
  34.  
  35. makeElementDraggable(container);
  36.  
  37. const tokenBox = document.createElement('textarea');
  38. tokenBox.placeholder = 'Enter your token';
  39. tokenBox.style.width = '100%';
  40. tokenBox.style.height = '40px';
  41. tokenBox.style.resize = 'none';
  42. tokenBox.style.backgroundColor = '#000000';
  43. tokenBox.style.color = '#00FF00';
  44. container.appendChild(tokenBox);
  45.  
  46. const inputBox = document.createElement('textarea');
  47. inputBox.placeholder = 'Enter your secret message to aarr';
  48. inputBox.style.width = '100%';
  49. inputBox.style.height = '100px';
  50. inputBox.style.resize = 'none';
  51. inputBox.style.backgroundColor = '#000000';
  52. inputBox.style.color = '#00FF00';
  53. container.appendChild(inputBox);
  54.  
  55. const button = document.createElement('button');
  56. button.innerText = 'Send DM';
  57. button.style.marginTop = '10px';
  58. button.style.width = '100%';
  59. button.style.backgroundColor = '#575757';
  60. button.style.color = '#ffffff';
  61. button.style.border = 'none';
  62. button.style.borderRadius = '3px';
  63. button.style.cursor = 'pointer';
  64. container.appendChild(button);
  65.  
  66. const buttonContainer = document.createElement('div');
  67. buttonContainer.style.marginTop = '10px';
  68. buttonContainer.style.display = 'flex';
  69. buttonContainer.style.justifyContent = 'space-between';
  70.  
  71. const channelButton1 = document.createElement('button');
  72. channelButton1.innerText = '1荒らし雑談';
  73. channelButton1.style.width = '30%';
  74. channelButton1.style.backgroundColor = '#575757';
  75. channelButton1.style.color = '#ffffff';
  76. channelButton1.style.border = 'none';
  77. channelButton1.style.borderRadius = '3px';
  78. channelButton1.style.cursor = 'pointer';
  79. channelButton1.addEventListener('click', () => {
  80. channelId = '1320740083552358410';
  81. updateButtonStyles(channelButton1);
  82. });
  83.  
  84. const channelButton2 = document.createElement('button');
  85. channelButton2.innerText = '2情勢雑談';
  86. channelButton2.style.width = '30%';
  87. channelButton2.style.backgroundColor = '#575757';
  88. channelButton2.style.color = '#ffffff';
  89. channelButton2.style.border = 'none';
  90. channelButton2.style.borderRadius = '3px';
  91. channelButton2.style.cursor = 'pointer';
  92. channelButton2.addEventListener('click', () => {
  93. channelId = '1320740430924611665';
  94. updateButtonStyles(channelButton2);
  95. });
  96.  
  97. const channelButton3 = document.createElement('button');
  98. channelButton3.innerText = '3依頼版';
  99. channelButton3.style.width = '30%';
  100. channelButton3.style.backgroundColor = '#575757';
  101. channelButton3.style.color = '#ffffff';
  102. channelButton3.style.border = 'none';
  103. channelButton3.style.borderRadius = '3px';
  104. channelButton3.style.cursor = 'pointer';
  105. channelButton3.addEventListener('click', () => {
  106. channelId = '1320740533886517280';
  107. updateButtonStyles(channelButton3);
  108. });
  109.  
  110. buttonContainer.appendChild(channelButton1);
  111. buttonContainer.appendChild(channelButton2);
  112. buttonContainer.appendChild(channelButton3);
  113. container.appendChild(buttonContainer);
  114.  
  115. function updateButtonStyles(activeButton) {
  116. [channelButton1, channelButton2, channelButton3].forEach(button => {
  117. if (button === activeButton) {
  118. button.style.backgroundColor = '#047500';
  119. } else {
  120. button.style.backgroundColor = '#575757';
  121. }
  122. });
  123. }
  124.  
  125. inputBox.value = GM_getValue('inputBoxValue', '');
  126. tokenBox.value = GM_getValue('tokenBoxValue', '');
  127.  
  128. async function sendDM() {
  129. const token = tokenBox.value.trim();
  130. if (!token) {
  131. alert('Token is required');
  132. return;
  133. }
  134.  
  135. const message = inputBox.value.trim();
  136. if (!message) {
  137. alert('Message cannot be empty');
  138. return;
  139. }
  140.  
  141. tokenBox.style.display = 'none';
  142. GM_setValue('tokenBoxValue', token);
  143.  
  144. const success = await sendMessage(channelId, message, token);
  145. if (success) {
  146. inputBox.value = '';
  147. GM_setValue('inputBoxValue', '');
  148. } else {
  149. alert('Failed to send message');
  150. }
  151. }
  152.  
  153. button.addEventListener('click', sendDM);
  154.  
  155. inputBox.addEventListener('keydown', (event) => {
  156. if (event.key === 'Enter' && !event.shiftKey) {
  157. event.preventDefault();
  158. sendDM();
  159. } else if (event.key === 'Enter' && event.shiftKey) {
  160. event.preventDefault();
  161. const cursorPos = inputBox.selectionStart;
  162. const textBefore = inputBox.value.substring(0, cursorPos);
  163. const textAfter = inputBox.value.substring(cursorPos);
  164. inputBox.value = `${textBefore}\n${textAfter}`;
  165. inputBox.selectionStart = cursorPos + 1;
  166. inputBox.selectionEnd = cursorPos + 1;
  167. }
  168. });
  169.  
  170. inputBox.addEventListener('input', () => {
  171. GM_setValue('inputBoxValue', inputBox.value);
  172. });
  173.  
  174. tokenBox.addEventListener('input', () => {
  175. GM_setValue('tokenBoxValue', tokenBox.value);
  176. });
  177.  
  178. function makeElementDraggable(el) {
  179. el.onmousedown = function(event) {
  180. if (event.target === inputBox || event.target === tokenBox) {
  181. return;
  182. }
  183.  
  184. event.preventDefault();
  185.  
  186. let shiftX = event.clientX - el.getBoundingClientRect().left;
  187. let shiftY = event.clientY - el.getBoundingClientRect().top;
  188.  
  189. function moveAt(pageX, pageY) {
  190. el.style.left = pageX - shiftX + 'px';
  191. el.style.top = pageY - shiftY + 'px';
  192. }
  193.  
  194. function onMouseMove(event) {
  195. moveAt(event.pageX, event.pageY);
  196. }
  197.  
  198. document.addEventListener('mousemove', onMouseMove);
  199.  
  200. el.onmouseup = function() {
  201. document.removeEventListener('mousemove', onMouseMove);
  202. el.onmouseup = null;
  203. };
  204. };
  205.  
  206. el.ondragstart = function() {
  207. return false;
  208. };
  209. }
  210.  
  211. async function sendMessage(channelId, message, token) {
  212. const nonce = generateNonce();
  213. return new Promise((resolve) => {
  214. GM_xmlhttpRequest({
  215. method: 'POST',
  216. url: `https://discord.com/api/v9/channels/${channelId}/messages`,
  217. headers: {
  218. 'Content-Type': 'application/json',
  219. 'Authorization': token
  220. },
  221. data: JSON.stringify({
  222. content: message,
  223. flags: 0,
  224. mobile_network_type: "unknown",
  225. nonce: nonce,
  226. tts: false
  227. }),
  228. onload: (response) => {
  229. resolve(response.status === 200);
  230. },
  231. onerror: () => resolve(false)
  232. });
  233. });
  234. }
  235.  
  236. function generateNonce() {
  237. const now = Date.now();
  238. return `${now}${Math.floor(Math.random() * 1000)}`;
  239. }
  240. })();
  241.