diff --git a/index.js b/index.js index 7a1ccff..9903bee 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,8 @@ let btcMonitorChannel = null; let btcSocket = null; let isMonitoring = false; 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({ intents: [ @@ -214,16 +216,34 @@ async function validateMultimediaUrl(url) { async function initBitcoinMonitoring() { try { // Make sure to stop any existing monitoring first - if (btcSocket) { + if (btcSocket || isMonitoring) { 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.onTransaction(async (tx) => { - if (!btcMonitorChannel || !isMonitoring) return; + // Create the transaction handler function + transactionHandler = async (tx) => { + // Double check that monitoring is still active + if (!btcMonitorChannel || !isMonitoring || !btcSocket) return; 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 const txData = await processBitcoinTransaction(tx, blockexplorer); @@ -232,6 +252,9 @@ async function initBitcoinMonitoring() { 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 const embed = new EmbedBuilder() .setTitle('🟠 New Bitcoin Transaction') @@ -271,7 +294,10 @@ async function initBitcoinMonitoring() { } catch (error) { console.error('Error processing Bitcoin transaction:', error); } - }); + }; + + // Register the transaction handler + btcSocket.onTransaction(transactionHandler); console.log('Bitcoin monitoring initialized successfully'); return true; @@ -320,17 +346,30 @@ function stopBitcoinMonitoring() { // Properly close the WebSocket connection try { - if (btcSocket.ws && btcSocket.ws.readyState === 1) { // WebSocket.OPEN = 1 - btcSocket.ws.close(); + if (btcSocket.ws) { + 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) { console.error('Error closing WebSocket:', error); } + // Clear all references btcSocket = null; + transactionHandler = null; btcMonitorChannel = null; 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 false; @@ -611,7 +650,9 @@ client.on(Events.InteractionCreate, async interaction => { .addFields( { name: 'Status', value: isMonitoring ? '🟢 Running' : '🔴 Stopped', 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) .setTimestamp();