First working version
This commit is contained in:
99
main.js
Normal file
99
main.js
Normal file
@@ -0,0 +1,99 @@
|
||||
require('dotenv').config();
|
||||
const axios = require('axios');
|
||||
const WebSocket = require('ws');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const obsWebSocketUrl = 'ws://localhost:4444';
|
||||
const password = process.env.OBS_PASSWORD;
|
||||
|
||||
const ws = new WebSocket(obsWebSocketUrl);
|
||||
|
||||
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 = () => {
|
||||
const tokenAddress = process.env.tokenAddress;
|
||||
|
||||
axios.get(`https://api.dexscreener.com/latest/dex/tokens/${tokenAddress}`)
|
||||
.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);
|
||||
Reference in New Issue
Block a user