How to create your first DISCORD BOT

In this tutorial, we will build a Discord bot from scratch, using Node.js and the Discord.js library, which allows users to directly interact with the Discord API.

We will be making a simple Bot that will reply “Hi” to users message.

Prerequisite

Before you get started, you will need the following:

I will divide this blog into three part. Follow all three parts to run your first discord bot. I have also attached My Repository’s Link at the end of the post.

  1. Setting up your system
  2. Writing a discord bot
  3. Running a discord bot

Step 1 — Setting up your system

Setting up Nodejs
First of all you need to install NodeJS on your system. To install NodeJs go to https://nodejs.org/en/download/.
Click the Windows Installer button to download the latest default version. At the time this article was written, version 10.16.0-x64 was the latest version. The Node.js installer includes the NPM package manager which we sill use to install “discord.js”.

Creating a discord application
You need an application on Discord to create your bot. To create an application go to your developer portal.

Click New Application and give your application a supercool name. I will name mine “Useless Bot” [It reflects my personality].After creating an application, add a bot to your recently created application. To add a Bot, go to Settings > Bot and click Add Bot

After creating an application and a bot. You will have access to CLIENT ID and a TokenClient ID

Bot Token:

Warning: Never share or upload your bot token. It acts like a password to your bots’s account. Anyone with the token can update your BOT.

To create a discord server click + on your Discord application and add required fields [Name Only]

Add bot in your Discord server
To add bot to a server go to Discord Permission Calculator.
Give administrator access to your application by selecting “Administrator”. Also paste your Client ID to generate authentication URL.

After entering a valid Client ID, you can click the link to install bot on your server. From there, select your server and click Authorize.

Step 2 — Writing a discord bot

Now that our bot is ready, we can start coding to make our bot do something.

Lets first create a empty directory. I will name it “Useless Bot“.

Add two file “package.json” and “index.js”.

package.json:

{
"name": "useless-bot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Your Name",
"license": "ISC
}

Installing “discord.js” to your project

To install discord.js, open terminal on your project’s directory and run following command:
npm install discord.js

This command will import some necessary libraries that you will need to create a discord bot.

Writing “index.js”

Now we can start writing codes on “index.js” to make our bot functional. I have written a small block of code which will reply when someone writes a message. You can modify as per your need.

index.js:

const Discord = require('discord.js');
const bot = new Discord.Client();
const token = "##YOUR TOKEN HERE##";
bot.login(token);
bot.on('ready', function(){
console.log('Bot Ready');
});
bot.on('message', function(message){
if(message.content == "Hi"){
message.reply('Hi');
}
});

Testing the bot locally

To run the bot you can easily run “index.js” file. You can use following command to initialize your bot.

node .

As soon as you initialize index.js, the bot will go online on Discord.

Now that your bot is online, Type “Hi” on your channel and the bot will reply with a “Hi”.

Step 3 — Running a discord bot

Creating a repository on GitHub

Go to Github and create a new repository

I have named my repository “Useless Bot”

As soon as you create your repository, you will get a URL to your Repository

Now that you have your repository’s URL. Run following git commands:

git init
git add .
git commit -m "first commit"
git remote add origin YOUR-GIT-URL-HERE
git push -u origin master

Creating an heroku application
Go to heroku.com and create a new app.

Give your bot a unique name.

Select GitHub as your development method.

Do not forget to enable “Automatic Deployment“. Enabling Automatic deployment will help you automatically initializing your app when you make changes to your repository.

Now that we have our app ready we have to let our app know to run this app. For this to to Settings > Buildpacks and Add a buildpack.

Select nodejs and save the changes

Now deploy your App and wait until the deployment is complete.

And now for the final step, go to Resources section[Reload your browser once] and toggle Web and Worker

As you can see that our bot has nothing much to do. You can add functionality to your bot and program it accordingly.

