twice transactions fixed
Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
parent
8306df346a
commit
fce59c4463
57
index.js
57
index.js
@ -20,6 +20,8 @@ let btcMonitorChannel = null;
|
|||||||
let btcSocket = null;
|
let btcSocket = null;
|
||||||
let isMonitoring = false;
|
let isMonitoring = false;
|
||||||
let minBtcAmount = 0; // Minimum BTC amount to show transactions
|
let minBtcAmount = 0; // Minimum BTC amount to show transactions
|
||||||
|
let transactionHandler = null; // Store the transaction handler reference
|
||||||
|
let processedTransactions = new Set(); // Track processed transaction hashes
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [
|
intents: [
|
||||||
@ -214,16 +216,34 @@ async function validateMultimediaUrl(url) {
|
|||||||
async function initBitcoinMonitoring() {
|
async function initBitcoinMonitoring() {
|
||||||
try {
|
try {
|
||||||
// Make sure to stop any existing monitoring first
|
// Make sure to stop any existing monitoring first
|
||||||
if (btcSocket) {
|
if (btcSocket || isMonitoring) {
|
||||||
stopBitcoinMonitoring();
|
stopBitcoinMonitoring();
|
||||||
|
// Wait a moment to ensure cleanup is complete
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear processed transactions to start fresh
|
||||||
|
processedTransactions.clear();
|
||||||
|
console.log('Starting Bitcoin monitoring with fresh transaction history');
|
||||||
|
|
||||||
|
// Create a completely new Socket instance
|
||||||
btcSocket = new Socket();
|
btcSocket = new Socket();
|
||||||
|
|
||||||
btcSocket.onTransaction(async (tx) => {
|
// Create the transaction handler function
|
||||||
if (!btcMonitorChannel || !isMonitoring) return;
|
transactionHandler = async (tx) => {
|
||||||
|
// Double check that monitoring is still active
|
||||||
|
if (!btcMonitorChannel || !isMonitoring || !btcSocket) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Check if we've already processed this transaction
|
||||||
|
if (processedTransactions.has(tx.hash)) {
|
||||||
|
console.log(`Duplicate transaction detected: ${tx.hash}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add to processed transactions
|
||||||
|
processedTransactions.add(tx.hash);
|
||||||
|
|
||||||
// Process transaction data
|
// Process transaction data
|
||||||
const txData = await processBitcoinTransaction(tx, blockexplorer);
|
const txData = await processBitcoinTransaction(tx, blockexplorer);
|
||||||
|
|
||||||
@ -232,6 +252,9 @@ async function initBitcoinMonitoring() {
|
|||||||
return; // Skip this transaction if it's below the minimum amount
|
return; // Skip this transaction if it's below the minimum amount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log for debugging
|
||||||
|
console.log(`Processing BTC transaction: ${txData.hash} - ${txData.amount} BTC`);
|
||||||
|
|
||||||
// Create Discord embed
|
// Create Discord embed
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setTitle('🟠 New Bitcoin Transaction')
|
.setTitle('🟠 New Bitcoin Transaction')
|
||||||
@ -271,7 +294,10 @@ async function initBitcoinMonitoring() {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing Bitcoin transaction:', error);
|
console.error('Error processing Bitcoin transaction:', error);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Register the transaction handler
|
||||||
|
btcSocket.onTransaction(transactionHandler);
|
||||||
|
|
||||||
console.log('Bitcoin monitoring initialized successfully');
|
console.log('Bitcoin monitoring initialized successfully');
|
||||||
return true;
|
return true;
|
||||||
@ -320,17 +346,30 @@ function stopBitcoinMonitoring() {
|
|||||||
|
|
||||||
// Properly close the WebSocket connection
|
// Properly close the WebSocket connection
|
||||||
try {
|
try {
|
||||||
if (btcSocket.ws && btcSocket.ws.readyState === 1) { // WebSocket.OPEN = 1
|
if (btcSocket.ws) {
|
||||||
btcSocket.ws.close();
|
if (btcSocket.ws.readyState === 1) { // WebSocket.OPEN = 1
|
||||||
|
btcSocket.ws.close();
|
||||||
|
}
|
||||||
|
// Force close and cleanup
|
||||||
|
btcSocket.ws.onmessage = null;
|
||||||
|
btcSocket.ws.onopen = null;
|
||||||
|
btcSocket.ws.onclose = null;
|
||||||
|
btcSocket.ws.onerror = null;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error closing WebSocket:', error);
|
console.error('Error closing WebSocket:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear all references
|
||||||
btcSocket = null;
|
btcSocket = null;
|
||||||
|
transactionHandler = null;
|
||||||
btcMonitorChannel = null;
|
btcMonitorChannel = null;
|
||||||
minBtcAmount = 0; // Reset minimum amount
|
minBtcAmount = 0; // Reset minimum amount
|
||||||
console.log('Bitcoin monitoring stopped and WebSocket closed');
|
|
||||||
|
// Clear processed transactions to prevent duplicate detection issues
|
||||||
|
processedTransactions.clear();
|
||||||
|
console.log(`Bitcoin monitoring stopped, WebSocket closed, and cleared ${processedTransactions.size} processed transactions`);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -611,7 +650,9 @@ client.on(Events.InteractionCreate, async interaction => {
|
|||||||
.addFields(
|
.addFields(
|
||||||
{ name: 'Status', value: isMonitoring ? '🟢 Running' : '🔴 Stopped', inline: true },
|
{ name: 'Status', value: isMonitoring ? '🟢 Running' : '🔴 Stopped', inline: true },
|
||||||
{ name: 'Channel', value: btcMonitorChannel ? btcMonitorChannel.toString() : 'None', inline: true },
|
{ name: 'Channel', value: btcMonitorChannel ? btcMonitorChannel.toString() : 'None', inline: true },
|
||||||
{ name: 'Min Amount', value: `${minBtcAmount} BTC`, inline: true }
|
{ name: 'Min Amount', value: `${minBtcAmount} BTC`, inline: true },
|
||||||
|
{ name: 'Processed TXs', value: `${processedTransactions.size}`, inline: true },
|
||||||
|
{ name: 'WebSocket', value: btcSocket ? '🟢 Connected' : '🔴 Disconnected', inline: true }
|
||||||
)
|
)
|
||||||
.setColor(isMonitoring ? 0x00FF00 : 0xFF0000)
|
.setColor(isMonitoring ? 0x00FF00 : 0xFF0000)
|
||||||
.setTimestamp();
|
.setTimestamp();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user