DEV Community

shadowtime2000
shadowtime2000

Posted on • Updated on

Crypto Prices In Discord - Webhook

In this 2 part tutorial I will show how you can put cryptocurrency prices in Discord using two methods:

  1. Discord Webhook
  2. Discord Bot

Today, we will be going over using a webhook.

How the bot will work

A configuration file will be provided with the IDs of cryptocurrencies to watch, the currency to compare them with, and the interval to send updates on prices in minutes.

Setting up the workspace

Create a new folder and call npm init. Then, install the two packages we will be using, coingecko-api and discord.js, with npm install coingecko-api discord.js --save.

Inside a Discord server, create a webhook for the channel you want to send the crypto information to. You will get a link, which is patterned like this: https://discordapp.com/api/webhooks/webhookID/webhookToken.

Create a file called config.json and in that put the webhookID and webhookToken attributes in their appropriate fields. In this file create an array named watchlist filled with the cryptocurrencies to watch. Then, create an array called compare filled with the currencies to compare those cryptocurrencies to. Also create a field called interval and give it whatever number of minutes you want to wait before getting each update.

Now, lets get started with the programming.

Programming the program

We first have to write some basic starter code:

const Discord = require("discord.js");
const CoinGecko = require("coingecko-api");
const config = require("./config.json");

const webhookClient = new Discord.WebhookClient(config.webhookID, config.webhookToken);

const CoinGeckoClient = new CoinGecko();
Enter fullscreen mode Exit fullscreen mode

Lets first try making it so the program will send prices when it is run.

const sendPrices = async () => {
  let data = await CoinGeckoClient.simple.price({
    ids: config.watchlist,
    vs_currencies: config.compare
  });
  var embed = {
    title: "Crypto Update",
    fields: []
  };
  for (const i in data.data) {
    for (const j in data.data[i]) {
      embed.fields.push({name: i + " -> " + j, value: "1 -> " + data.data[i][j] });
    }
  }
  webhookClient.send("Crypto Prices", {embeds: [embed]});
}

sendPrices();
Enter fullscreen mode Exit fullscreen mode

This will get the prices and organize into an embed message and send it through the webhook.

Now we have to make it timed. We first have to calculate the milliseconds we have to wait, so put this where you initialize the webhook client and the CoinGeckoClient:

const waitTime = config.interval * 60 * 1000;
Enter fullscreen mode Exit fullscreen mode

We are now going to create a simple Promise based delay function, so we can use that in a loop.

const delay = async () => {
  return new Promise(resolve => setTimeout(resolve, waitTime));
}
Enter fullscreen mode Exit fullscreen mode

Now delete the sendPrices(); statement. We need to create an asynchronous loop to run and delay the wait time. This is what we could do:

(async () => {
  while (true) {
    await sendPrices();
    await delay();
  }
})();
Enter fullscreen mode Exit fullscreen mode

If you test it, it should work. Congratulations you made a program to send cryptocurrency prices to a Discord channel using Discord webhooks. The next post in the series will detail how to do the same thing with a bot instead of using a webhook.

The source code for this tutorial is on Github

Top comments (0)