How to Make a Discord Bot: Simplest Guide

Discord is a free communication tool that lets you gather people in a group and chat in both text and voice calls. A Discord bot is a great way to increase the server’s productivity. It lets you fetch essential data, schedule events, deliver notifications, moderate conversations, and much more. Moderators tell that Discord is the best way to automate tedious tasks. Here we will briefly walk you through how to make a discord bot.

The best part of creating a Discord bot is that you need not have a Computer Science Degree; Instead, learning the basic concepts of coding and going through the given step-by-step instructions will help you build your own bot.

In this tutorial, we will look through the process of building a Discord bot by coding in JavaScript. We will begin by creating a Discord server, creating a bot application, and then inviting the bot to the server. Then we will power the bot by coding with JavaScript (using node.js). By the end, you will have full control over your bot and can be invited to any server.

Doing all the prep work to create your bot – How to make a discord bot

Step 1: Register and Login

To create a Discord server for your chatbot, you will have to create a Discord account. The very first step you would need to do is to create your login credentials on the Register page and then provide your email address and password credentials and register.

Go to https://discord.com/login

Step 2: Create a server

Once you are logged in, the next step is to create a server. Click on the plus sign that is available on the left side of the screen. You would be asked whether to create a server or to join a server. This will be the important part for the creation of discord bot.

Click on “Create a Server” and name your server. You can also change your server image and region if desired. I would recommend choosing the closest region possible for faster access.

After your server is created, it should appear on the left side on the server list. Click on it to see a general channel (in your Text Channel section), and on the member list on the right side, you should see your user.

Step 3: Create your application

Now you have created your very own server; you will have to register your application with Discord.

Go to the Discord developer portal at https://discord.com/developers/applications and click on the “New Application” button on your screen’s right side.

Try giving a unique name for your application and register your application to your server.

Discord Bot Application - New

Now in the General Information page, fill out the name of your application. By default, it is shown as “My Application,” and now you could give it a custom name.

Step 4: Create a bot for your application

On this guide, how to make a discord bot, we now will go forward to create a new application. Click on the “Bot” section in the Settings menu and choose “Add Bot.” Change the bot’s name if you want to and give your custom name.

Discord Bot - Add Bot

In the “Token” section, click on “Click to Reveal” and then click on the “Copy” button to copy the generated token.

The token is your username and password combined into a single string, and it is necessary to keep it a secret as anyone who has your token can control all the features and data in your chatbot.

Editor’s Choice:

Click on “Regenerate” and regenerate a new token if you wish to. The token will be used in the source code and so you will have to copy and save it.

Step 5: Inviting Bot to your server

After you have registered your application and created a bot for your application, you are ready to invite (or Authorize) your bot for your server.

The Bot can be invited to many other servers, and here we will invite the Bot to our server created. Click on “OAuth2” and scroll down to the “Scope” section. Check on the “bot” scope and copy the generated URL.

Paste the generated URL in your web browser, and you will be asked which server you want to invite your bot to. Choose the server you have created and click “Authorize.”

You can see that your bot has successfully joined your server in the member list as an offline user when you see the Authorized success screen.

Offline - My ChatBot Discord

The same URL that you have used to authorize the bot can be shared with others to add the bot to their server. They’ll be able to interact with your bot but cannot control it.

Now all the prep work for your bot is done; Fortunately, there is no need to follow any of the above steps, and it will always stay the same.

After following till all the steps, how to make a discord bot, we will go over some simple line of codes.

Now you can move to the JavaScript code!

Implementing some basic code – How to make a discord bot

Before diving into the code let’s check whether we have the latest version of node.js installed in our system

Open the ‘Command Prompt’ in your system and type “node –version.” The newest version provided is version 14 and make sure you get a response of the mentioned version in the command prompt.

Create a New Folder in your preferred location or use an already existing folder to add on all the contents in making our chatbot.

Now open the Visual Studio Code Editor (or if you want, you can use your preferred editor) and click on File -> Open Folder.

Now hover your mouse over the folder name and click on the New File icon and create a file with your preferred name (here, we have named it as mybot.js).

