👨🏻‍💻 V2 Tutorial: Creating your first Discord Bot — Part 1

Renemari Padillo
Davao JS
Published in
7 min readApr 18, 2019

Introduction

A lot of time has passed by since I have created my first bot tutorial. Since then a lot of people where taking interest on how to create bots for their Discord servers, but unfortunately a lot of updates happened since then and I was not able to keep up on updating my article due to my busy schedule and many people were quite confused on setting it up.

Fret not, I spared some of my free time to create this new and improve bot tutorial. Let’s do this 😎

Prerequisites

  • Basic knowledge with Javascript (Node JS is a bonus)
  • Basic knowledge on using your terminal (mac/linux) or command line (windows)
  • You must have Node JS installed (v8.0.0 and up). link
  • You must have code editor installed (eg: Visual Studio Code, Sublime Text, Atom)
  • And a Discord account and desktop client (obviously…)

Please verify that you have installed your Node JS correctly by opening up your terminal (Mac/Linux) or command line (Windows) and type in node -v and it should show the version of Node installed in your computer.

any version above 8.0.0 will do

⚠️ If this command does not work then you did not installed Node JS properly in your computer.

Please try to search your errors in Stack Overflow or Google.

Let’s Get Started

Creating your test server

Before we start working with our bot, we need to have test server first in Discord.

Go ahead open up your Discord client and create a server, in this tutorial I’ll be using `discord bot playground` as my server name.

please use a different name for your server to avoid conflicts

That’s it for now, we’ll be adding our bot here soon!

Creating Discord bot

Shortcut version (For advance users who already know their way around the code)

You can download the source code here and skip on how to register your bot and generate bot token.

Long version

First things first, let’s create our project folder.

NOTE: you can choose your own path and project name but please to make sure to replace the path & name that I’ll be mentioning on my code, for this tutorial I’ll be using `discord-greeter-bot` as my project name.

Open up your terminal/command line and type in mkdir discord-greeter-bot this will create our project folder.

Navigate inside your folder by typing in cd discord-greeter-bot

Now, initialize the project by typing in npm init -y this will create a package.json file inside the project folder.

ℹ️ A package.json file is a manifest file used on Node based projects to make it easy for others to manage and install dependencies and setup scripts that helps generating builds, running test, and other stuff regards to your project. In this tutorial we won’t be deep diving in to the anatomy of the package.json, let’s focus on getting up our bot shall we?

Now that we have our project initialized, we can now add the project dependency.

Let’s start first by installing a well known Javascript library for interacting with Discord API called discord.js type in:

npm install --save discord.js dotenv
Ignore the `npm WARN` this won’t affect the project anyways, unless you get a `npm ERR` then Houston we got a problem!

Next is to create a file called .env this is where we’ll put our Discord token later.

Open and edit the file and use this template:

Generating Token Key

When creating a Discord bot it should be registered first so that we can get the token key for our project go to https://discordapp.com/developers/applications/ and login.

You should be able to see your dashboard after logging in

Let’s create a new application, for this tutorial I’ll be using discord-greeter-bot

Open up your app and you should see the app dashboard

On the left navigation go to Bot and click on Add Bot

And you’ll be prompted with a popup

and.. yes.. just do it!

You should now see your bot details

To get your token click on Click to Reveal Token it should now be shown to you, go back to your .env file and add the value:

DISCORD_TOKEN=generated-token

Next is to create our bot script, go back to your project folder and create a file named bot.js this will contain our bot script here’s a code template:

Save your file and test run your script by running the command node bot.js

If you have followed my instructions then your script should be running without a problem 😉

Adding bot to our test server

Now that we have our bot up and running we will now add our bot to our test server. Go back again to the Developer Portal and inside our app page go to OAuth2

Then scroll down and set the scopes and bot permission

ℹ️ As you can see there are a lot of bot permissions that can be set, it depends on how you intended to use your bot. It’s ideal to only enable the permissions that you only needed in order to prevent misuse of your bot that might be exploited some day.

For this tutorial we will just set the Send Messages and Read Message History permission only.

Under scopes it should provide the authorization URL of your app, copy and paste this into a new browser tab.

It should show you this page, and of course add the bot to our test server (discord bot playground)

Success! You have now added your bot to the server 👏

Now let’s try to do basic interaction with the bot, go back to your project folder and add this code inside bot.js

This is how your final code looks like

Go back to your terminal cancel the running node bot.js command by doing CTRL+C and re-run the script again node bot.js

Go back to your Discord client, and now when you type in “ping” the bot will reply to you “pong”

CONGRATULATIONS! YOU HAVE CREATED YOUR FIRST DISCORD BOT 🎉

This is just the tip of the ice berg 🗻as this tutorial only covers the development phase of the bot, now we have our bot script running successfully we can do a lot of things out of it!

For advance users: You can learn more about the Discord API here.

I hope you have learned something on this tutorial, let me know if you have question(s) or problem(s) with your setup and create your issues here.

COMING UP NEXT

On the next tutorial I’ll be showing you on how to host your bot so you won’t rely with local hosting! You can check it out here.

Have fun and keep learning, stay tuned on the next series 👊🏼

UPDATE:

02/05/2020 — Updated outdated screenshots and made change to file name instead of auth.json, .env file will be used using dotenv library to be compatible with Heroku environment variables. Added link to the second part of the tutorial.

--

--

Renemari Padillo
Davao JS

Things I do: 👨🏻‍💻 🎮 📸 🚴🏼 🛫(repeat) • https://renesansz.me