If you’ve been searching around and looking at some other Discord bot creation guides, you’ve likely noticed that nearly all of them are written in… JavaScript.

Using JavaScript (with node.js, specifically) is a trend because it’s easy to setup compared to more rigid development environments, and there’s no particular IDE that you have to use to make it work.

JavaScript is a great language for beginners to pick up and make their first Discord bot in, particularly because there’s a ton of learning content out there.

No matter where you get stuck, you’ll always be able to find an answer!

Today we’re going to go over how you can build a Discord bot from scratch using JavaScript and Node.js.

Choosing an Easy to Use JS Library for Your Discord Bot

To make this process a lot easier on ourselves we’re going to use a library that helps us talk to the Discord API in JavaScript.

For this guide in particular, one of the easiest libraries I’ve found to use for JS / Node projects is discord.js.

Discord.js is going to significantly simplify your code and make it much easier to get projects up and running as quickly as possible.

We’re going to add discord.js to your project in the next section of this guide where you actually start coding your bot.

Before that, we’ll create your bot application in Discord and generate a Token that’ll let your bot communicate with the Discord API.

Creating Your Discord Bot Application and Generating a Token

Creating your bot application and getting a custom token assigned to your bot is the first step you’ll need to take to bring your bot to life.

Once you register your bot on Discord, you can start inviting it to your channels. Once it’s invited to your channel, then it can communicate with your node.js code and actually start to function.

You’re going to want to follow along with this Discord Bot Token Guide.

It’ll show you, step by step, how to create your application in Discord (and add an awesome icon to it!)

Then it’ll walk you through the process of retrieving your token and inviting your bot to whatever server you choose.

Once you’ve got your Discord bot token created, it’s time to move into setting up your development environment and actually programming your bot!

Important: A bot token is essentially the “key” to controlling your Discord bot. This is meant to be kept secret! Do not under any circumstances share your token with anyone. A token leak scenario can be a real nightmare — something you’d never want happening with your bot.

Developing Your Discord Bot in JavaScript / Node.js

Now we get to the fun part: actual development and real-life coding! Let’s walk through how to get your bot up and running ASAP.

Installing Node.js and Discord.js

The first thing we’re going to do is set up node.js and discord.js on our machine. This can be easily done on Windows, Mac, and Linux; as a general note, everything JavaScript/Node.js related will be platform-independent.

Follow along with this guide to Setting Up Node.js and Discord.js on your machine.

Installing Node.js and Discord.js

In this section you’ll go through the process of installing node.js on Windows, macOS, and Linux.

You’ll learn how to create a project folder and use npm (node package manager) to install discord.js in your project.

Creating Your Bot’s Code Framework

Once you’ve gotten node.js installed and discord.js included in your project, you’re ready to start writing some actual code.

Follow along step by step with this guide to getting your bot’s code framework in place.

Running Discord Bot Tutorial

You’ll choose a code editor if your choice, with the most popular options being Visual Studio Code, Atom, or Sublime Text.

Personally, I use Visual Studio Code and Atom for most of my projects and prefer Atom a bit more due to its speed, usability, and clean look and feel.

Once you’ve created a new file in your project and loaded it up in your code editor, you’ll begin by writing a simple bit of code that logs your bot into your server:

const Discord = require('discord.js');
const client = new Discord.Client();

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

client.login('your-token-goes-here');

We include the discord.js library in our project and create a new instance of the discord.js “client” object.

Once our bot is logged in (‘ready’), then we’ll write a message to our node.js console window that says, ‘Ready!’

If you look down at the last line, that’s where the token you generated earlier goes, and it’s what allows your bot to communicate through the Discord API.

As you continue going through the guide, you’ll learn how to listen for chat messages and actually reply to a chat message, in turn creating a very simple “ping-pong” bot.

Next Steps With JavaScript and Node.js Discord Bot Development

Congratulations! If you’ve gotten this far, you know how to create a bot from scratch in JavaScript / Node.js.

What should you do next?

Well, as a first step, I’d recommend that you go down to the “Discord Bot Ideas” section of this guide and go through the idea generation process to come up with an idea for the bot you want to make next.

Once you’ve done that, I strongly recommend that you read through all of the guide and topics over at discordjs.guide. They have some excellent content out there and cover everything from command handling to permissions to databases.

And that’s all there is to it.

Get out there, create your next project, and you’ll have a working Discord bot up and running before you know it!

Leave a Reply