<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dipendra Neupane</title>
    <description>The latest articles on DEV Community by Dipendra Neupane (@neupanedipendra).</description>
    <link>https://dev.to/neupanedipendra</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F517142%2F94a58606-34a9-4d9b-954f-57c0a6d76261.jpg</url>
      <title>DEV Community: Dipendra Neupane</title>
      <link>https://dev.to/neupanedipendra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neupanedipendra"/>
    <language>en</language>
    <item>
      <title>Building a Simple Covid Tracker Discord Bot With Node JS</title>
      <dc:creator>Dipendra Neupane</dc:creator>
      <pubDate>Thu, 19 Nov 2020 05:42:40 +0000</pubDate>
      <link>https://dev.to/neupanedipendra/building-a-simple-covid-tracker-discord-bot-with-node-js-3ed8</link>
      <guid>https://dev.to/neupanedipendra/building-a-simple-covid-tracker-discord-bot-with-node-js-3ed8</guid>
      <description>&lt;p&gt;I published a simple guide to create a &lt;a href="https://neupanedipendra.com.np/simple-covid-19-tracker-app-with-react-js"&gt;simple corona tracker web app with react js&lt;/a&gt; some time back in my &lt;a href="https://neupanedipendra.com.np"&gt;blog&lt;/a&gt;. It had a simple interface where you enter the country’s name on the input field and it will display the details like total covid cases, deaths, today’s reported cases, and also the country’s flag.&lt;/p&gt;

&lt;p&gt;Recently I got to learn about creating a discord bot with an awesome node js module called Discord.js. It is an awesome module that allows communicating with Discord API very easily. So I decided to create a simple bot for my demo server that serves the same purpose as my react web app.&lt;/p&gt;

&lt;p&gt;In this blog post, I will share how you can also create a simple Discord bot that can provide the covid stats right into your server. This is meant to be for beginners and it is one of my first bots ever created. Before getting into the actual code, let me first talk about the requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;To get started with creating your own discord bot with Discord.js, you’ll need to learn these things and also use the tools I’ve mentioned below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Node JS installed on your computer
&lt;/li&gt;
&lt;li&gt;  Code Editor (Recommended VS Code)
&lt;/li&gt;
&lt;li&gt;  Basic understanding of Node JS and JavaScript
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have these basic understandings and tools, you are ready to build your simple bot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the bot
&lt;/h2&gt;

&lt;p&gt;First, we will create an application and add a bot to our server through the discord developers platform which will provide us a token so that we can code the actual bot.&lt;/p&gt;

&lt;p&gt;Head over to the Discord Developer website and click on New Application. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hTaIfo6_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/7c52c047afbdda0f391404b3ac336f75/5ca24/discord-application.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hTaIfo6_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/7c52c047afbdda0f391404b3ac336f75/5ca24/discord-application.webp" alt="discord application dashboard" width="590" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Provide a name for the application like “Covid Tracker App” and click Create.&lt;/p&gt;

&lt;p&gt;It will redirect to a General Information page. Click on Bot from the left sidebar and then Add Bot button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O8XYxQsu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/d7a3b6e00d545da9f8238bb2f8d2a430/5ca24/discord-general.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O8XYxQsu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/d7a3b6e00d545da9f8238bb2f8d2a430/5ca24/discord-general.webp" alt="discord general" width="590" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll need to enter the bot Name on this page. This is the actual name of the bot that appears on any discord server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y4quQg_i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/4ecab58978f3bc610dbadc0ef5486d4f/5ca24/naming-bot.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y4quQg_i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/4ecab58978f3bc610dbadc0ef5486d4f/5ca24/naming-bot.webp" alt="adding a new bot" width="590" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the token below your bot’s name and save it somewhere safe. &lt;strong&gt;This token must be kept secret and you must never share it with anyone.&lt;/strong&gt; It will be used to login and control your bot.&lt;/p&gt;

&lt;p&gt;Next, click on OAuth2 from the left sidebar and choose “bot” from the scopes checklist. It will generate a URL below, copy the URL and paste it on your browser. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xdEKkEDg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/9b195d4d8aa6dd84bf5077caa402a32d/5ca24/add-bot-url.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xdEKkEDg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/9b195d4d8aa6dd84bf5077caa402a32d/5ca24/add-bot-url.webp" alt="bot-auth-url" width="590" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will open a list of the servers where you have manager access permissions, select a server where you want to add the bot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lNEJTwix--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/27cacd94d57a736f72eaa2c5714d6be5/bde39/authorize-bot.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lNEJTwix--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://neupanedipendra.com.np/static/27cacd94d57a736f72eaa2c5714d6be5/bde39/authorize-bot.webp" alt="authorize bot dialog box" width="419" height="581"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding our Bot
&lt;/h2&gt;

&lt;p&gt;Let’s first understand how our bot works in this section.&lt;/p&gt;

&lt;p&gt;It is a simple bot that fetches the data for a particular country from an API and displays the stats inside your discord server.&lt;/p&gt;

