DEV Community

shadowtime2000
shadowtime2000

Posted on

1 3

Crypto Prices in Discord - Bot

Crypto Prices in Discord - Bot

Sorry for the long delay, but this is the second and last part of my series of bringing cryptocurrency prices into Discord. This tutorial will use a Discord bot.

Setting up

To create our bot template, we are going to use a cool project called create-discord-bot.

Run npx peterthehan/create-discord-bot in the bash if you are on a Unix based system and in the Git bash if you are on Windows. Fill out the questions that it prompts you and you have a Discord bot ready to be used.

We will also be using the CoinGecko API to get prices for cryptocurrency, so run npm install --save coingecko-api.

How the current bot works

Our bot currently runs on a widget based framework. Any folder located under the folder widgets is considered a widget and every widget must contain a folder handlers with .js files with names of discord.js events. There is one widget command which comes with it, and has it's own system of commands. You can insert any command into it's commands folder and use a CommandBuilder class to create commands.

Programming the bot

First, you should run npm run dev to start nodemon so you don't need to keep on restarting the bot.

Create a new file in the command/commands folder called coinPrice.js. Put this inside of it:

const CommandBuilder = require("../classes/CommandBuilder");

const CoinGecko = require("coingecko-api");

const CoinGeckoClient = new CoinGecko();

module.exports = new CommandBuilder()
  .setName("coinprice")
  .setAliases(["coin", "price"])
  .setOwnersOnly(false)
  .setGuildOnly(false)
  .setRequireArgs(true)
  .setDeletable(false)
  .setCooldown(10)
  .setDisabled(false)
  // eslint-disable-next-line
  .setExecute(async (message, user, args) => {
    let data = await CoinGeckoClient.simple.price({
      ids: [args[0]],
      vs_currencies: [args[1]],
    });
    await message.channel.send(
      `${args[2] || 1} ${args[0]} -> ${
        data.data[args[0]][args[1]] * (args[2] || 1)
      } ${args[1]}`
    );
  });

Enter fullscreen mode Exit fullscreen mode

This basically allows you to run the command coin with this format: .coin <coinToCheck> <currencyToCompareAgainst> [optionalAmountForCoinToCheck]

Finishing Thoughts

Try adding more commands such as comparison of margin and other things you could do.

WARNING The create-discord-bot project is currently going through some major changes migrating the codebase to Typescript in a way that you still won't have to change your project. You may still need to change your project for that though.

Source code on Github

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Collapse
 
sleklere profile image
sleklere

Hi, i am very new to programming and stuff, and i tried to follow the steps of this post to create a price bot, but when i finished and tried to use the .coin command i got back NaN. I tried to write it in different ways (i.e .coin bitcoin tether; .coin bitcoin tether 1)(as i've said i know very little to nothing so i was just trying anything)
what can i do here?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay