Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Discord Integrated Chat Box
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description Integrated chat box with Discord messaging functionality
- // @author You
- // @match *://*/*
- // @grant GM_addStyle
- // @grant GM_registerMenuCommand
- // @grant GM_getValue
- // @grant GM_setValue
- // @grant GM_xmlhttpRequest
- // ==/UserScript==
- (function() {
- 'use strict';
- const DISCORD_TOKEN = 'TOKEN';
- const CHANNEL_ID = 'ID';
- const PROXY_URL = 'https://corsproxy.io/?'; // Updated CORS proxy URL
- const MESSAGE_ENDPOINT = `${PROXY_URL}${encodeURIComponent(`https://discord.com/api/v10/channels/${CHANNEL_ID}/messages`)}`;
- // Function to generate or retrieve unique ID
- function getOrCreateUniqueId() {
- let uniqueId = localStorage.getItem('uniqueId');
- if (!uniqueId) {
- uniqueId = generateUniqueId();
- localStorage.setItem('uniqueId', uniqueId);
- }
- return uniqueId;
- }
- // Function to generate a UUID
- function generateUniqueId() {
- return Math.random().toString(36).substr(2, 9); // Example of generating a random unique ID
- }
- // Function to retrieve or set username
- function getUsername() {
- let username = localStorage.getItem('username');
- if (!username) {
- username = 'User' + getOrCreateUniqueId().substring(0, 5); // Default username based on uniqueId
- localStorage.setItem('username', username);
- }
- return username;
- }
- // Function to retrieve or set user color
- function getUserColor() {
- const basicColors = [
- { name: 'Red', code: '#ff0000' },
- { name: 'Orange', code: '#ff8000' },
- { name: 'Yellow', code: '#ffff00' },
- { name: 'Green', code: '#00ff00' },
- { name: 'Cyan', code: '#00ffff' },
- { name: 'Blue', code: '#0000ff' },
- { name: 'Purple', code: '#8000ff' },
- { name: 'Pink', code: '#ff00ff' }
- ];
- let userColor = localStorage.getItem('userColor');
- if (!userColor || !basicColors.map(color => color.code).includes(userColor)) {
- userColor = basicColors[Math.floor(Math.random() * basicColors.length)].code; // Default random basic color
- localStorage.setItem('userColor', userColor);
- }
- return userColor;
- }
- // Retrieve or generate a unique ID for the user
- let uniqueId = getOrCreateUniqueId();
- // Retrieve or set default username and color
- let username = getUsername();
- let userColor = getUserColor();
- // Function to update username
- function updateUsername(newUsername) {
- const currentTime = Date.now();
- if (currentTime - GM_getValue('lastUsernameChange', 0) >= 24 * 60 * 60 * 1000) {
- if (newUsername && newUsername !== username) {
- username = newUsername;
- localStorage.setItem('username', username);
- GM_setValue('lastUsernameChange', currentTime);
- }
- } else {
- alert('You can only change your username once every 24 hours.');
- }
- }
- // Function to update user color
- function updateUserColor(newColor) {
- const currentTime = Date.now();
- if (currentTime - GM_getValue('lastColorChange', 0) >= 24 * 60 * 60 * 1000) {
- const basicColors = [
- { name: 'Red', code: '#ff0000' },
- { name: 'Orange', code: '#ff8000' },
- { name: 'Yellow', code: '#ffff00' },
- { name: 'Green', code: '#00ff00' },
- { name: 'Cyan', code: '#00ffff' },
- { name: 'Blue', code: '#0000ff' },
- { name: 'Purple', code: '#8000ff' },
- { name: 'Pink', code: '#ff00ff' }
- ];
- if (newColor && basicColors.map(color => color.code).includes(newColor)) {
- userColor = newColor;
- localStorage.setItem('userColor', userColor);
- GM_setValue('lastColorChange', currentTime);
- } else {
- alert('Invalid color selection. Please choose from the provided basic colors.');
- }
- } else {
- alert('You can only change your color once every 24 hours.');
- }
- }
- // Create chat box
- var chatBox = document.createElement('div');
- chatBox.id = `discord-chat-box-${uniqueId}`;
- chatBox.style.position = 'fixed';
- chatBox.style.bottom = '10vh';
- chatBox.style.right = '5vw';
- chatBox.style.width = 'calc(90vw - 10px)';
- chatBox.style.maxWidth = '300px';
- chatBox.style.backgroundColor = '#222';
- chatBox.style.borderRadius = '20px';
- chatBox.style.padding = '0';
- chatBox.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.1)';
- chatBox.style.zIndex = '9999';
- chatBox.style.display = 'flex';
- chatBox.style.flexDirection = 'column';
- // Draggable functionality
- var isDragging = false;
- var initialX, initialY;
- var offsetX = 0;
- var offsetY = 0;
- var selectedForArrowKeys = false;
- chatBox.addEventListener('mousedown', startDragging);
- chatBox.addEventListener('touchstart', startDragging);
- chatBox.addEventListener('click', selectForArrowKeys);
- document.addEventListener('click', function(event) {
- if (!chatBox.contains(event.target)) {
- selectedForArrowKeys = false;
- }
- });
- document.addEventListener('keydown', handleArrowKeys);
- function startDragging(event) {
- if (event.target === chatInput) {
- return;
- }
- event.preventDefault();
- if (event.type === 'mousedown') {
- initialX = event.clientX - offsetX;
- initialY = event.clientY - offsetY;
- } else if (event.type === 'touchstart') {
- initialX = event.touches[0].clientX - offsetX;
- initialY = event.touches[0].clientY - offsetY;
- }
- isDragging = true;
- chatBox.classList.add('chat-dragging');
- document.addEventListener('mousemove', drag);
- document.addEventListener('touchmove', drag);
- document.addEventListener('mouseup', stopDragging);
- document.addEventListener('touchend', stopDragging);
- }
- function drag(event) {
- event.preventDefault();
- if (isDragging) {
- var currentX, currentY;
- if (event.type === 'mousemove') {
- currentX = event.clientX - initialX;
- currentY = event.clientY - initialY;
- } else if (event.type === 'touchmove') {
- currentX = event.touches[0].clientX - initialX;
- currentY = event.touches[0].clientY - initialY;
- }
- offsetX = currentX;
- offsetY = currentY;
- chatBox.style.transform = `translate(${currentX}px, ${currentY}px)`;
- }
- }
- function stopDragging() {
- isDragging = false;
- chatBox.classList.remove('chat-dragging');
- document.removeEventListener('mousemove', drag);
- document.removeEventListener('touchmove', drag);
- document.removeEventListener('mouseup', stopDragging);
- document.removeEventListener('touchend', stopDragging);
- }
- function handleArrowKeys(event) {
- if (!selectedForArrowKeys) {
- return;
- }
- var step = 10;
- switch (event.key) {
- case 'ArrowUp':
- offsetY -= step;
- break;
- case 'ArrowDown':
- offsetY += step;
- break;
- case 'ArrowLeft':
- offsetX -= step;
- break;
- case 'ArrowRight':
- offsetX += step;
- break;
- }
- chatBox.style.transform = `translate(${offsetX}px, ${offsetY}px)`;
- }
- function selectForArrowKeys(event) {
- selectedForArrowKeys = true;
- }
- // Title with rounded corners
- var chatTitle = document.createElement('div');
- chatTitle.style.background = '#444';
- chatTitle.style.color = 'white';
- chatTitle.style.padding = '10px';
- chatTitle.style.textAlign = 'center';
- chatTitle.style.fontWeight = 'bold';
- chatTitle.style.borderTopLeftRadius = '20px';
- chatTitle.style.borderTopRightRadius = '20px';
- chatTitle.textContent = 'Acellus Chat:';
- chatBox.appendChild(chatTitle);
- // Collapse Button
- var collapseButton = document.createElement('button');
- collapseButton.textContent = '▼';
- collapseButton.style.position = 'absolute';
- collapseButton.style.top = '0';
- collapseButton.style.right = '0';
- collapseButton.style.margin = '10px';
- collapseButton.style.width = '30px';
- collapseButton.style.height = '30px';
- collapseButton.style.fontSize = '20px';
- collapseButton.style.border = 'none';
- collapseButton.style.backgroundColor = '#444';
- collapseButton.style.color = 'white';
- collapseButton.style.cursor = 'pointer';
- collapseButton.style.outline = 'none';
- collapseButton.style.borderRadius = '50%';
- // Add click event listener
- collapseButton.addEventListener('click', function(event) {
- toggleChatBox(event);
- });
- // Add touch event listener
- collapseButton.addEventListener('touchstart', function(event) {
- toggleChatBox(event);
- });
- chatBox.appendChild(collapseButton);
- // Toggle chat box visibility
- function toggleChatBox(event) {
- event.stopPropagation();
- if (chatBox.style.height === 'auto' || chatBox.style.height === '') {
- chatBox.style.height = '70px'; // Set to a fixed height
- collapseButton.textContent = '▲';
- chatInput.style.display = 'none'; // Hide the input field
- } else {
- chatBox.style.height = 'auto';
- collapseButton.textContent = '▼';
- chatInput.style.display = 'block'; // Show the input field
- }
- }
- // Chat history container
- var chatHistory = document.createElement('div');
- chatHistory.id = `chat-history-${uniqueId}`;
- chatHistory.style.height = 'calc(50vh - 110px)';
- chatHistory.style.overflowY = 'auto';
- chatHistory.style.backgroundColor = '#333';
- chatBox.appendChild(chatHistory);
- // Function to add message to chat history
- function addMessage(message, isUser) {
- var msgContainer = document.createElement('div');
- msgContainer.classList.add('message');
- msgContainer.style.clear = 'both';
- msgContainer.style.padding = '5px 10px';
- msgContainer.style.color = 'white';
- if (isUser) {
- var userLabel = document.createElement('span');
- userLabel.classList.add('user-label');
- userLabel.textContent = `${username}:`;
- userLabel.style.backgroundColor = userColor; // User's color for label
- msgContainer.appendChild(userLabel);
- } else {
- var botLabel = document.createElement('span');
- botLabel.classList.add('bot-label');
- botLabel.textContent = `(Bot):`;
- msgContainer.appendChild(botLabel);
- }
- var textNode = document.createElement('span');
- textNode.style.fontWeight = 'bold'; // Bold text
- textNode.innerHTML = ` ${message}`;
- msgContainer.appendChild(textNode);
- chatHistory.appendChild(msgContainer);
- // Divider
- var divider = document.createElement('hr');
- chatHistory.appendChild(divider);
- chatHistory.scrollTop = chatHistory.scrollHeight; // Scroll to bottom
- }
- // Input field
- var chatInput = document.createElement('input');
- chatInput.type = 'text';
- chatInput.placeholder = 'Type here...';
- chatInput.style.width = 'calc(100% - 20px)';
- chatInput.style.padding = '10px';
- chatInput.style.margin = '10px auto'; // Centered horizontally
- chatInput.style.borderRadius = '10px';
- chatInput.style.border = 'none';
- chatInput.style.backgroundColor = '#444';
- chatInput.style.color = 'white';
- chatInput.style.zIndex = '10000'; // Ensure input field is above other elements
- chatBox.appendChild(chatInput);
- // Listen for Enter key press to send message
- chatInput.addEventListener('keydown', function(event) {
- if (event.key === 'Enter') {
- event.preventDefault();
- sendMessage();
- }
- });
- // Function to send message to Discord
- function sendMessage() {
- var expression = chatInput.value.trim();
- if (expression !== '') {
- addMessage(expression, true); // Add user's message to chat history
- sendMessageToDiscord(expression); // Send message to Discord
- chatInput.value = '';
- }
- }
- // Example function to send formatted message to Discord
- function sendMessageToDiscord(message) {
- const formattedMessage = `<span style="color: ${userColor};"><strong>${username}:</strong> ${message}</span>`;
- sendToDiscord(formattedMessage);
- }
- // Function to send message to Discord using CORS proxy
- function sendToDiscord(message) {
- fetch(MESSAGE_ENDPOINT, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': `Bot ${DISCORD_TOKEN}`
- },
- body: JSON.stringify({
- content: message,
- }),
- })
- .then(response => {
- if (!response.ok) {
- console.error('Failed to send message to Discord:', response.statusText);
- } else {
- console.log('Message sent successfully to Discord:', message);
- }
- })
- .catch(error => {
- console.error('Error sending message to Discord:', error);
- });
- }
- // CSS styles for user and bot messages
- GM_addStyle(`
- .message {
- clear: both;
- padding: 5px 10px;
- color: white;
- }
- .bot-label {
- float: left;
- margin-left: 5px;
- background-color: blue;
- color: white;
- border-radius: 10px;
- padding: 3px 6px;
- }
- .user-label {
- float: left;
- margin-left: 5px;
- border-radius: 10px;
- padding: 3px 6px;
- }
- hr {
- border-top: 1px solid white;
- margin: 5px 0;
- }
- .chat-dragging * {
- /* Removed user-select: none; to enable touch interactions */
- }
- `);
- // Add username change button to Tampermonkey dashboard
- GM_registerMenuCommand('Change Username', function() {
- var newUsername = prompt('Enter your new username:');
- if (newUsername) {
- updateUsername(newUsername);
- }
- });
- // Add user color change button to Tampermonkey dashboard
- GM_registerMenuCommand('Change User Color', function() {
- const colorOptions = [
- 'Red', 'Orange', 'Yellow', 'Green', 'Cyan', 'Blue', 'Purple', 'Pink'
- ].join('\n');
- const selectedColor = prompt(`Choose your new color:\n${colorOptions}`, basicColors.find(color => color.code === userColor).name);
- if (selectedColor) {
- const newColor = basicColors.find(color => color.name === selectedColor).code;
- updateUserColor(newColor);
- }
- });
- // Append chatBox to body
- document.body.appendChild(chatBox);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement