twice transactions fixed

Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
ale 2025-06-08 03:00:53 +02:00
parent 8306df346a
commit fce59c4463
Signed by: ale
GPG Key ID: 244A9C4DAB1C0C81

View File

@ -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();