require('dotenv').config(); const { Client, GatewayIntentBits, EmbedBuilder } = require('discord.js'); const fs = require('fs'); const path = require('path'); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); const solanaPriceChannelId = process.env.SOLANA_PRICE_CHANNEL_ID; const announcementsChannelId = process.env.ANNOUNCEMENTS_CHANNEL_ID; const solanaDataFile = './data/solana.json'; let lastKnownPriceAtStartup; let lastPriceMessageId; client.once('ready', async () => { console.log('Bot is online!'); immediatePriceCheckAndAnnounce() const solanaData = readSolanaData(); lastKnownPriceAtStartup = solanaData ? solanaData.price : null; lastPriceMessageId = solanaData ? solanaData.lastPriceMessageId : null; checkPriceContinuously(); }); async function fetchSolanaPrice() { const cryptocompareApiUrl = 'https://min-api.cryptocompare.com/data/price?fsym=SOL&tsyms=USD'; try { const fetch = (await import('node-fetch')).default; const response = await fetch(cryptocompareApiUrl); const data = await response.json(); return parseFloat(data.USD).toFixed(2); } catch (error) { console.error('Error fetching Solana price:', error); return null; } } async function immediatePriceCheckAndAnnounce() { const solanaData = readSolanaData(); const lastKnownPrice = solanaData ? solanaData.price : null; const currentPrice = await fetchSolanaPrice(); if (currentPrice && lastKnownPrice !== null && (parseFloat(currentPrice) - lastKnownPrice >= 5)) { const announcementsChannel = await client.channels.fetch(announcementsChannelId); await announcementsChannel.send(`@everyone Significant Solana price increase detected! Previous recorded price: $${lastKnownPrice}. Current price: $${currentPrice}.`); } checkPriceContinuously(); } async function checkPriceContinuously() { const price = await fetchSolanaPrice(); if (!price) { console.log('Could not fetch price, will try again in 60 seconds.'); setTimeout(checkPriceContinuously, 60000); return; } console.log(`Current Solana Price: $${price}`); const solanaPriceChannel = await client.channels.fetch(solanaPriceChannelId); const embed = new EmbedBuilder() .setColor(0x0099ff) .setTitle('Solana (SOL) Price') .setDescription(`$${price}`) .setTimestamp(); if (lastPriceMessageId) { try { const message = await solanaPriceChannel.messages.fetch(lastPriceMessageId); await message.edit({ embeds: [embed] }); } catch (error) { console.error('Error updating price message, sending a new one:', error); const sentMessage = await solanaPriceChannel.send({ embeds: [embed] }); lastPriceMessageId = sentMessage.id; } } else { const sentMessage = await solanaPriceChannel.send({ embeds: [embed] }); lastPriceMessageId = sentMessage.id; } if (lastKnownPriceAtStartup !== null && (parseFloat(price) - lastKnownPriceAtStartup >= 5)) { const announcementsChannel = await client.channels.fetch(announcementsChannelId); await announcementsChannel.send(`@everyonle Solana price has increased significantly! Current price: $${price}`); lastKnownPriceAtStartup = parseFloat(price); } saveSolanaData({ price: parseFloat(price), lastPriceMessageId }); setTimeout(checkPriceContinuously, 60000); } function saveSolanaData(data) { const dir = path.dirname(solanaDataFile); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } fs.writeFileSync(solanaDataFile, JSON.stringify(data), 'utf8'); } function readSolanaData() { try { if (fs.existsSync(solanaDataFile)) { const fileContent = fs.readFileSync(solanaDataFile, 'utf8'); return JSON.parse(fileContent); } } catch (error) { console.error('Error reading Solana data:', error); } return null; } client.login(process.env.DISCORD_BOT_TOKEN);