Skip to content

Instantly share code, notes, and snippets.

@Kakulukian
Created May 28, 2023 11:51
Show Gist options
  • Save Kakulukian/df8e8486656a2fb0aff9482bf8447804 to your computer and use it in GitHub Desktop.
Save Kakulukian/df8e8486656a2fb0aff9482bf8447804 to your computer and use it in GitHub Desktop.
Never Miss a Cardano NFT Sale Again: Automating Discord Notifications with JavaScript
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);
@Kakulukian
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment