require('dotenv').config(); const axios = require('axios'); const WebSocket = require('ws'); const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); const { Client, GatewayIntentBits } = require('discord.js') const envPath = path.resolve(__dirname, '.env'); const obsWebSocketUrl = 'ws://localhost:4444'; const password = process.env.OBS_PASSWORD; const discordToken = process.env.DISCORD_TOKEN; const controlChannelId = "1203520978148134953"; let currentToken = process.env.tokenAddress; const ws = new WebSocket(obsWebSocketUrl); const discordClient = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); let calculationInterval; function restartCalculations() { if (calculationInterval) { clearInterval(calculationInterval); } calculationInterval = setInterval(checkStatusAndUpdateOBS, 5000); } discordClient.login(discordToken); discordClient.on('ready', () => { console.log(`Logged in as ${discordClient.user.tag}!`); restartCalculations(); }); discordClient.on('messageCreate', message => { if (message.channel.id === controlChannelId) { if (message.content.startsWith('!token ')) { const newToken = message.content.split(' ')[1]; if (newToken) { currentToken = newToken; updateEnvToken(newToken); restartCalculations(); message.reply('Token updated successfully. Restarting calculations.'); } } } }); function updateEnvToken(newToken) { fs.readFile(envPath, 'utf8', (err, data) => { if (err) { console.error('Error reading .env file', err); return; } let updatedData = data.split('\n').map(line => { if (line.startsWith('tokenAddress=')) { return `tokenAddress=${newToken}`; } return line; }).join('\n'); fs.writeFile(envPath, updatedData, 'utf8', (err) => { if (err) console.error('Error writing to .env file', err); }); }); } ws.on('open', () => { console.log('Connected to OBS WebSocket server.'); ws.send(JSON.stringify({ 'request-type': 'GetAuthRequired', 'message-id': 'authRequired' })); }); ws.on('message', (data) => { const message = JSON.parse(data.toString()); if (message['message-id'] === 'authRequired') { if (message.authRequired) { const auth = generateAuth(password, message.salt, message.challenge); ws.send(JSON.stringify({ 'request-type': 'Authenticate', 'message-id': 'authenticate', 'auth': auth })); } else { setInterval(updateOBSTextVisibility, 60000); } } else if (message['message-id'] === 'authenticate') { if (message.status === 'ok') { setInterval(updateOBSTextVisibility, 5000); } else { console.error('Authentication failed:', message.error); } } else if (message['message-id'] === 'setText') { if (message.status === 'ok') { console.log('Text updated successfully in OBS.'); } else { console.error('Failed to update text in OBS:', message.error); } } }); ws.on('error', (error) => { console.error('WebSocket error:', error); }); const setSourceVisibility = (sourceName, visible) => { const message = { 'request-type': 'SetSourceRender', 'source': sourceName, 'render': visible, 'message-id': 'setSourceVisibility' }; ws.send(JSON.stringify(message)); }; const updateOBSTextVisibility = (makingMoney) => { setSourceVisibility("TextGreen", makingMoney); setSourceVisibility("TextRed", !makingMoney); }; const checkStatusAndUpdateOBS = () => { axios.get(`https://api.dexscreener.com/latest/dex/tokens/${currentToken}`) .then((response) => { const pairs = response.data.pairs; if (pairs && pairs.length > 0) { const priceChange = pairs[0].priceChange.m5; const makingMoney = priceChange > 0; console.log(`Price Change (Last 5 Minutes): ${priceChange}%`); console.log('Making Money:', makingMoney); setSourceVisibility("TextGreen", makingMoney); setSourceVisibility("TextRed", !makingMoney); } else { console.log('No pairs data found for the token address'); } }) .catch((error) => { console.error('Error fetching data:', error); }); }; function generateAuth(password, salt, challenge) { const secret = crypto.createHash('sha256').update(password + salt).digest('base64'); const authResponse = crypto.createHash('sha256').update(secret + challenge).digest('base64'); return authResponse; } setInterval(checkStatusAndUpdateOBS, 5000);