How to build a simple Discord bot using Node.js ? Last Updated : 02 Aug, 2022 Summarize Comments Improve Suggest changes Share 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 A ayushharwani2011 Follow Improve Article Tags : Web Technologies Node.js Node.js-Misc Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read REST API Introduction REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server. 7 min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel 7 min read Like