How to build a simple Discord bot using Node.js ?

03 Nov, 2020
Discord is an instant messaging application mostly used by developers and gamer communities. Many discord servers use bots to automate the task. Bots are programs that allow us to automate some tasks like messaging, maintaining our server, etc. Discord provides us with many built-in bots. Discord also allows us to build our own bots.
For javascript developers, discord provides discord.js package which can help them to develop bot for their server.
Prerequisites:
Discord account with your own discord server.
Node.js with npm installed.
Basic knowledge of Javascript.
Steps to build Discord Bot:
Create your Bot: To register your bot visit https://discord.com/developers/applications/ and log in with your account.Click on the “New Application” button and give name to your application. Then, click the “Create” button to create an application which uses Discord API.Click on the bot tab and then, click on “Add Bot” button to create a new bot. Give a name and avatar to your bot of your choice. Add bot to your server: To add bot to your server, you should use following URL:https://discord.com/oauth2/authorize?client_id=CLIENT_ID&scope=botIn the URL, you should replace CLIENT_ID with your own client id that you can find on the “General Information” tab. Visit that URL and choose the server to add it to and then click on “Authorize” button, this will put your bot in your server.Project setup: To start building a project, create a new folder and then create a new file named index.js. Then, install discord.js package by using following command:npm i discord.jsThen import discord.js package in your project using following code:const discord = require('discord.js');Now, we want our bot to send a message “Hello Geeks!!” whenever someone on the server sends “hello”. So, to do that we require a discord client which can handle the event. Discord client allows you to listen for a message event. This means the bot can read any message that is sent to a channel.Filename: index.js// Creates a discord clientconst client = new discord.Client();   // Runs whenever a message is sentclient.on("message", message => {       // Checks if the message says "hello"    if (message.content === "hello") {           // Sending custom message to the channel        message.channel.send("Hello Geeks!!");     }});To start the bot, we have to add client.login(YOUR_BOT_TOKEN) call in index.js file. client.login("YOUR_BOT_TOKEN"); // Starts the bot upReplace YOUR_BOT_TOKEN with your bot-token which you can find in Bot tab. So, after following above steps, our final index.js file will look like this:Filename: index.js// Requiring moduleconst discord = require('discord.js');  // Creates a discord clientconst client = new discord.Client();   // Runs whenever a message is sentclient.on("message", message => {       // Checks if the message says "hello"    if (message.content === "hello") {           // Sending custom message to the channel        message.channel.send("Hello Geeks!!");     }});  client.login("YOUR_BOT_TOKEN");Run your index.js file to run your bot: To run index.js file, use the following command in your terminal:node index.js 
Create your Bot: To register your bot visit https://discord.com/developers/applications/ and log in with your account.Click on the “New Application” button and give name to your application. Then, click the “Create” button to create an application which uses Discord API.Click on the bot tab and then, click on “Add Bot” button to create a new bot. Give a name and avatar to your bot of your choice. 
Create your Bot: To register your bot visit https://discord.com/developers/applications/ and log in with your account.
Click on the “New Application” button and give name to your application. Then, click the “Create” button to create an application which uses Discord API.
Click on the bot tab and then, click on “Add Bot” button to create a new bot. 
Give a name and avatar to your bot of your choice. 
Add bot to your server: To add bot to your server, you should use following URL:https://discord.com/oauth2/authorize?client_id=CLIENT_ID&scope=botIn the URL, you should replace CLIENT_ID with your own client id that you can find on the “General Information” tab. Visit that URL and choose the server to add it to and then click on “Authorize” button, this will put your bot in your server.
Add bot to your server: To add bot to your server, you should use following URL:https://discord.com/oauth2/authorize?client_id=CLIENT_ID&scope=bot
In the URL, you should replace CLIENT_ID with your own client id that you can find on the “General Information” tab. Visit that URL and choose the server to add it to and then click on “Authorize” button, this will put your bot in your server.
Project setup: To start building a project, create a new folder and then create a new file named index.js. Then, install discord.js package by using following command:npm i discord.jsThen import discord.js package in your project using following code:const discord = require('discord.js');Now, we want our bot to send a message “Hello Geeks!!” whenever someone on the server sends “hello”. So, to do that we require a discord client which can handle the event. Discord client allows you to listen for a message event. This means the bot can read any message that is sent to a channel.Filename: index.js// Creates a discord clientconst client = new discord.Client();   // Runs whenever a message is sentclient.on("message", message => {       // Checks if the message says "hello"    if (message.content === "hello") {           // Sending custom message to the channel        message.channel.send("Hello Geeks!!");     }});To start the bot, we have to add client.login(YOUR_BOT_TOKEN) call in index.js file. client.login("YOUR_BOT_TOKEN"); // Starts the bot upReplace YOUR_BOT_TOKEN with your bot-token which you can find in Bot tab. So, after following above steps, our final index.js file will look like this:Filename: index.js// Requiring moduleconst discord = require('discord.js');  // Creates a discord clientconst client = new discord.Client();   // Runs whenever a message is sentclient.on("message", message => {       // Checks if the message says "hello"    if (message.content === "hello") {           // Sending custom message to the channel        message.channel.send("Hello Geeks!!");     }});  client.login("YOUR_BOT_TOKEN");
Project setup: To start building a project, create a new folder and then create a new file named index.js. Then, install discord.js package by using following command:
npm i discord.js
Then import discord.js package in your project using following code:
const discord = require('discord.js');
Now, we want our bot to send a message “Hello Geeks!!” whenever someone on the server sends “hello”. So, to do that we require a discord client which can handle the event. Discord client allows you to listen for a message event. This means the bot can read any message that is sent to a channel.
Filename: index.js
// Creates a discord clientconst client = new discord.Client();   // Runs whenever a message is sentclient.on("message", message => {       // Checks if the message says "hello"    if (message.content === "hello") {           // Sending custom message to the channel        message.channel.send("Hello Geeks!!");     }});
To start the bot, we have to add client.login(YOUR_BOT_TOKEN) call in index.js file. 
client.login("YOUR_BOT_TOKEN"); // Starts the bot up
Replace YOUR_BOT_TOKEN with your bot-token which you can find in Bot tab. 
So, after following above steps, our final index.js file will look like this:Filename: index.js
// Requiring moduleconst discord = require('discord.js');  // Creates a discord clientconst client = new discord.Client();   // Runs whenever a message is sentclient.on("message", message => {       // Checks if the message says "hello"    if (message.content === "hello") {           // Sending custom message to the channel        message.channel.send("Hello Geeks!!");     }});  client.login("YOUR_BOT_TOKEN");
Run your index.js file to run your bot: To run index.js file, use the following command in your terminal:node index.js 
Run your index.js file to run your bot: To run index.js file, use the following command in your terminal:
node index.js 
Note: Whenever our index.js will stop running, our bot will also stop working. If you want your bot to work 24X7, you must deploy it to some server.
Node.js-Misc
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Installation of Node.js on Windows
JWT Authentication with Node.js
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Mongoose find() Function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React