&lt;p&gt;Here's the free API I am using for the bot.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://corona.lmao.ninja/v2/countries&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you provide a country’s name at the end of this API, you’ll receive the data for that particular country.&lt;/p&gt;

&lt;p&gt;So we will set a custom command which will trigger the bot and provide the country as an argument. The stats will be then provided by the bot in the reply.&lt;/p&gt;

&lt;p&gt;Now let’s get our hands dirty with some code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s Code
&lt;/h2&gt;

&lt;p&gt;To get into coding, you must have node installed in your machine first. You can download and install node from this official website here. It will also install npm on your machine.&lt;/p&gt;

&lt;p&gt;To check if node and npm are installed correctly, open your terminal or command prompt then enter the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;node --version&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm --version&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If it displays the versions of node and npm, they are installed correctly. If it throws any errors, try installing node again as instructed above.&lt;/p&gt;

&lt;p&gt;Then create a new directory where your bot will be located. I will name it &lt;code&gt;corona-tracker-bot&lt;/code&gt;. Then open the folder in VS Code. Next, open the terminal and run &lt;code&gt;npm init -y&lt;/code&gt;. It will create a &lt;code&gt;package.json&lt;/code&gt; file in the folder. It will hold all our dependencies for the project and also define our main script file.&lt;/p&gt;

&lt;p&gt;We will need to install Discord.js module into our app. Also we will require node-fetch module to fetch data from our API. We can install the required modules with the following command on the terminal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i discord.js node-fetch&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will add the &lt;code&gt;node_modules&lt;/code&gt; folder to our root directory and also generate a &lt;code&gt;package-lock.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Then we will create our script file &lt;code&gt;index.js&lt;/code&gt; where we will write the actual code for the bot.&lt;/p&gt;

&lt;p&gt;First we will require Discord.js module on our index.js file so that we can use it on the app. Then we will create a new instance with the name client with the code as below. Also, we will store the secret token in the constant variable “token”. Then we can log in the bot with the command &lt;code&gt;client.login(token)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The complete code will look like below if you follow all these steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;  &lt;span class="nx"&gt;Discord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;discord.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;  &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;  &lt;span class="nx"&gt;Discord&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;  &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xxxxxxxxxxxx&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can run this code by running &lt;code&gt;node index&lt;/code&gt; from the terminal on the root directory. If we run this code, our bot will already appear online. Just to make sure let me add a console.log whenever our bot is online. Add the following code for the purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ready&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Logged in as &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s create a custom command that will trigger the bot. It will be just a message that will enable the bot for the particular action. It is a good idea to initiate the command with some special characters like &lt;code&gt;!&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt; etc. I will use &lt;code&gt;!&lt;/code&gt; for this bot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PREFIX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we will split the commands provided after the prefix symbol and store it in an array &lt;code&gt;args&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt;  &lt;span class="nx"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PREFIX&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the first element of the array args i.e.(args[0]) is “corona”, we will trigger the bot.&lt;/p&gt;

&lt;p&gt;The user must provide the country’s name as the second argument after the “corona” command. If no second argument after the “corona” command is provided, it will reply an error message to the user. Else the second argument is stored in the “country” variable and passed into the URL as template literal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cliet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt;  &lt;span class="nx"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PREFIX&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;country&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;corona&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please supply a valid country in the format `!corona [country]` &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`https://corona.lmao.ninja/v2/countries/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;country&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the API is fetched for data. This is where we use node-fetch module. To use this module, we need to require it in the beginning of the index.js file and store it in fetch constant.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const fetch = require(‘node-fetch’)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then corona stats are fetched as below. Add this code on the else block above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`
        Country: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;country&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
        Total Cases: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cases&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
        Total Deaths: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deaths&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
        Active Cases: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
        Today Cases: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;today&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
        `&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you enter an invalid country or if the country has no cases, it will provide a JSON with just “message” key. In the case of a valid country, it will provide valid stats. Then the command is replied with stats as listed on the template literals.&lt;/p&gt;

&lt;p&gt;You can now start your node app as &lt;code&gt;node index&lt;/code&gt;. It will show your bot as online and if you provide &lt;code&gt;!corona nepal&lt;/code&gt; on any of your channels on the server where you added the bot, it will provide the details as mentioned on the template literals.&lt;/p&gt;

&lt;p&gt;If you want to see the bot in action, check out my &lt;a href="https://discord.gg/Kmrj58hp"&gt;Demo Discord Server&lt;/a&gt; and head over to #corona channel where you can use the commands to get the corona stats for any country. I have implemented Embeds for this bot, added a display picture and also added help command. &lt;/p&gt;

&lt;p&gt;I hope this simple guide will help you create your first simple Discord bot with node js and Discord.js. Don't forget to share your bots in the comment section below. &lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published in &lt;a href="https://neupanedipendra.com.np"&gt;https://neupanedipendra.com.np&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>node</category>
    </item>
  </channel>
</rss>
