Created
May 28, 2023 11:51
-
-
Save Kakulukian/df8e8486656a2fb0aff9482bf8447804 to your computer and use it in GitHub Desktop.
Never Miss a Cardano NFT Sale Again: Automating Discord Notifications with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Discord = require('discord.js'); | |
const client = new Discord.Client(); | |
const token = process.env.DISCORD_TOKEN; | |
const opencnftKey = process.env.OPENCNFT_KEY; | |
const policy = process.env.POLICY; | |
let lastTimeSent = undefined; | |
const opencnftURL = new URL( | |
`https://api.opencnft.io/2/collection/${policy}/transaction`, | |
); | |
client.once('ready', () => { | |
console.log('Bot is ready!'); | |
}); | |
client.on('message', (message) => { | |
// Start sending Cardano NFT sales notifications every minute | |
setInterval(async () => { | |
// Code to fetch and send Cardano NFT sales notifications | |
opencnftURL.searchParams.set( | |
'end_time', | |
lastTimeSent ? Math.floor(lastime / 1_000) : undefined, | |
); | |
const opencnftResponse = await fetch(opencnftURL.toString(), { | |
headers: { | |
'x-api-key': opencnftKey, | |
}, | |
}); | |
if (opencnftResponse.ok) { | |
const { transactions } = await opencnftResponse.json(); | |
const notification = `${transactions | |
.map( | |
(tx) => | |
`New sale on ${tx.marketplace}: ${tx.name} at ₳${Math.floor( | |
tx.price / 1e6, | |
)}`, | |
) | |
.join('\n')}`; | |
message.channel.send(notification); | |
lastTimeSent = new Date(); | |
} | |
}, 60000); | |
}); | |
client.login(token); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Companion blog post: https://developers.opencnft.io/blog/discord-bot-sales-cardano-nft