a blog for those who code

Thursday 23 August 2018

Let's Learn How to Create a Discord Bot

In the event that you are in the Discord App you might have seen a considerable amount of Bot around whether its Banjo, Tatsumaki and so forth. You may think about how these bots be made, the answer is to write an application and run it on your local server or a public server to make it active. In this post, we will figure out how to make a discord bot utilizing Node.js.

On the off chance that you are not acquainted with Node.js, at that point its simple to begin with Node.js as there are such a large number of articles out there on web to kick you off. One such article I have composed on my blog Getting Started with Node.js. To get started we need to install Node.js from Nodejs.org. Once installed you can check the version of Node.js installed using command node -v in the Node.js Command Prompt.

Also Read an In-depth article : How to Make a Discord Bot: an Overview and Tutorial

Create new Discord App

Now its time to create a new Discord App. At first you need to visit Discord App Developer URL and create a new App. I have given my bot name as iamabot.

Next, since the bot will be operated as a user, you need to create it by clicking on Create a Bot User.

Once you do that you will get a User ID and a Token ID (Keep the Token ID Safe) and click on Public Bot (can be added on any server).

Our Bot has been created, now it's time to write some code in Node.js.

Node.js Application

At first, you need to create a package.json file. The content of the Package.json is shown below. We will be using discord.js node.js module to interact with Discord App.

  "name": "discordapp",
  "version": "1.0.0",
  "description": "A discord Bot",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discordie": "^0.11.0"

The index.js file content is

var Discordie = require('discordie');
var request = require('request');
var Events = Discordie.Events;
var client = new Discordie();

    token: 'Your Bot Token'

client.Dispatcher.on(Events.GATEWAY_READY, e => {
    console.log('Connected as: '+ client.User.username);

client.Dispatcher.on(Events.MESSAGE_CREATE, e => {
    var content = e.message.content;
    if(content.indexOf("$price ") == 0) {
        var coin = content.replace("$price ", "");
        var value = '';
            request('http://api.coinmarketcap.com/v1/ticker/' + coin + '/',
              function(error,res,body) {
                var obj = JSON.parse(body);
                if(obj[0] === undefined)
                    e.message.channel.sendMessage("You have entered a wrong id");
                    value = coin.toUpperCase() +
                               " : Current Price " + obj[0].price_usd +
                               " | 24hr Percentage Change " + obj[0].percent_change_24h;
        catch (err) {
            e.message.channel.sendMessage("Wrong ID, Have a Great Day");

In the above code at first, we need to connect to Discord client using the token of the app. Now once it is connected it will emit an event and write it to the console that my bot is connected. Next, we will check which cryptocurrency the user has typed after the $price and get the price of that cryptocurrency using the coinmarketcap api.

Now run the index.js file using command node index.js as shown below:

Add bot to the Server

Once Connected we need to add this bot to our server. For this, we need to take the Client ID of the bot and add it to the below url


When you open the link it will ask which server you want to add the bot too. Note that you can only add this bot to that server which you are managing, other servers will not be shown.

Once added you can see it on the right-hand side, now time to play with our new bot. See in the below pic, it's giving me the output.

In this way you can create a number of Discord Bot with minimal code. Happy Coding :)

Please like and share the CodingDefined.com blog if you find it useful and helpful.

No comments:

Post a Comment