How to build a simple Discord bot using Node.js ? Last Updated : 02 Aug, 2022 Comments Improve Suggest changes Like Article Like Report 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. Our Discord Server also let's you connect with the like minded people. Time to be the part of fastest growing tech community! 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=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.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 javascript // Creates a discord client const client = new discord.Client(); // Runs whenever a message is sent client.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 javascript // Requiring module const discord = require('discord.js'); // Creates a discord client const client = new discord.Client(); // Runs whenever a message is sent client.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 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. Comment More infoAdvertise with us Next Article How to build a simple Discord bot using Node.js ? A ayushharwani2011 Follow Improve Article Tags : Web Technologies Node.js Node.js-Misc Similar Reads How to Build a Simple Web Server with Node.js ? Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and itâs not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make 3 min read How to Design Movie Telegram Bot using Node.js ? Telegram bot API can be used to create a chatbot that returns the complete details of movies, web series, and Tv series by sending the name of the movie or series as a command. Telegram provides a bunch of APIâs methods to perform different functions. The telegram bot can be used to know the complet 3 min read How to Create a Simple Server Using ExpressJS? The server plays an important role in the development of the web application. It helps in managing API requests and communication between the client and the backend. ExpressJS is the fast and famous framework of the Node.Js which is used for creating the server.In this article, we will create a simp 3 min read How to use TypeScript to build Node.js API with Express ? TypeScript is a powerful version of JavaScript that incorporates static typing and other features, making it easy to build and maintain large applications. Combined with Node.js and Express, TypeScript can enhance your development experience by providing better type safety and tools. This guide will 4 min read How to Create a Chat App Using socket.io in NodeJS? Socket.io is a JavaScript library that enables real-time, bidirectional, event-based communication between the client and server. It works on top of WebSocket but provides additional features like automatic reconnection, broadcasting, and fallback options.What We Are Going to Create?In this article, 5 min read Like