Create and deploy a Discord Bot

Are you interested to make a custom bot for your discord server? Don't worry!! This post will walk you through the basic steps of designing a simple bot using Node.Js.

Note: Your machine should have a NodeJs of version like 12.x.x.
I recommend Visual Studio editor due to the inbuilt powershell feature which makes designing and testing quite easy.

  • Let's start by creating a discord application as shown in the picture. Consider a discord developer application as a project. After creating the application, discord gives access to use the resources for our project/application.


  • After this a box will be prompted on the screen as below.
  • Type a name which suits your motive of designing the discord bot. I will just name it "Test".
  • Now in the left panel select Bot.
  • Create a bot by clicking Add Bot > Yes, do it!


  • Let's switch to the editor now to design our custom bot. Open a blank directory where you want to design the bot. Open Visual Studio Code in this directory.
  • Open a powershell window here and type 
npm init
  • Fill the project details accordingly.
  • Now install these package : discord.js
npm i discord.js
  • Create index.js file which will contain the logic:
const Discord = require('discord.js'); 
const client = new Discord.Client();
client.on('ready', () => {
  console.log(`Bot: ${client.user.tag}!`);
});
client.on('message', msg => {
  msg.reply('hello from my bot!');
});
client.login(YOUR_DISCORD_BOT_TOKEN); 
 
Discord client object takes care of the communication between the user and your logic (It is essentially your bot).
 
  • YOUR_DISCORD_BOT_TOKEN : Copy the discord token from here 

  • In the powershell window type
node index.js
  • As a last step go here and select your bot. Now go to OAuth2 and select "bot" from scopes > "Administrator"  from Bot Permissions. (Selecting Administrator gives all permissions. You can also select required permissions.)

  • Now Copy the url and paste it in the browser and add it to your discord server.
  • To deploy it to heroku add a Procfile in the root of the project directory with the following content:
    worker: node index.js

Check out this cool bot application.
Hope you liked the post. Please like, share and comment. For any queries or suggestions drop an email to nagpalparas5@yahoo.co.uk .

Comments

Popular posts from this blog

Understanding and building Dialogflow agents to handle custom intents

Three ways to deploy Node.Js applications quickly