The first step in the process is to install the Discord module, and it’s dependencies; Go to View -> Terminal and in the Terminal section; It should automatically open you to the directory where the folder has been created.

Now in the Terminal type, the command ‘npm install discord.js’; You would see some warnings, which is fine unless you see an error. The warnings tell you the optional modules that you can install. The version of the discord module will be displayed in the command prompt, and now you are good to go with the code.

1. Installing Discord module and triggering the bot online

Let’s write a simple program to ensure that everything required is installed properly, and the bot user is working correctly.

We will load the discord.js module into our code and then connect our bot. Then let’s write a few lines of code that would trigger your bot to be online. When the bot successfully connects, it should print a message saying that is has connected successfully.

In the file, write the code given below.

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

client.on('ready',() =>
console.log('Logged in as $client.user.tag!');
);


client.login("NzIwOTk4NzUwMDEyNzAyNzYx.XuzbwA.Wat3AJNCb-tvXzIx3ITLCkzMR3I");

In the client.login statement, you will have called the bot’s secret token as the parameter to login into the chatbot.

Now Go to Terminal and run your code by typing the command ‘node mybot.js’. The output should say Logged in as <Username> as shown in the picture below in the Terminal section.

If you check your server, your bot should appear as an online user in the member list of the server panel. Congrats that you have installed everything correctly, the bot has been appropriately configured and now most of your legwork is done.

 Press Ctrl+C to terminate the code; now, your bot would go again offline. It would appear online for some time before it goes offline, and it’s quite normal.

At this point, how to make a discord bot, we hope you have been enjoying. Let’s dive into some more fun things.

We will now do some code to make fun activities in our bot!

2. Updating and Changing Bot’s Activity

Let’s write a few lines of code to update the status of our bot saying “Watching Discord Bot Tutorial”.

Four status activities can be updated – ‘Playing’, ‘Watching’, ‘Listening to’, and ‘Streaming’ (Refer to the documentation to know more – https://discord.js.org/#/docs/main/stable/general/welcome).

Here in the client.user.setActivity function lets pass the string which says type to be mentioned as ‘Watching’.

const Discord = require ('discord.js');
const client = new Discord.Client();
client.on('ready',() => 
console.log(`Connected as $client.user.tag!`); client.user.setActivity('Discord Bot Tutorial', type: 'WATCHING');
);

client.login(“NzIwOTk4NzUwMDEyNzAyNzYx.XuzbwA.Wat3AJNCb-tvXzIx3ITLCkzMR3I”);

Again, open the Terminal section and run the code with the command ‘node mybot.js’. Now it would show the bot connected just like our previous step. Head to the server and in the member list you could see the updated status below the bot’s name.

Changing the bot’s status is very simple. You will have to call the client.user.setActivity() to change the status of your bot. The default is Playing <Mentioned Activity>, and you can mention the other verbs (Watching, Listening to, or Streaming) in the type.

3. Sending and replying a message

We will now use our chatbot is to send a message and get a response from the bot. Let’s write a simple code where the bot replies with a ‘Hello!’ when you say ‘Hi’.

const Discord = require ('discord.js');
const client = new Discord.Client();
client.on('ready',() => 
    console.log(`Connected as $client.user.tag!`);
    client.user.setActivity('Discord Bot Tutorial',type:'WATCHING');
);
client.on('message', msg => 
    if (msg.content === 'Hi') 
      msg.reply('Hello!');
    
  );
client.login("NzIwOTk4NzUwMDEyNzAyNzYx.XuzbwA.Wat3AJNCb-tvXzIx3ITLCkzMR3I");

The client.on function for the messaging process calls the attribute msg to look out for a message and give an automated reply.

Here the message received is ‘Hi’, and there is an automated reply from the bot saying @<Member name> ‘Hello!’.

The Discord is an all-in-one voice and text platform for gamers and other communities, free, available as both desktop and mobile applications, and is much easier to build. After going through this tutorial, you will be able to perform the very basic functions of changing the bot’s status activity, sending and replying to a message. With these building blocks, you can create applications that involve your required functions hassle-free. Hope you have liked our simplest guide on how to make a discord bot. Stay tuned for related content at SwitchGeek.

Leave a Reply