I will divide this blog into three part. Follow all three parts to run your first discord bot. I have also attached My Repository’s Link at the end of the post.

  1. Setting up your system
  2. Writing a discord bot
  3. Running a discord bot

Step 1 — Setting up your system

Setting up Nodejs
First of all you need to install NodeJS on your system. To install NodeJs go to https://nodejs.org/en/download/.
Click the Windows Installer button to download the latest default version. At the time this article was written, version 10.16.0-x64 was the latest version. The Node.js installer includes the NPM package manager which we sill use to install “discord.js”.

Creating a discord application
You need an application on Discord to create your bot. To create an application go to your developer portal.

Click New Application and give your application a supercool name. I will name mine “Useless Bot” [It reflects my personality].After creating an application, add a bot to your recently created application. To add a Bot, go to Settings > Bot and click Add Bot

After creating an application and a bot. You will have access to CLIENT ID and a TokenClient ID

Bot Token:

Warning: Never share or upload your bot token. It acts like a password to your bots’s account. Anyone with the token can update your BOT.

To create a discord server click + on your Discord application and add required fields [Name Only]

Add bot in your Discord server
To add bot to a server go to Discord Permission Calculator.
Give administrator access to your application by selecting “Administrator”. Also paste your Client ID to generate authentication URL.

After entering a valid Client ID, you can click the link to install bot on your server. From there, select your server and click Authorize.

Step 2 — Writing a discord bot

Now that our bot is ready, we can start coding to make our bot do something.

Lets first create a empty directory. I will name it “Useless Bot“.

Add two file “package.json” and “index.js”.

package.json:

{
"name": "useless-bot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Your Name",
"license": "ISC
}

Installing “discord.js” to your project

To install discord.js, open terminal on your project’s directory and run following command:
npm install discord.js

This command will import some necessary libraries that you will need to create a discord bot.

Writing “index.js”

Now we can start writing codes on “index.js” to make our bot functional. I have written a small block of code which will reply when someone writes a message. You can modify as per your need.

index.js:

const Discord = require('discord.js');
const bot = new Discord.Client();
const token = "##YOUR TOKEN HERE##";
bot.login(token);
bot.on('ready', function(){
console.log('Bot Ready');
});
bot.on('message', function(message){
if(message.content == "Hi"){
message.reply('Hi');
}
});

Testing the bot locally

To run the bot you can easily run “index.js” file. You can use following command to initialize your bot.

node .

As soon as you initialize index.js, the bot will go online on Discord.

Now that your bot is online, Type “Hi” on your channel and the bot will reply with a “Hi”.

Step 3 — Running a discord bot

Creating a repository on GitHub

Go to Github and create a new repository

I have named my repository “Useless Bot”

As soon as you create your repository, you will get a URL to your Repository

Now that you have your repository’s URL. Run following git commands:

git init
git add .
git commit -m "first commit"
git remote add origin YOUR-GIT-URL-HERE
git push -u origin master

Creating an heroku application
Go to heroku.com and create a new app.

Give your bot a unique name.

Select GitHub as your development method.

Do not forget to enable “Automatic Deployment“. Enabling Automatic deployment will help you automatically initializing your app when you make changes to your repository.

Now that we have our app ready we have to let our app know to run this app. For this to to Settings > Buildpacks and Add a buildpack.

Select nodejs and save the changes

Now deploy your App and wait until the deployment is complete.

And now for the final step, go to Resources section[Reload your browser once] and toggle Web and Worker

As you can see that our bot has nothing much to do. You can add functionality to your bot and program it accordingly.

Some useful links:

My Useless Bot : https://github.com/mangitmaharjan/Useless-Bot

Heroku: https://id.heroku.com/login

Discord Developer Portal: https://discord.com/developers/applications

Discord Permission Checker: https://discordapi.com/permissions.html

Happy Programming :)

Shopify | PHP | Javascript | and many more https://mangitmaharjan.com.np/