How to Make a Discord Bot

Create a custom bot to run on your Discord server

Image depicting a virtual chatbot

B4LLS / iStock/Getty Images

Discord bots come in all shapes and sizes and can be used for a wide array of purposes that range from moderating user behavior on your server, automatically doling out punishments like muting or banning gamers who step out of line, to playing music for everyone to enjoy. 

The instructions below assume that you already have a Discord account and server in place. If not, you should set this up at discordapp.com before continuing. 

While Discord bots are written in the JavaScript language, you don’t have to be an experienced coder to create them. In fact, the process is a lot less intimidating than you might imagine. Keep reading to learn how to make a Discord Bot that does your bidding.

This tutorial is intended for users running macOS or Windows operating systems.

Download and Install Node.js

Before getting started with Discord bot creation you’ll first want to install Node.js, a JavaScript runtime environment built on Google Chrome’s V8 engine. 

  1. Open a web browser and navigate to the official Node.js download page.

    screenshot of the Node.js download page
  2. Select the appropriate installer package for your particular platform (macOS or Windows) and click on its download link.

  3. Open the downloaded file and follow the on-screen prompts to install Node.js.

  4. Once complete, launch the Command Prompt (Windows) or Terminal (macOS) application.

  5. Type the following text at the prompt and press Enter on your keyboard: node -v

    screenshot of node -v command in macOS Terminal
  6. If a version number is returned, then Node.js is installed correctly. If not, revisit the above steps and ensure that installation is completed.

Create a Discord Application

Now that you’ve gotten the prerequisites out of the way, it’s time to create a new application that your bot can later be added to. 

  1. Open a browser and navigate to the Discord Developer Portal for your server, logging in if necessary.

  2. Click New Application.

    screenshot of the Discord Developer Portal
  3. Enter a name for your new application in the edit field provided and click Create when ready. 

    screenshot of the Create An Application flow in Discord Developer Portal
  4. The General Information screen for your new application should now be displayed, as shown in the accompanying screenshot. Select Bot, found in the left menu pane.

    screenshot of the General Information screen in Discord Developer Portal
  5. Click Add Bot.

    screenshot of Discord Bots screen
  6. A message will now appear, asking if you’re sure you want to add a bot to your application. Click Yes, do it!

    screenshot of the add bot confirmation screen in Discord
  7. Your new bot should now be created, with its information and options displayed in the BUILD-A-BOT section. Select Click to Reveal Token.

    screenshot of the Bots interface in Discord Developer Portal
  8. A long string of characters should now be displayed in place of the aforementioned link. Click Copy to send this token to your clipboard.

  9. Paste this token into a text file for now using Notepad, TextEdit or a similar application. 

You should delete this file and remove it from your Recycle Bin or Trash once you’ve completed this tutorial.

Coding Your Bot

