Getting Started with Discord Bots using Discord.js

June 08, 2023

Discord 3d Icon Concept. Dark Mode Style.

Photo Credit: Alexander Shatov

Introduction

Discord is a popular communication platform among gamers and communities. Discord bots are automated programs that can perform various tasks on Discord servers, such as moderating content, playing music, and much more. In this tutorial, we will be using Discord.js, a powerful Node.js module that allows you to interact with the Discord API.

Prerequisites

Before we get started, make sure you have the following:

  • Node.js installed on your computer
  • A Discord account
  • A Discord server where you have the necessary permissions to add a bot

Setting up the Discord Bot

  1. Open the Discord Developer Portal and create a new application.
  2. Give your bot a name and a profile picture.
  3. Go to the Bot section and click on "Add Bot".
  4. Set a username for your bot and enable the "Presence Intent" and "Server Members Intent".
  5. Copy the bot token as we will need it later.
  6. Go to the OAuth2 section and under the "Scopes" section, select "bot".
  7. Under the "Bot Permissions" section, select the permissions you want your bot to have on the server.
  8. Copy the generated OAuth2 URL and paste it into your web browser.
  9. Follow the prompts to select the server you want to add the bot to.
  10. Complete the verification process by solving the captcha.
  11. Your bot should now be added to the selected server.

Creating a Node.js Project

  1. Create a new folder for your project and navigate to it in the terminal.
  2. Initialize a new Node.js project by running npm init and following the prompts.
  3. Install Discord.js by running npm install discord.js.

Writing the Code

  1. Create a new file called index.js.
  2. Require the Discord.js module by adding the following line to the top of your file:
    const { Client, GatewayIntentBits } = require("discord.js");
    
  3. Create a new instance of the Discord client by adding the following line:
    const client = new Client({
      intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildVoiceStates,
      ],
    });
    
  4. Add an event listener for when the bot is ready:
    client.on("ready", () => {
      console.log(`Logged in as ${client.user.tag}!`);
    });
    
  5. Log in to the Discord API using the bot token:
  • create a new file called config.json add the token that you copied earlier as follows,

    {
      "token": "your-bot-token-goes-here"
    }
    
  • after this import the token to index.js file by adding the following line

    const config = require("./config.json");
    
  • and add the following code at end of index.js to implement login

    client.login(config.token);
    

After implementing all the steps index.js will look like,

const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildVoiceStates,
  ],
});

const config = require("./config.json");

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.login(config.token);
  1. Run the code by running node index.js in the terminal.

Congratulations, you have successfully created your first Discord bot using Discord.js! In the next part of this tutorial, we will be exploring how to add more functionality to your bot.