One of the biggest reasons why Discord is the favorite VoIP for a lot of people is because of bots. Bots add tons of new features that aren’t even present in the app itself—things such as playing music, hosting text-based games, moderating, and a lot more.
However, not everything has been thought of…yet. So, if you have an idea for a Discord bot that would be useful to you or the millions of users on this application, you will need to learn how to make one. It may sound daunting, but we can show you how!
Things You Will Need
First things first, you will need to set up everything before you can start programming an entire bot.
That’s about it. Of course, you’ll need the Discord desktop app and Node.js too.
Before we start, we recommend installing Node.js as soon as you can. You don’t want to skip this step because you won’t be able to continue at all.
Download it from the official website, and run the installer. The installation process is no different than any other regular app.
Follow through the steps and then open your terminal (CMD on Windows) and type:
This command will confirm that Node.js has been successfully installed on your computer. It should report the version of Node JS. If not, it will return an error.
Install Discord And Create Account
Since you are here reading this article, you probably already have Discord on your computer and an account, but as this is another vital part of the process, we can’t skip it.
Download the app from the official website, install it and create an account while you are there. This shouldn’t take more than a few minutes.
Creating A Server
After installing the application, you will need a place to test whether your bot is working. So it’s best to create a brand new server dedicated solely to testing.
To do that, just click on the + icon on the left side of the desktop client, right below Home.
Give it a name and with that, you should be done.
Making A New Application
Now, it’s time to set up a place for your bot. That’s done on Discord’s Developer Portal. Go to https://discord.com/developers/applications/me and log in with your account. This is where you will need to publish your bot to have it work on Discord.
Right now, there shouldn’t be anything on your dashboard, so click on New Application. Give your bot a name and click on the Bot tab on the left. Click on Add Bot, which will show a scary prompt, but click Yes, do it!
For now, that is all you will have to do on this page. Leave it as is because we will come back to it later.
Creating The Bot
We have come to the most important part of the process. Programming the actual bot.
To start, we will need to create a new project/folder where all of the files will be stored. So, think of a name, open cmd/terminal, and type in:
mkdir “your bot name”
(eg. mkdir Basic-discord-bot).
Now, you will need to navigate into this newly created folder. That can be done with the command cd. So, type in:
Next up, you will need a .json package file, and we can get that by initializing the project with the command:
npm init –y.
Additionally, you will need a known library for Discord API, known as discord.js, which you can install with the command:
npm install discord.js.
npm install --save discord.js dotenv
Now, we’ll need a file where we can store what’s known as the authorization token. This token will be used as your bot’s unique id.
To create this file through Windows CMD, you can use the command:
call >> .env
Alternatively, for UNIX-based systems, use the command:
Now, we’ll need to find the authorization token. Fortunately, we already went through the process of creating a new application on the Discord Developer Portal, so let’s head back there.
On the same page we previously left, go to the Bot tab. Click on Copy or Click to Reveal Token. It should be right under the name of the bot and its icon.
With the token in your copy clipboard now, let’s open up the newly created token.env file. Go to your basic-discord bot folder and open the file with your favorite code editor. In our case, we are using Atom.
Discordbot_Token=“paste the bot’s token here”
Go back to your command line and type:
call >> discordbot.js
Head into the project folder, find the newly created .js file, and open it. This will be the foundation of the bot. But, if you’re coding without any previous JS knowledge, it would be best to use someone else’s code.
Apply this template to your bot.js, save and exit. Now, we’ll need to call the bot through the command line. You can do that by typing in:
If you properly followed every step of the way, the results should look like the image above.
If an error shows up, make sure you have properly added the token to the .env file and follow the steps again.
Adding Your Bot To A Server
Now, it’s finally time to try and test whether all of this hard work will provide any positive results.
First, it’s time to head back to the Developer Portal again and then into the OAuth2 tab. This is the place where you can play around with permissions.
Under Scopes, the only thing you will need to select is bot.
Now scroll down a bit further and have a look at the options under Bot Permissions. Here is where you will decide what kind of features/power the bot will have over your server.
If it is a moderation bot, it should probably have the permissions to kick, ban and manage members.
But, since we are creating something pretty simple right now, we suggest selecting Read Message History and Send Messages. Once you get the gist of it and JS coding, you can expand into something more advanced.
Scroll back up to Scopes and copy the link provided right below the options. This link will redirect you to Discord, where you can connect your bot to your server. Authorize the connection.
Now, back to the project and editing discordbot.js because we still haven’t added any command. We want to add a piece of code that will make the bot itself respond in Discord. So, we need a sign of life!
Again, we’ll have a look at renesansdz’s template for a basic reply command.
By adding this part of the code, this is how your entire discordbot.js file should look:
Restart your terminal/command line to cancel your previous run of discordbot.js. Or use the Ctrl + C shortcut. Be aware that it doesn’t work for everyone. Especially in Windows cmd. So, navigate back to your project folder and use the same code:
Since you are now logged back into the updated .js, it’s time to head back to the Discord desktop app and test what you have created.
In our case, the bot should respond to the message “Hi!” and it will respond with “Bye!”
Let’s test it out!
It works! And it should work for you too!
If this is the result you got, then congratulations are in order. You made your own Discord bot that you can now shape into anything you want!
Still, you know the basics and you have the foundation to continue. So get out there and start creating!