You’ve created a bot and added it to your server. Next comes the fun part, actually coding your bot to do what you want it to.

  1. Launch the Command Prompt (Windows) or Terminal (macOS) application.

  2. Type the following text at the prompt and press Enter or Return on your keyboard: mkdir discord-test-bot

    screenshot of macOS Terminal window

    You can replace discord-test-bot with a name of your choosing.

  3. Next, type the following command to traverse into your newly-created directory: cd discord-test-bot

    screenshot of macOS Terminal window
  4. The command prompt should now be updated, displaying the name of your bot’s folder project folder. Type the following and hit Enter or Return: npm init -y

    screenshot of macOS Terminal window
  5. A file named package.json should now be created in your project folder, as shown in the above screenshot. Type the following at your command prompt and hit Enter or Return: npm install --save discord.js

    screenshot of macOS Terminal window
  6. A list of WARN messages may now be displayed, which can be ignored as long as there are no errors (ERR) shown and the message toward the bottom of your Command Prompt or Terminal window reads “added 7 packages” or “added 8 packages”. Type the following and hit Enter or Return to create the auth.json file: touch auth.json

    screenshot of macOS Terminal window

    If you receive an error message when trying to use the touch command, you may first need to install it by entering the following syntax at your command prompt: npm install touch-cli -g

  7. There will be a confirmation message noting that this file was created, but you can type ls -al (macOS) or dir (Windows) to see the contents of your project directory and confirm for yourself that auth.json is in fact listed. 

    screenshot of macOS terminal window
  8. Launch your preferred code or text editor such as Atom, Notepad or TextEdit, and navigate to your new project folder.

  9. Open the auth.json file and enter the text shown in the accompanying screenshot, replacing AUTH-TOKEN with the authentication token string that you stored earlier in the tutorial. Save the file when done.

    screenshot of auth.json file

    You must copy and paste the entire authentication string within the quotes shown. If you are missing even one character then your bot will not function as expected. 

  10. Return to the editor and create a new file in your project folder named bot.js

  11. The bot.js file will contain the code that dictates your bot’s behavior, which is completely up to you. For the purposes of this tutorial, however, we recommend using the following code and testing your bot end-to-end to make sure that everything is working as expected. You can then go back and edit the contents of bot.js as you see fit.

    const Discord = require('discord.js');
    const client = new Discord.Client();
    const auth = require('./auth.json');
    client.on('ready', () => {
     console.log(`Logged in as ${client.user.tag}!`);
    });
    client.on('message', msg => {
     if (msg.content === ‘hello’) {
      msg.reply(‘hi!’);
     }
    });
    client.login(auth.token);

    This sample code will write a message to the command line console when the bot is called, confirming a successful login and containing your user tag.

  12. Save your updated bot.js file.

  13. Return to Command Prompt or Terminal and type the following to run your bot script: node bot.js

  14. If you’ve done everything correctly up to this point, the following text should appear in your Command Prompt or Terminal window: Logged in as discord-test-bot#<client tag>

Integrate the Bot Code With Your Server

You're almost there...

  1. Open a browser and navigate to the Discord Developer Portal for your server, logging in if necessary.

  2. Select the application we created earlier from the MY APPLICATIONS screen, if prompted.

    screenshot of MY APPLICATIONS screen in Discord Developer Portal
  3. Click OAuth2, located in the left menu pane.

    screenshot of General Information screen in Discord Developer Portal
  4. Scroll to the bottom of the screen until you locate the SCOPES section. Place a check mark next to the bot option by clicking on it once. 

    screenshot of SCOPES in Discord Devleoper Portal
  5. Scroll down again to the BOT PERMISSIONS section, placing check marks next to each permission type that your individual bot might need to function as expected. For the purposes of this example bot, we need the following permissions: Send Messages, Read Message History

    screenshot of bot permissions in Discord

    Your particular bot will likely need a significantly different set of permissions. It’s important to understand what each permission entails before enabling it, so that bot users cannot exploit it for nefarious purposes.

  6. Click Copy, located in between the SCOPES and BOT PERMISSIONS sections and accompanied by a long URL.

    screenshot of OAUTH2 URL Generator in Discord Developer Portal
  7. Open a new browser tab and paste this URL in the address bar, hitting Enter or Return to load the page.

  8. The CONNECT TO DISCORD interface should now be displayed, as shown in the accompanying screenshot. Click Select a server and choose the name of your server from the list provided.

    screenshot of Connect to Discord interface
  9. Click Authorize.

    screenshot of Connect to Discord interface
  10. Place a check mark next to I’m not a robot by clicking on its check box once.

    screenshot of Discord CAPTCHA interface
  11. A confirmation message should now be displayed, noting that your bot is authorized and has been added to your server. 

    screenshot of Discord bot authorized screen

How to Test Your Bot on the Server

You can test your bot by launching the Discord client and sending it commands or messages that correspond to your particular code. In this example, send the word hello to your bot and it should respond with hi!