Update token via discord

This commit is contained in:
2024-03-02 16:04:10 -05:00
parent 2ed93d3e93
commit d46bdd3c90
4 changed files with 326 additions and 4 deletions

62
main.js
View File

@@ -2,11 +2,69 @@ 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.');
@@ -66,9 +124,7 @@ const updateOBSTextVisibility = (makingMoney) => {
};
const checkStatusAndUpdateOBS = () => {
const tokenAddress = process.env.tokenAddress;
axios.get(`https://api.dexscreener.com/latest/dex/tokens/${tokenAddress}`)
axios.get(`https://api.dexscreener.com/latest/dex/tokens/${currentToken}`)
.then((response) => {
const pairs = response.data.pairs;
if (pairs && pairs.length > 0) {