Advertisement
Meadhelm

Untitled

Jun 27th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Discord Integrated Chat Box
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Integrated chat box with Discord messaging functionality
  6. // @author       You
  7. // @match        *://*/*
  8. // @grant        GM_addStyle
  9. // @grant        GM_registerMenuCommand
  10. // @grant        GM_getValue
  11. // @grant        GM_setValue
  12. // @grant        GM_xmlhttpRequest
  13. // ==/UserScript==
  14.  
  15. (function() {
  16.     'use strict';
  17.  
  18.     const DISCORD_TOKEN = 'TOKEN';
  19.     const CHANNEL_ID = 'ID';
  20.     const PROXY_URL = 'https://corsproxy.io/?'; // Updated CORS proxy URL
  21.     const MESSAGE_ENDPOINT = `${PROXY_URL}${encodeURIComponent(`https://discord.com/api/v10/channels/${CHANNEL_ID}/messages`)}`;
  22.  
  23.     // Function to generate or retrieve unique ID
  24.     function getOrCreateUniqueId() {
  25.         let uniqueId = localStorage.getItem('uniqueId');
  26.         if (!uniqueId) {
  27.             uniqueId = generateUniqueId();
  28.             localStorage.setItem('uniqueId', uniqueId);
  29.         }
  30.         return uniqueId;
  31.     }
  32.  
  33.     // Function to generate a UUID
  34.     function generateUniqueId() {
  35.         return Math.random().toString(36).substr(2, 9); // Example of generating a random unique ID
  36.     }
  37.  
  38.     // Function to retrieve or set username
  39.     function getUsername() {
  40.         let username = localStorage.getItem('username');
  41.         if (!username) {
  42.             username = 'User' + getOrCreateUniqueId().substring(0, 5); // Default username based on uniqueId
  43.             localStorage.setItem('username', username);
  44.         }
  45.         return username;
  46.     }
  47.  
  48.     // Function to retrieve or set user color
  49.     function getUserColor() {
  50.         const basicColors = [
  51.             { name: 'Red', code: '#ff0000' },
  52.             { name: 'Orange', code: '#ff8000' },
  53.             { name: 'Yellow', code: '#ffff00' },
  54.             { name: 'Green', code: '#00ff00' },
  55.             { name: 'Cyan', code: '#00ffff' },
  56.             { name: 'Blue', code: '#0000ff' },
  57.             { name: 'Purple', code: '#8000ff' },
  58.             { name: 'Pink', code: '#ff00ff' }
  59.         ];
  60.  
  61.         let userColor = localStorage.getItem('userColor');
  62.         if (!userColor || !basicColors.map(color => color.code).includes(userColor)) {
  63.             userColor = basicColors[Math.floor(Math.random() * basicColors.length)].code; // Default random basic color
  64.             localStorage.setItem('userColor', userColor);
  65.         }
  66.         return userColor;
  67.     }
  68.  
  69.     // Retrieve or generate a unique ID for the user
  70.     let uniqueId = getOrCreateUniqueId();
  71.  
  72.     // Retrieve or set default username and color
  73.     let username = getUsername();
  74.     let userColor = getUserColor();
  75.  
  76.     // Function to update username
  77.     function updateUsername(newUsername) {
  78.         const currentTime = Date.now();
  79.         if (currentTime - GM_getValue('lastUsernameChange', 0) >= 24 * 60 * 60 * 1000) {
  80.             if (newUsername && newUsername !== username) {
  81.                 username = newUsername;
  82.                 localStorage.setItem('username', username);
  83.                 GM_setValue('lastUsernameChange', currentTime);
  84.             }
  85.         } else {
  86.             alert('You can only change your username once every 24 hours.');
  87.         }
  88.     }
  89.  
  90.     // Function to update user color
  91.     function updateUserColor(newColor) {
  92.         const currentTime = Date.now();
  93.         if (currentTime - GM_getValue('lastColorChange', 0) >= 24 * 60 * 60 * 1000) {
  94.             const basicColors = [
  95.                 { name: 'Red', code: '#ff0000' },
  96.                 { name: 'Orange', code: '#ff8000' },
  97.                 { name: 'Yellow', code: '#ffff00' },
  98.                 { name: 'Green', code: '#00ff00' },
  99.                 { name: 'Cyan', code: '#00ffff' },
  100.                 { name: 'Blue', code: '#0000ff' },
  101.                 { name: 'Purple', code: '#8000ff' },
  102.                 { name: 'Pink', code: '#ff00ff' }
  103.             ];
  104.             if (newColor && basicColors.map(color => color.code).includes(newColor)) {
  105.                 userColor = newColor;
  106.                 localStorage.setItem('userColor', userColor);
  107.                 GM_setValue('lastColorChange', currentTime);
  108.             } else {
  109.                 alert('Invalid color selection. Please choose from the provided basic colors.');
  110.             }
  111.         } else {
  112.             alert('You can only change your color once every 24 hours.');
  113.         }
  114.     }
  115.  
  116.     // Create chat box
  117.     var chatBox = document.createElement('div');
  118.     chatBox.id = `discord-chat-box-${uniqueId}`;
  119.     chatBox.style.position = 'fixed';
  120.     chatBox.style.bottom = '10vh';
  121.     chatBox.style.right = '5vw';
  122.     chatBox.style.width = 'calc(90vw - 10px)';
  123.     chatBox.style.maxWidth = '300px';
  124.     chatBox.style.backgroundColor = '#222';
  125.     chatBox.style.borderRadius = '20px';
  126.     chatBox.style.padding = '0';
  127.     chatBox.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.1)';
  128.     chatBox.style.zIndex = '9999';
  129.     chatBox.style.display = 'flex';
  130.     chatBox.style.flexDirection = 'column';
  131.  
  132.     // Draggable functionality
  133.     var isDragging = false;
  134.     var initialX, initialY;
  135.     var offsetX = 0;
  136.     var offsetY = 0;
  137.     var selectedForArrowKeys = false;
  138.  
  139.     chatBox.addEventListener('mousedown', startDragging);
  140.     chatBox.addEventListener('touchstart', startDragging);
  141.     chatBox.addEventListener('click', selectForArrowKeys);
  142.  
  143.     document.addEventListener('click', function(event) {
  144.         if (!chatBox.contains(event.target)) {
  145.             selectedForArrowKeys = false;
  146.         }
  147.     });
  148.  
  149.     document.addEventListener('keydown', handleArrowKeys);
  150.  
  151.     function startDragging(event) {
  152.         if (event.target === chatInput) {
  153.             return;
  154.         }
  155.         event.preventDefault();
  156.         if (event.type === 'mousedown') {
  157.             initialX = event.clientX - offsetX;
  158.             initialY = event.clientY - offsetY;
  159.         } else if (event.type === 'touchstart') {
  160.             initialX = event.touches[0].clientX - offsetX;
  161.             initialY = event.touches[0].clientY - offsetY;
  162.         }
  163.         isDragging = true;
  164.         chatBox.classList.add('chat-dragging');
  165.  
  166.         document.addEventListener('mousemove', drag);
  167.         document.addEventListener('touchmove', drag);
  168.  
  169.         document.addEventListener('mouseup', stopDragging);
  170.         document.addEventListener('touchend', stopDragging);
  171.     }
  172.  
  173.     function drag(event) {
  174.         event.preventDefault();
  175.         if (isDragging) {
  176.             var currentX, currentY;
  177.             if (event.type === 'mousemove') {
  178.                 currentX = event.clientX - initialX;
  179.                 currentY = event.clientY - initialY;
  180.             } else if (event.type === 'touchmove') {
  181.                 currentX = event.touches[0].clientX - initialX;
  182.                 currentY = event.touches[0].clientY - initialY;
  183.             }
  184.  
  185.             offsetX = currentX;
  186.             offsetY = currentY;
  187.  
  188.             chatBox.style.transform = `translate(${currentX}px, ${currentY}px)`;
  189.         }
  190.     }
  191.  
  192.     function stopDragging() {
  193.         isDragging = false;
  194.         chatBox.classList.remove('chat-dragging');
  195.  
  196.         document.removeEventListener('mousemove', drag);
  197.         document.removeEventListener('touchmove', drag);
  198.  
  199.         document.removeEventListener('mouseup', stopDragging);
  200.         document.removeEventListener('touchend', stopDragging);
  201.     }
  202.  
  203.     function handleArrowKeys(event) {
  204.         if (!selectedForArrowKeys) {
  205.             return;
  206.         }
  207.         var step = 10;
  208.         switch (event.key) {
  209.             case 'ArrowUp':
  210.                 offsetY -= step;
  211.                 break;
  212.             case 'ArrowDown':
  213.                 offsetY += step;
  214.                 break;
  215.             case 'ArrowLeft':
  216.                 offsetX -= step;
  217.                 break;
  218.             case 'ArrowRight':
  219.                 offsetX += step;
  220.                 break;
  221.         }
  222.         chatBox.style.transform = `translate(${offsetX}px, ${offsetY}px)`;
  223.     }
  224.  
  225.     function selectForArrowKeys(event) {
  226.         selectedForArrowKeys = true;
  227.     }
  228.  
  229.     // Title with rounded corners
  230.     var chatTitle = document.createElement('div');
  231.     chatTitle.style.background = '#444';
  232.     chatTitle.style.color = 'white';
  233.     chatTitle.style.padding = '10px';
  234.     chatTitle.style.textAlign = 'center';
  235.     chatTitle.style.fontWeight = 'bold';
  236.     chatTitle.style.borderTopLeftRadius = '20px';
  237.     chatTitle.style.borderTopRightRadius = '20px';
  238.     chatTitle.textContent = 'Acellus Chat:';
  239.     chatBox.appendChild(chatTitle);
  240.  
  241.     // Collapse Button
  242.     var collapseButton = document.createElement('button');
  243.     collapseButton.textContent = '▼';
  244.     collapseButton.style.position = 'absolute';
  245.     collapseButton.style.top = '0';
  246.     collapseButton.style.right = '0';
  247.     collapseButton.style.margin = '10px';
  248.     collapseButton.style.width = '30px';
  249.     collapseButton.style.height = '30px';
  250.     collapseButton.style.fontSize = '20px';
  251.     collapseButton.style.border = 'none';
  252.     collapseButton.style.backgroundColor = '#444';
  253.     collapseButton.style.color = 'white';
  254.     collapseButton.style.cursor = 'pointer';
  255.     collapseButton.style.outline = 'none';
  256.     collapseButton.style.borderRadius = '50%';
  257.  
  258.     // Add click event listener
  259.     collapseButton.addEventListener('click', function(event) {
  260.         toggleChatBox(event);
  261.     });
  262.  
  263.     // Add touch event listener
  264.     collapseButton.addEventListener('touchstart', function(event) {
  265.         toggleChatBox(event);
  266.     });
  267.  
  268.     chatBox.appendChild(collapseButton);
  269.  
  270.     // Toggle chat box visibility
  271.     function toggleChatBox(event) {
  272.         event.stopPropagation();
  273.  
  274.         if (chatBox.style.height === 'auto' || chatBox.style.height === '') {
  275.             chatBox.style.height = '70px'; // Set to a fixed height
  276.             collapseButton.textContent = '▲';
  277.             chatInput.style.display = 'none'; // Hide the input field
  278.         } else {
  279.             chatBox.style.height = 'auto';
  280.             collapseButton.textContent = '▼';
  281.             chatInput.style.display = 'block'; // Show the input field
  282.         }
  283.     }
  284.  
  285.     // Chat history container
  286.     var chatHistory = document.createElement('div');
  287.     chatHistory.id = `chat-history-${uniqueId}`;
  288.     chatHistory.style.height = 'calc(50vh - 110px)';
  289.     chatHistory.style.overflowY = 'auto';
  290.     chatHistory.style.backgroundColor = '#333';
  291.     chatBox.appendChild(chatHistory);
  292.  
  293.     // Function to add message to chat history
  294.     function addMessage(message, isUser) {
  295.         var msgContainer = document.createElement('div');
  296.         msgContainer.classList.add('message');
  297.         msgContainer.style.clear = 'both';
  298.         msgContainer.style.padding = '5px 10px';
  299.         msgContainer.style.color = 'white';
  300.  
  301.         if (isUser) {
  302.             var userLabel = document.createElement('span');
  303.             userLabel.classList.add('user-label');
  304.             userLabel.textContent = `${username}:`;
  305.             userLabel.style.backgroundColor = userColor; // User's color for label
  306.             msgContainer.appendChild(userLabel);
  307.         } else {
  308.             var botLabel = document.createElement('span');
  309.             botLabel.classList.add('bot-label');
  310.             botLabel.textContent = `(Bot):`;
  311.             msgContainer.appendChild(botLabel);
  312.         }
  313.  
  314.         var textNode = document.createElement('span');
  315.         textNode.style.fontWeight = 'bold'; // Bold text
  316.         textNode.innerHTML = ` ${message}`;
  317.         msgContainer.appendChild(textNode);
  318.  
  319.         chatHistory.appendChild(msgContainer);
  320.  
  321.         // Divider
  322.         var divider = document.createElement('hr');
  323.         chatHistory.appendChild(divider);
  324.  
  325.         chatHistory.scrollTop = chatHistory.scrollHeight; // Scroll to bottom
  326.     }
  327.  
  328.     // Input field
  329.     var chatInput = document.createElement('input');
  330.     chatInput.type = 'text';
  331.     chatInput.placeholder = 'Type here...';
  332.     chatInput.style.width = 'calc(100% - 20px)';
  333.     chatInput.style.padding = '10px';
  334.     chatInput.style.margin = '10px auto'; // Centered horizontally
  335.     chatInput.style.borderRadius = '10px';
  336.     chatInput.style.border = 'none';
  337.     chatInput.style.backgroundColor = '#444';
  338.     chatInput.style.color = 'white';
  339.     chatInput.style.zIndex = '10000'; // Ensure input field is above other elements
  340.     chatBox.appendChild(chatInput);
  341.  
  342.     // Listen for Enter key press to send message
  343.     chatInput.addEventListener('keydown', function(event) {
  344.         if (event.key === 'Enter') {
  345.             event.preventDefault();
  346.             sendMessage();
  347.         }
  348.     });
  349.  
  350.     // Function to send message to Discord
  351.     function sendMessage() {
  352.         var expression = chatInput.value.trim();
  353.         if (expression !== '') {
  354.             addMessage(expression, true); // Add user's message to chat history
  355.             sendMessageToDiscord(expression); // Send message to Discord
  356.             chatInput.value = '';
  357.         }
  358.     }
  359.  
  360.     // Example function to send formatted message to Discord
  361.     function sendMessageToDiscord(message) {
  362.         const formattedMessage = `<span style="color: ${userColor};"><strong>${username}:</strong> ${message}</span>`;
  363.         sendToDiscord(formattedMessage);
  364.     }
  365.  
  366.     // Function to send message to Discord using CORS proxy
  367.     function sendToDiscord(message) {
  368.         fetch(MESSAGE_ENDPOINT, {
  369.             method: 'POST',
  370.             headers: {
  371.                 'Content-Type': 'application/json',
  372.                 'Authorization': `Bot ${DISCORD_TOKEN}`
  373.             },
  374.             body: JSON.stringify({
  375.                 content: message,
  376.             }),
  377.         })
  378.         .then(response => {
  379.             if (!response.ok) {
  380.                 console.error('Failed to send message to Discord:', response.statusText);
  381.             } else {
  382.                 console.log('Message sent successfully to Discord:', message);
  383.             }
  384.         })
  385.         .catch(error => {
  386.             console.error('Error sending message to Discord:', error);
  387.         });
  388.     }
  389.  
  390.     // CSS styles for user and bot messages
  391.     GM_addStyle(`
  392.         .message {
  393.             clear: both;
  394.             padding: 5px 10px;
  395.             color: white;
  396.         }
  397.         .bot-label {
  398.             float: left;
  399.             margin-left: 5px;
  400.             background-color: blue;
  401.             color: white;
  402.             border-radius: 10px;
  403.             padding: 3px 6px;
  404.         }
  405.         .user-label {
  406.             float: left;
  407.             margin-left: 5px;
  408.             border-radius: 10px;
  409.             padding: 3px 6px;
  410.         }
  411.         hr {
  412.             border-top: 1px solid white;
  413.             margin: 5px 0;
  414.         }
  415.         .chat-dragging * {
  416.             /* Removed user-select: none; to enable touch interactions */
  417.         }
  418.     `);
  419.  
  420.     // Add username change button to Tampermonkey dashboard
  421.     GM_registerMenuCommand('Change Username', function() {
  422.         var newUsername = prompt('Enter your new username:');
  423.         if (newUsername) {
  424.             updateUsername(newUsername);
  425.         }
  426.     });
  427.  
  428.     // Add user color change button to Tampermonkey dashboard
  429.     GM_registerMenuCommand('Change User Color', function() {
  430.         const colorOptions = [
  431.             'Red', 'Orange', 'Yellow', 'Green', 'Cyan', 'Blue', 'Purple', 'Pink'
  432.         ].join('\n');
  433.         const selectedColor = prompt(`Choose your new color:\n${colorOptions}`, basicColors.find(color => color.code === userColor).name);
  434.         if (selectedColor) {
  435.             const newColor = basicColors.find(color => color.name === selectedColor).code;
  436.             updateUserColor(newColor);
  437.         }
  438.     });
  439.  
  440.     // Append chatBox to body
  441.     document.body.appendChild(chatBox);
  442.  
  443. })();
  444.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement