Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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:

  1. 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. 

  2. 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.

  3. 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 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




    // 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");

    
    

  4. 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.



Last Updated : 02 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads