How to Create a Discord Bot?

For many years, Discord has shown that it is focused on continuing to improve to offer the best possible experience to its users, becoming a platform that is now not only used by gamers. It is now one of the most used platforms for message, video calls and streaming between friends.

Discord’s features have been growing as the development team has been improving the platform, but without a doubt the bots are one of the most special additions we can find in this platform.

how to create a discord bot

With a Discord bot users can do almost anything with a JavaScript code. But if you don’t have any programming knowledge you don’t have to worry. In this article we will guide you step by step so that you can create your own Discord bot even if you are not a programming expert.

1.- Download Node.js and prepare your Discord account

Node.js will allow you to run JavaScript for free as it is an open source program, which is completely necessary for your bot to work. You can easily download it from the nodejs.org (here) page and you have to install it before starting the rest of the process.

As you might imagine, you will also need a Discord account and your own server to be able to test your bot. If you still don’t have a Discord account for some reason, you can easily do it from the discord.com page. If you already have an account you will simply have to log in to your account and open the server on which you want to bring your bot to life.

You will also need a program like Notepad++ in Windows to be able to work more comfortably and effectively with your bot code.

2.- Create your own bot

Now you will have to create an “application” on Discord to make your bot work. This can be done very simply, as the process is not at all complex. The main goal here is to obtain an “authorization token” so that Discord can recognize the code and add your bot on your own servers.

create your discord bot

To do this, you have to start by going to discordapp.com/developers/applications/me (here). You should be inside your account so you can go directly to your application list configuration. Here you simply have to select New App to get started.

You will have to give your bot a name and then you will have to select the Save Changes option.

For the next step you have to go to the right menu and select Button. When you see the new menu, select the Add bot option under the Build-a-bot option. If you only have one application (the one we just made) then it will be selected automatically. Otherwise you will have to select it.

3.- Obtain the authorization token for your bot

In the App Bot User box, you have to look for the word Token: Click to reveal. Once you click here you will see a line of text. This text will be the authorization token for your bot, which allows you to send the code.

get-token-authorization-bot-discord

It is very important that you do not share this token with anyone, since the token allows anyone who has it to create code for the bot, which means that anyone else who has it will have control over your bot.

In case you think your token has been compromised, you can easily generate a new token. In fact, it is recommended that you create a different token each month by selecting the Generate New Token option. After doing this you will have your new token in a few seconds.

4.- Send your bot to your server

Now you must go to the part where it says App Details and find your Client ID, which is a fairly long number. Copy that number and add it to the following URL replacing the part that says CLIENTID:

send-bot-a-server-discord

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

Remember to remove the part that says “CLIENTID” and put in its place your real Client ID that you have obtained from Discord.

Now that you have made this change, copy the URL that already has your Client ID in your browser to enter the page. This will take you to the Discord page so you can send your bot to it.

You will know that everything has gone well if the page sends you to your application or to your server. The channel will say that a bot has entered the room and you will now be able to see it in the menu on the right side.

5.- Create a “bot” folder on your computer

After doing the above steps, it is also highly recommended that you create a folder on your computer, this being a very easy way to replace your bot files.

create-folder-bot-discord-in-your-pc

Simply call “MyBot” or “DiscordBot” so you don’t forget what the content inside is.

6.- Open the text editor to make your bot files

Now you will have to create three files in the text editor of your choice (you can use the one we recommend previously).

In the first file you will have to copy this code:

{

“token”: “Your Bot Token”

}

In this code you will have to replace the text “Your Bot Token” with the token you generated in the previous steps for your bot. Make sure the token is inside the quotes. Now save the file in the folder you created for your Discord bot. The file has to be named auth.json.

It is very important that you do not save the file as . txt, but that you save it as . json. Now create a second file and add the following code:

{

“name”: “greeter-bot”,

“version”: “1.0.0”,

“description”: “My First Discord Bot”, “main”: “bot.js”,

“author”: “Your Name”, “dependencies”: {}

}

This time you will have to replace “Your Name” with your name, plus you can also change the “description” part by adding whatever you want about your bot. This can be useful for you to remember what your bot is supposed to do.

Save this file with the name package.json in the folder we created in the previous step.

7.- Define your bot code

We still need to create a file, which is the most important because it will be in charge of controlling the behavior of your bot.

define bot code discord

To really have control over your bot you will need to be familiar with JavaScript, but if you are completely new to programming and just want to do something, you can copy and paste the following code to make a simple bot that can welcome people to your server.

Start of the code

var Discord = require(‘discord.io’);

var logger = require(‘winston’);

var auth = require(‘./auth.json’);

// Configure logger settings

logger.remove(logger.transports.Console);

logger.add(new logger.transports.Console, {

    colorize: true

});

logger.level = ‘debug’;

// Initialize Discord Bot

var bot = new Discord.Client({

   token: auth.token,

   autorun: true

});

bot.on(‘ready’, function (evt) {

    logger.info(‘Connected’);

    logger.info(‘Logged in as: ‘);

    logger.info(bot.username + ‘ – (‘ + bot.id + ‘)’);

});

bot.on(‘message’, function (user, userID, channelID, message, evt) {

    // Our bot needs to know if it will execute a command

    // It will listen for messages that will start with `!`

    if (message.substring(0, 1) == ‘!’) {

        var args = message.substring(1).split(‘ ‘);

        var cmd = args[0];

        args = args.splice(1);

        switch(cmd) {

            // !ping

            case ‘ping’:

                bot.sendMessage({

                    to: channelID,

                    message: ‘Pong!’

                });

            break;

            // Just add any case commands if you want to..

         }

     }

});

End of code

As mentioned above, this code makes your bot able to respond to certain messages, specifically those that start with a “!” character.

That is, we are programming a bot to be able to respond to the “! intro” command, so if someone writes this to the server while the bot is on it, it will respond with a pre-programmed message.

In the code we shared earlier you can see that the message is defined as “Greetings! welcome to the server!” You can change this message to whatever greetings you want to give. Just make sure you keep the quotes intact.

Save this file with the name bot.js in the Discord folder we created.

8.- Open the command prompt on your computer and navigate to the Discord folder

In Windows you can open the command prompt or the console very easily, you just have to click on Start and start typing “Command prompt” and then select it in the results that appear.

open-command-prompt-computer

Once it is open, type “cd” followed by the path to your folder. You can see it very easily by navigating to the folder and holding down the shift key while right clicking on a blank area of the folder, then you can select the option “Open the command prompt here” or “Open PowerShell here”, depending on the console you use.

9.- Use the command prompt to install your bot

Now you will have to use Node.js. At the command prompt, with your bot’s folder path, type the following: “npm install discord.io winston -save.”

This will automatically install the files that were inside the folder we created for the Discord bot earlier.

Likewise, you must use the following command line to be able to install some complementary dependencies:

https://github.com/woor/discord.io/tarball/gateway_v6

This should give you all the files you need.

10.- Running the bot

This should be all and now your bot should be working with complete normality. To try to run your bot, you must type “node bot.js” at the Command Prompt (be sure to keep navigating the path of your Discord folder).

run-bot-discord

Now go back to your Discord server and try to test your bot by typing “!intro” or simply “!” followed by any message that is in the “bot.js” file.

If everything went well, you will now receive a reply message from your bot.

Remember that Discord has a very large user community that is constantly creating new things, including bots. So you will be able to create a lot of bots created by other users. If you are going to use them, just make sure you give them the recognition they deserve.