Made announcement logic better

This commit is contained in:
Wizzard 2024-03-14 00:47:50 -04:00
parent bcdfb07eb0
commit af047f4183
1 changed files with 17 additions and 6 deletions

23
main.js
View File

@ -18,7 +18,7 @@ const client = new Client({
const solanaPriceChannelId = process.env.SOLANA_PRICE_CHANNEL_ID;
const announcementsChannelId = process.env.ANNOUNCEMENTS_CHANNEL_ID;
const solanaDataFile = './data/solana.json';
let lastKnownPriceAtStartup;
let lastAnnouncedPrice;
let lastPriceMessageId;
client.once('ready', async () => {
@ -26,7 +26,7 @@ client.once('ready', async () => {
let solanaData = readSolanaData();
if (solanaData) {
priceHistory = solanaData;
lastKnownPriceAtStartup = solanaData.currentPrice;
lastAnnouncedPrice = solanaData.lastAnnouncedPrice;
lastPriceMessageId = solanaData.lastPriceMessageId;
}
immediatePriceCheckAndAnnounce();
@ -134,6 +134,8 @@ async function checkPriceContinuously() {
await fetchSolanaPriceAndUpdateHistory();
const { oneMinChange, fiveMinChange, oneHourChange } = calculateChanges();
console.log(`Current Price: ${priceHistory.currentPrice}`);
const embed = new EmbedBuilder()
.setColor(0x0099ff)
.setThumbnail('https://solana.com/src/img/branding/solanaLogoMark.png')
@ -164,10 +166,11 @@ async function checkPriceContinuously() {
sendNewPriceMessage(solanaPriceChannel, embed);
}
if (lastKnownPriceAtStartup !== null && (parseFloat(priceHistory.currentPrice) - lastKnownPriceAtStartup >= 2.5)) {
if (lastAnnouncedPrice !== null && (parseFloat(priceHistory.currentPrice) - lastAnnouncedPrice >= 2.5)) {
const announcementsChannel = await client.channels.fetch(announcementsChannelId);
await announcementsChannel.send(`@everyone Solana price has increased significantly! Current price: $${priceHistory.currentPrice}`);
lastKnownPriceAtStartup = parseFloat(priceHistory.currentPrice);
lastAnnouncedPrice = parseFloat(priceHistory.currentPrice);
saveSolanaData({ ...priceHistory, lastAnnouncedPrice });
}
setTimeout(checkPriceContinuously, 60000);
@ -179,14 +182,22 @@ function saveSolanaData(data) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(solanaDataFile, JSON.stringify(data, null, 2), 'utf8');
const dataToSave = {
...data,
lastAnnouncedPrice: lastAnnouncedPrice,
};
fs.writeFileSync(solanaDataFile, JSON.stringify(dataToSave, null, 2), 'utf8');
}
function readSolanaData() {
try {
if (fs.existsSync(solanaDataFile)) {
const fileContent = fs.readFileSync(solanaDataFile, 'utf8');
return JSON.parse(fileContent);
const data = JSON.parse(fileContent);
lastAnnouncedPrice = data.lastAnnouncedPrice || null;
return data;
}
} catch (error) {
console.error('Error reading Solana data:', error);