How To Build a Simple Discord Bot using Node.js--JavaScript

in GEMS5 years ago

Hello people!

Today we are going to create a discord bot that will respond whenever you call him. This bot is just a very basic practice to understand the Discord.js(Discord API). So without wasting time let's start this tutorial.

  • Login to your Discord account. If you don't have account kindly go to discord website and sign up for free account.

image.png

  • Create a testing server in discord by clicking this + sign.

image.png

  • Now click on Create server.

image.png

  • Just name you sever and click create. Remember this is your server name, not your bot name.

image.png

image.png

  • Click on New Application.

image.png

  • Name your application and click create.

image.png

  • Click on the bot.

image.png

  • Now click on the add bot.

image.png

  • Now our bot is created. We need to invite it to our testing server and later we will need its keys.

image.png

  • Click on the OAuth2 and select bot.

image.png

  • Define the permissions and invite the bot to your channel. As we are developing a basic bot which will only send and read messages so we will grant these permissions only. When you select both options, notice the number has changed for the permissions parameter in the authorization URL.

image.png

  • Now copy the whole URL and paste in a new tab.

image.png

  • On the new window, select the server in which you want to add your bot. I will add it to Discord bot testing because i have created this server just for this post. Click on continue after selecting your server.

image.png

  • After that, the next window will ask for confirmation of permissions. Grant the permission and click next. Further the site will ask for a captcha to prove that you are human. Solve that captcha and your bot will be added to your server.

image.png

In my case, the bot is added to my testing server

image.png

  • But if you notice the bot is offline that because its back-end is empty. It is just like a car without engine so to make the bot functional we need to write a code for it. We will add functionality to the bot with the help of nodejs.

The script.

To run a bot 24/7 we need a server that keeps our bot online. But here I need the bot just for a very short interval of time that's why I am going to run the code on an online compiler.

  • Visit Repl online IDE and start a new repl. Select Javascript.
  • Click on packages .

image.png

  • Search for Discord.js and select the first one. Now click on the add pakage.

image.png

  • Wait for the package to install the console should look like this.

image.png

  • Now in the index.js file start defining the functionality of your bot.
  • For now, we will just write a few lines of code so if the user type hello the bot should replay with Hi! This is a testing bot.
 const Discord = require("discord.js");
const bot=new Discord.Client();
const token="your private token here";








bot.on('ready', () => {
 
  console.log("Bot is online"); // If the code work the bot will online and this message will display on the console.
  })

bot.on('message',msg=>{
    if(msg.content==="hello?"){     //If the message from the user match with these text then the bot will replay with the etxt below.
        msg.reply("hi this a testing bot");
        }})
         bot.login(token);

How to copy your bot token?

Back to Discord developers portal and click on bot.

image.png

  • Do not share this token key with anyone this is just like your password. if anyone has this token they can access your bot so better keep it safe .

image.png

Click on copy token and paste it in the code.

image.png

  • Run the script now.

image.png

As you can see in the console the bot is online and displaying the message we wrote while writing the script.

  • Let's check our bot in discord.

image.png

  • As you can see in the above snap our bot is online. Now let's check if the bot responds.

image.png

  • The bot is responding which means our code is working fine. This is just a basic practice like I said before. We can add a lot of function to it but for now this much is enough.

Contact me on discord for any questions. Pakgamer#8759

See you in the next post. Take care till that

Sort:  

Thanks for sharing your creative and inspirational post on HIVE!



This post got curated by our fellow curator @tibfox and you received a 100% upvote from our non-profit curation service!

Join the official DIYHub community on HIVE and show us more of your amazing work!

Thank you for appricating.