Browse Month

June 2021

Deploying a discord.js bot using Heroku and GitHub for free

This post will teach you how to create your own Discord bot using the Node.js discord.js library and how to deploy it using GitHub + Heroku for free if you are a new user. GitHub is great because it lets us store our code in the cloud so that we can access it anywhere and on any device. Hooking it up with Heroku’s automatic deploy is amazing because we don’t need to manually run any commands to build or start the bot. Once new changes are pushed to the GitHub repo, the bot will automatically be updated within minutes. It’s like magic.

1. Discord Application

Visit, click on New Application, and enter an app name.

After making the app, we’ll need to create the bot that lives within the app. Click on Bot in the menu, and then Add Bot. Update the bot name if necessary and then click on the Copy button below to save the bot’s token in a safe place for now.

Now we can add the newly created bot into our Discord server. Click on Oauth2 in the menu, select the bot OAuth2 scope, and copy the URL.

Paste this URL into your browser’s address bar, and then select your server where you want your bot to join, and click Authorize.

Now the bot has joined your server, but it will be offline until we finish coding and deploying it.

Note: Your bot can be added to other servers if people have your OAuth2 link. You can disable this by flipping the “Public Bot” toggle.

2. Discord.js

Create a new folder somewhere to store your code for your new bot. Inside the folder, initialize it as a new npm project and install discord.js with:

npm init
npm install discord.js

Open up the package.json file and you should see an existing "scripts" object — add a new "start" key inside that object with the value "node index.js". It should look something like this:

"scripts": {
  "start": "node index.js",
  "test": "echo \"Error: no test specified\" && exit 1"

This will allow us (and Heroku) to start the bot by running the command: npm start

Create a new index.js file and paste in some sample bot code:

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

// log when the bot is ready
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);

// listen to messages and reply
client.on('message', msg => {
  if (msg.content === 'hello') {
    msg.reply('hey there!');


On the very last line of code, replace the ‘token’ word with the bot’s real token which you should have saved in a safe place earlier.

Now in your terminal, you can run npm start. The bot should log in, and you should see it online in your Discord server.

Type “hello”, and your bot should reply back to you immediately with “hey there!”

Excellent, the bot is working locally! Press Ctrl+C in your terminal to quit the bot application and it should go offline from your server in a minute or two.

3. GitHub

I recommend creating a GitHub repo to store your bot code. We will also set up automatic deploys to Heroku each time new code is pushed into the main branch.

In GitHub, create a new private repository (because the code has your private bot token).

Go back in your terminal and let’s run a few commands to:

  1. Initialize the folder we’ve been working in into a git repo.
  2. Add a .gitignore file that ignores the node_modules folder from git because we don’t need to track/push that into GitHub.
  3. Convert the branch from “master” to “main” to promote more inclusive language.
  4. Link to your GitHub repo (change doobix/seewes-bot to your-username/your-repo).
git init
echo "node_modules" >> .gitignore
git branch -M main
git remote add origin

Now we can commit the files into GitHub:

git add .
git commit -m 'initial commit'
git push origin main

4. Heroku

Heroku has a “Free and Hobby” tier for users to run apps for $0. By default, new accounts get 550 free dyno hours to use per month. If you simply add a credit card to your account, you will receive an additional 450 hours, for a total of 1,000 hours for free. In months with 31 days, you can run your Discord bot for 24 hours straight, and it’ll only use up 744 dyno hours, with 256 hours left to spare! So make sure to add a credit card and your bot will not run into any time usage limits.

Ok now let’s create our first Heroku app. After signing in, click New > Create new app. Enter your app name and then click Create app.

Go to the Deploy section and connect it to the new GitHub repo that we created earlier by clicking on the GitHub icon. Follow the steps to connect your Heroku account to your GitHub account. Once Heroku has access to see your repos, it should let you search and connect to your bot repo.

Below in the Automatic deploys section, click on the Enable Automatic Deploys button. This will enable Heroku to deploy your new bot code every time you push a change to the main branch on GitHub.

Now one last thing is that this new Heroku app defaults to being a web app. This means that it only turns on and uses dyno hours when users visit the web url, and it turns off in 30 minutes if it has no web traffic. Since we are not running a website, we need to convert this Heroku app to a worker app with a Procfile.

Run the following commands in the your terminal to add a new Procfile and push the change to GitHub:

echo "worker: npm start" >> Procfile
git add .
git commit -m 'add procfile'
git push origin main

After that’s done, visit the Resources section in Heroku. Disable the web dyno type, and Enable the worker dyno type.

And that’s all there is to it. Your bot should be up and running, and it should reply to you when you type “hello” in your Discord server!

5. Finish

Now you can continue to extend your bot with more features. Have it reply to other commands. Make it give reactions when it detects certain phrases. Etc. Check out the discord.js docs to see more examples and what API’s are available for you to use. The possibilities are endless!

Thanks for reading and hoped this has helped you.

Here’s my example bot repo if you’re interested:

Special thanks Linda H’s post which helped get me started: Create & host a Discord bot with Heroku in 5 min