How to create a Discord bot with Node.js

Updated: December 30, 2023 By: Guest Contributor Post a comment

Introduction

Creating a Discord bot is an engaging way to interact with the Discord community through your programmed logic. Node.js, given its efficient performance and vast npm ecosystem, remains a popular choice for building these bots. This guide will take you through using the latest JavaScript features and Discord.js, a powerful Node.js module, to create your very own Discord bot.

Prerequisites

Before you begin, ensure you have the following prerequisites sorted out:

  • Knowledge of JavaScript or TypeScript
  • Node.js installed on your system
  • A Discord account and a server to test your bot

Setting Up Your Project

First, generate a new Node.js project by creating a new directory, initializing npm, and installing discord.js:

mkdir my-discord-bot 
cd my-discord-bot 
npm init -y 
npm install discord.js

Creating the Bot on Discord

Next, you need to set up a new bot account on Discord’s Developer Portal:

  1. Navigate to https://discord.com/developers/applications and log in.
  2. Click ‘New Application’, give it a name, and create your app.
  3. Go to the ‘Bot’ tab and then click ‘Add Bot’.
  4. Copy your bot’s token securely as this will be used to log in via the code.

Inviting the Bot to Your Server

After your bot is created, you need to invite it to a Discord server:

  1. In the Developer Portal, navigate to the ‘OAuth2’ tab.
  2. Under ‘Scopes’, select ‘bot’.
  3. In the ‘Bot permissions’ section, select the permissions your bot will need.
  4. Copy the generated URL and open it in a web browser to invite your bot to a Discord server where you have permissions to add bots.

Coding the Bot

With the bot created and added to your server, you now need to set up the Node.js application. Create a new file called ‘bot.js’ and import discord.js:

import { Client, Intents } from 'discord.js';
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.once('ready', () => {
 console.log('Ready!');
});

Handling Messages

To have your bot do something when it sees a message, you can listen to the ‘messageCreate’ event:

client.on('messageCreate', message => {
 if (message.content === '!ping') {
   message.channel.send('Pong!');
 }
});

Logging In the Bot

Use the token to log the bot in:

client.login('your-bot-token');

Replace ‘your-bot-token’ with the token you copied from your bot’s Discord Developer page. *Ensure you keep this token private*.

Final Bot Code

The following code registers a bot that logs in to Discord and responds with ‘Pong!’ whenever someone types ‘!ping’ in a channel:

import { Client, Intents } from 'discord.js';
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

// Bot is ready
client.once('ready', () => {
 console.log('Ready!');
});

// Message handling
client.on('messageCreate', message => {
 if (message.content === '!ping') {
   message.channel.send('Pong!');
 }
});

// Bot login
client.login('your-bot-token'); 

Conclusion

Creating a Discord bot with Node.js is straightforward once you set up the Discord application and write some basic event-driven JavaScript. With the foundation laid out in this guide, you can continue to expand your bot’s functionality to include features like command handling, rich interactions, and real-time data processing. As you delve deeper into Discord bot development, always remember to responsibly manage sensitive data like your bot’s token and respect Discord’s rate limits and user privacy.