DEV Community

Cover image for Step-by-Step Guide to Make Discord Bot
Raghu Vijay for Extern Labs Inc.

Posted on • Originally published at externlabs.com

Step-by-Step Guide to Make Discord Bot

Are you all set to create your first Discord Bot ?
I’m guessing you are on this page because you want to create a Discord Bot and so you want to know how, or you are simply here to acquire knowledge on this topic.

Whatever your reason, you have landed on the right page. Here, you will get all the information about Discord Bot and a complete guide on how to make a Discord Bot.

Discord is a popular messaging platform that supports robust, programmable bots. The Discord Bot is ubiquitous, and it offers various services like music, internet searches, gaming moderation, payment processing, and more.

However, before developing and personalizing your bot, you have to set up an application project with Discord and create a listing for your bot.

Moreover, this blog will guide you through all the steps required to list your bot with Discord and how to move it to your personal server.
So, let’s get started!

Bots! What Are These Discord Bots?

Image description
Bot is a short term used for Robots, these are “Automated Robots.” Today bots are the main part of the internet. These automated robots take on the tasks and fulfill them independently. The bots are ubiquitous and take on a range of services like moderation assistance, games, music, internet searches, payment processing, and more.

In the last five or more years, Discord has become an instant messaging platform for not only gamers but everyone who is looking to message, video chat, or live stream with friends and their community online.

And if you have a Discord server, managing your community will be tough without any automation. Thanks to all the tinkerers, the developers have built a massive ecosystem of Discord bots.

Some of the popular bots on Discord are:
The music bot, that automatically plays music.
The voice command bot for voice control.
The all rounder bot to combine various functions
The translating bots.
The statistics bots to analyze server data.

Although Discord Bots are particularly applicable for the gaming community, as they can communicate automatically with a committed server like a Minecraft server, analyze user behavior, and build a connection between Discord and the gaming server.

Moreover, automation is the main reason for the use of the Discord bot,so you can program one to do anything (at least the ones that you can manage in JavaScript code).
Fret not, you don’t need any programming knowledge to get started.

This guide will be enough to get you started making your own Discord bots, even if you've never written a line of code before. Let’s take a look at them first.

Now, these Discord bots are also of various types.

Discord Bot Categories

Image description

  1. Command Bot
  2. Reply Bot
  3. Notification Bot

The Command Bot is initiated by a command, usually starting with a slash. For example, the inbuilt \tableflip command will add the table flipping emoticon to your message.

The Reply Bot will answer specific phrases that you have set in advance. These are mostly used for FAQs or rule reminders.

The Notification Bot will alert a channel about the events and other happenings, like someone starting a stream.

Let’s go through the prerequisites for making a Discord bot.
Things a Discord Development Calls For
Creating a Discord bot is not at all a difficult task. You need a little programming language, just to set things up, although the complexity of the code will depend on the type of bot you are trying to make.
A few of the things that will be required for you to get started are- a Discord account (which you must have). If you don’t have one, you can create one by heading over to the Discord homepage.

You have to set up your own server to use the bot. You can invite the bot to your server in a few simple 2-3 steps.
When the bot has arrived on your server, you have to code a function for it or assign it to another server for which you have admin rights.
For bot function assigning, you will need Node.js to obtain the discord.js Node library. A code editor is also required to write and improve the bot's functions.
Let’s get to the business now-
How to Create and Add a Discord Bot to Your Server
Initially, you have to create an application on Discord so that your bot can work. The idea is to generate a token for your bot so that Discord can identify the code.
To do this, you have to move over to Discord’s applications page. When you log in with your Discord account, you will be able to see a list of your apps. If you see an empty list, don’t worry; you will create one now.

  1. Click the “New Application” button to get started.

Image description

  1. Give a name to your application and press “Create”

Image description

  1. Now, it will direct you to a page where you have to enter details such as app description, add tags, an app icon, and more. Once completed, click the “Save Changes” button to move ahead.

  2. Now, press the "Bot" option on the left sidebar menu. Open the bot page and press the “Add Bot” button. There is the option to grant permission to create the bot when prompted.

Image description

  1. You will be directed to the next page, where you will see a security token for your bot. Now, if the token hasn’t been generated, click on the “Reset” button to generate a new token. Copy this token ID as we will need it in the next steps.

  2. This token will allow you to control the bot, so you should not share this with anyone. Even if you think your token has been compromised, you can reset it by going back to the previous page.

Image description

  1. Look out for the OAuth2 option in the left sidebar menu, click on it and you will get a CLIENT ID. It is a long string number that you have to copy paste to the URL provided below.

https://discordapp.com/oauth2/authorize?&client_id=CLIENTID&scope=bot&permissions=8

Here, replace the CLIENT ID with the actual client ID that you just copied.

  1. Now, paste this URL to the web browser and hit enter. A page will open up where you can tell discord where to send your bot. Choose the server to which you want to add your new bot from a given dropdown menu.

  2. Click on the Continue option and confirm whether you want to give Admin rights to your server or not.

  3. After you add your bot to the server, you will get a message confirming the bot's arrival. This is how you will know if all this process worked or not.

And that’s it, you have successfully created a Discord bot for your server.
Now the bot will stay offline until you define a function for it in code. This is going to be the real work, as you have to set up a few parameters and guide your bot exactly in the direction you want it to go.
Assign Bot Function and Host a Bot

  1. To do this create a new folder on your PC or Mac. Now, open Visual Studio Code and create new file with the code- DISCORD_TOKEN = "Paste your bot's token here without quotes."

In the place of "Paste your bot's token here without quotes,” enter the bot’s token that you copied earlier.

  1. Create a new file and insert the following code. Save this particular file as bot.js in the same folder. Through this code, the function will be added to your Discord bot. And since it is a Ping-Pong Discord bot, it will now reply “Pong” every time you type “Ping”.

require("dotenv").config(); const Discord = require("discord.js"); const client = new Discord. Client({intents: ["GUILDS", "GUILD_MESSAGES"]}); client.on("ready", () => { console.log(Logged in as ${client.user.tag}!) }) client.on("message", msg => { if (msg.content === "ping") { msg.reply("pong"); } }) client.login(process.env.DISCORD_TOKEN);

  1. Next, open the terminal window from the new folder that you created and install the Discord.js library using the below given command. However, it is required to install Node.js on your system

npm install --save discord.js dotenv

  1. Next, is to create a package.json file. For this, use the following command-

npm init -y

  1. Finally, you can use the “node bot.js” command to run the bot.,

  2. After running the node bot.js command, you can see the Discord bot on your server as being online. Now test the bot by typing ping,” to which the bot will respond “pong.”
    Bonus Point: You can create a Discord bot and host it in the cloud. For that, you will require an online IDE like Replit to host your code.

So, this is how you can create a Discord bot with minimal coding.
However, the complexity of this coding can increase depending on the type of function you want to assign to your bot.
There are plenty of useful Discord bots, so there are tons of possibilities to create one new.
So, create one yourself, or if there is something unique that you want to create and it requires complex code, reach out to us, Extern Labs will do the work for you.

Top comments (2)

Collapse
 
thomasbnt profile image
Thomas Bnt ☕

Hello ! Don't hesitate to put colors on your codeblock like this example for have to have a better understanding of your code 😎

console.log('Hello world!');
Enter fullscreen mode Exit fullscreen mode

Example of how to add colors and syntax in codeblocks

Collapse
 
rvijay5 profile image
Raghu Vijay

Thanks! Will keep that in mind. 😉