One of the biggest reasons why Discord is the favorite VoIP for many people is the ability to use bots. Bots add numerous features that aren’t present in the Discord application itself, such as playing music, hosting text-based games, automatic moderation, and much more.
There are bots for nearly every task, but not everything has been done yet. If you have an idea for a Discord bot that would assist you or the millions of other users, you first need to learn how to create one. This may sound daunting, but this guide will demonstrate to you how!
Keep in mind that creating a bot requires at least basic coding knowledge, especially using Python or JavaScript. However, if you follow this guide, it is possible to create a bot without too much existing knowledge.
Table of ContentsShow
Things You Will Need
First of all, you need to prepare before you can start programming an entire bot.
The main prerequisite is to possess at least basic knowledge of Python or JavaScript (we’ll stick to JS in this article). You will also need some kind of code editor. If you are on Windows, your options include Notepad++, Atom, Sublime Text, and others. Mac users should consider using Espresso.
You will also need to have installed the Discord desktop app and Node.js.
Install Node.Js
Before proceeding further, we recommend installing Node.js as soon as you can. Without installing it, it won’t be possible to complete the other steps of this guide.
Download it from the official website and run the installer. The installation process is as straightforward as any other software.
Follow the steps, then open your terminal (CMD on Windows) and type:
node –v.
This command will confirm that Node.js has been successfully installed on your computer. It should report the version of Node.js that is installed. Otherwise, it will return an error.
Install Discord And Create an Account
As you are reading this article, you probably already have Discord installed on your computer and an active account. This is another vital part of the process, so we can’t skip it.
If you don’t already have Discord, 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 need a location to test whether or not your bot is working. 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 your testing server a name and create it, then you’re ready to proceed.
Making A New Application
It’s time to set up a home for your bot. This is 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 function on Discord.
If this is your first time creating a bot, there shouldn’t be anything on your dashboard. Click on New Application, give your bot a name, and click the Bot tab on the left. Click Add Bot, which will show an intimidating prompt. Click Yes, do it!
For now, that is all you need to do on this page. You can leave it for now, but we will return to it later.
Creating The Bot
We have reached the most important part of the process: programming the actual bot.
The first step is to create a new project/folder where the bot’s files will be stored. Think of a name, open cmd/terminal, and type in:
mkdir “your bot name”
(eg. mkdir basic-discord-bot).
You now need to navigate into this newly created folder. This can be accomplished with the command cd. In our example, we would need to type in:
cd basic-discord-bot
Next, you need a .json package file. We can get that by initializing the project with the command:
npm init –y.
You also need a library for Discord API, known as discord.js, which you can install with the command:
npm install discord.js.
Alternatively, use:
npm install --save discord.js dotenv
We will need a file where we can store what’s known as the authorization token. This token will serve as your bot’s unique id.
To create this file via Windows CMD, you can use the command:
call >> .env
For UNIX-based systems, use the command:
touch .env
We now 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.
At the identical webpage we visited earlier, navigate to the Bot tab. Press “Duplicate” or “Click to Uncover Token”. These choices must be directly beneath the bot’s name and its symbol.
With the token copied to your clipboard, open the newly created token.env file. Go to your basic-discord bot folder and open the file with your favorite code editor. In our example, we are using Atom.
Insert:
Discordbot_Token=“paste the bot’s token here”
Go back to your command line and type:
call >> discordbot.js
Go into the project folder, find the newly created .js file, and open it. This will be the foundation of your bot. If you’re coding without any prior JS knowledge, it’s usually best to use someone else’s code.
Here is a template for a Discord bot. Credit to renesansz!
Apply this template to your bot.js, save and exit. We now need to call the bot through the command line. You can do that by typing:
Node discordbot.js
If you did everything right, you should see the same outcome as in the picture above.
If an error appears, check that you have added the token to the .env file and follow the steps again.
Adding Your Bot To A Server
It’s finally time to test whether or not all of your hard work has produced any positive results.
Return to the Developer Portal again and then open the OAuth2 section. This is where you can modify various permissions.
Under Scopes, the only thing you will need to select is bot.
Scroll down a bit further and look at the options under Bot Permissions. This is where you will decide what kind of features/authority the bot will have on your server.
If it is a moderation bot, it will usually have the permissions necessary to kick, ban and manage members.
As we are making something rather easy at the moment, we recommend choosing Read Message History and Send Messages. Once you grasp it and understand how to use JS coding, you can progress to something more advanced.
Scroll back up to Scopes and copy the link displayed right below the options. This link will redirect you to Discord, where you can connect your bot to your server. Authorize the connection.
Now, go back to the project and edit discordbot.js because we still haven’t added any commands. We want to add a piece of code that will make the bot respond to us in Discord.
We’ll take another look at renesansz’s template for a basic reply command.
After 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 instance of discordbot.js. You can also use the Ctrl + C shortcut but be aware that it doesn’t work for everyone, especially in Windows cmd. Navigate back to your project folder and use the same code:
node discordbot.js
You are now logged into the updated .js. It’s time to head back to the Discord desktop app and test what you have created.
In this example, the bot should respond to our message of “Hi!” with a message of “Bye!”
Let’s test it out!
It works! Your new bot should work too!
If this is the result you see, congratulations are in order. You have created your own Discord bot that you can shape into anything you want!
Creating something more advanced will require a lot more investment of time and effort. You will also need to learn and have a deeper understanding of JavaScript.
Still, you now know the basics and have a foundation to continue. Get out there and start creating!