How to Make a Discord Bot

Table of Contents

In this Discord bot tutorial, we will start by discussing the Discord user interface and step by step tutorial on how to make a Discord bot.

Discord is a real-time messaging platform that bills itself as the “universal voice and text chat for gamers” because of its treatment interface, ease of use, and wide range of features, Discord has grown rapidly and is increasingly popular even among those less interested in video games. Between May 2017 and May 2018, its user base has grown from more than twice daily users to more than Slack to more than 130 million to 45 million users.

The requirement of creating a Discord bot

  • Basic Knowledge with JavaScript.
  • Basic knowledge of using your command line (windows) or terminal (Mac/Linux).
  • You must have Node JS installed.
  • You must have Code Editor installed (eg: Atom, Visual Studio Code, Sublime Text).
  • And a Discord Account and Desktop Client.

Time needed: 10 minutes.

How to make a Discord bot

  1. Download & Install Node.js Node.js is a JavaScript runtime that is free and open-source and you will need it to work your bot, eh. Download it at nodejs.org and install it before starting anything else.
    Obviously, you also need a discord account and your own server to test your bot. If you haven’t a Discord account. Then go to DiscordApp.com and create one. If you have it, log in to your account and open the server you want your bot to live on.
  2. Create your botNow you need to create an “app” in Discord for your bot to work. It takes a while to do this, but it’s not too complicated. The goal here is to get an “authorization token” for the bot so that Discord recognizes your code and adds it to the bot on its server.

    First, go to discordapp.com/developers/applications/me. Your account should be logged in, so you’ll go straight to the list of applications for your account. Hit the new app to get started. Give the bot a name, then press the button marked Save Changes.

    Now, in the menu on the right, click Bot. Once in the new menu, click Add Bot under the build-a-bot option. If you only have one app – which we’ve just created – it should appear automatically. Otherwise, select it.
  3. Get your bot’s authorization tokenLook for the token word in the box marked User App Bot: Click to publish. Click on that link and you will publish a string of text. This is the token of your bot’s approval, allowing you to transmit the code.

    Do not share it with anyone – this token allows anyone who has the code to create a code for the bot, meaning anyone who has it can control your bot. If you think the token has been compromised, the good news is that you can easily create a new one with the link below the token that says “Generate a new token”.

    You will need this token in just a second.
  4. Send your bot to your serverNow scroll to the box detailing the app and look for your Client ID, a long number. Copy the number and add the URL to it, in place of the client word.

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

    The final URL should look like this, but instead of this duplicate with your client ID number:
    https://discordapp.com/oauth2/authorize?&client_id=000000000000000001&scope=bot&permissions=8

    Copy the URL of your client ID number into your browser. This will take you to a website where you can tell Discord where to send your boat. If you open an application or discard in your browser and navigate to your server, you know the channel will say a bot has arrived in the room, and we can see it in the menu at the bottom right of the online member list.
  5. Create a “Bot” folder on your computerWhen you’re doing this, you may want to take a moment to create a folder where you can store all your bot files in a place that is easily accessible on your computer. Call it something simple, like “Discordbot” or “Mybot” so you know exactly what it is.
  6. Open your text editor and make your bot’s filesYou are about to create three files for your bot from your text editor. First, paste this code:

    {
    "Token": "Your Bot Token"
    }


    Replace “Your Boat Token” with the token you created earlier on your bot’s application page. Make sure the token is within quotation marks. Then save the file to the Discard Bot folder you created on your desktop using “auth.json”. Remember to save it as a text file. If Jason has .txt instead, it won’t work.

    Create a new file and enter this code:

    {
    "Name": "greeter-bot",
    "Version": "1.0.0",
    "Description": "My first dispute bot",
    "Main": "bot.js",
    "Author": "your name",
    "Dependency": {}
    }


    If you want, replace the author’s name with your name; If you want something more in harmony with what you are creating, you can change the “description” to something other than “My First Discovered Boat” that will work to make your bot think of what to do.

    Save this file as “package.json” to your Discord bot folder.
  7. Define your bot’s codeAnother text file needs to be created and it is important that it controls your bot’s behavior. You want to be familiar with JavaScript to have complete control of your bot and know what you are doing, but if you are new to coding and just want to create something, you can copy and paste this code into the file to create a simple bot that will welcome you to your server.

    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..
    }
    }
    });


    This code sets up a Discord bot that responds to specific messages: specifically, anything “!” character. Specifically, we are programming the bot to respond to the command “!intro”, so if someone writes a bot to your server while typing, the bot will respond to the programmed message.

    In our code, we define the message as “Greetings! Welcome to the server!”. You can replace the prompt message and response message in the code above with their new definition. Be sure to put single quotation marks around the messages.

    Save this last text file as “bot.js” in your Discord bot folder.
  8. Open your computer’s “Terminal/Command Prompt”On a Windows PC, you can easily get to the command prompt by clicking the Windows icon and typing “Command Prompt” in the field. When you open it, follow the path of the file to your folder and type “CD”. On my computer, the command can be found: “DesktopDesktopDiscordBot in the CDUUserFile”. This should change the command prompt line to include the path to the file in your folder.
  9. Use the Terminal/Command Prompt to install your botNow, when using Node.js at the command prompt, type “NPM Install Discord. Winston-Save” on your file path line. It will automatically install the file directly to the folder for your Discord bot.
  10. Run the Discord botThat should be everything. To try running your bot, type “node bot.js” at the command prompt (make sure you are still navigating to your Discord bot folder).

    Now go back to your Discord server and create the prompt message in your “bot.js” file followed by “! Try checking your bot by typing “Intro,” or “!”. If everything goes well, your bot should immediately return your coded message.

    Congratulations! You just create a Discord bot!

Now, go back to your server and make sure you stop running bots locally. Is your bot online? Yes? Congratulations, you’ve just created and installed your first Discord bot.

From here, you can learn a lot. Since you can add a bot to multiple servers at once, the servers are part of the Guilds map object. This object contains individual guilds and a channel list of all those guild objects that contain a list of all information and messages. The API is very deep and can take some time to learn, but at least it is easy to set up and start learning.

Related