Hosting a Discord.js bot for free using Heroku

Mason Spring
Aug 20, 2018 · 3 min read

Introduction

If you’ve used Discord for any substantial amount of time, you’ve probably interacted with a Discord bot. A Discord bot is an artificial user, one which can interact with normal Discord users through code. In this article I will be teaching you how to create an extremely basic Discord bot using Discord.js and hosting it, for free, on Heroku.

Requirements

Although this is all you need, I would also recommend that you grab the following:


Part 1: Creating Your Bot on Discord

Step 1: Open the following link

Step 2: Create a new application

Step 3: Give your application a new name and image

Step 4: Under the Bot tab, click Add Bot

Step 5: Copy your Client ID and Secret to another location.

Step 6: Invite your bot to a server using the following link: https://discordapp.com/oauth2/authorize?&client_id=YOUR_CLIENT_ID_HERE&scope=bot&permissions=0
Replace YOUR_CLIENT_ID_HERE with your Client ID


Part 2: Creating the GitHub Repository

Step 1: Create a new repository on GitHub.

Step 2: Add a new file, bot.js containing the following:

const Discord = require('discord.js');

const client = new Discord.Client();



client.on('ready', () => {

console.log('I am ready!');

});



client.on('message', message => {

if (message.content === 'ping') {

message.reply('pong');

}

});



// THIS MUST BE THIS WAY

client.login(process.env.BOT_TOKEN);//BOT_TOKEN is the Client Secret

Step 3: Add a new file, named package.json containing the following:

{

"name": "Example",

"description": "Example",

"version": "0.0.0",

"main": "bot.js",

"scripts": {

"start": "node bot.js"

},

"dependencies": {

"discord.js": "11.1.0",

"request": "2.81.0"

}

}

Step 4: Create a new file, named procfile containing the following:

worker: node bot.js

Commit these files to the repository


Part 3: Hosting on Heroku

Step 1: Go to the Heroku Dashboard

Step 2: Go to the deploy section, select GitHub, and enable Automatic Deploy

Step 3: Under Resources, deselect web and activate the worker

Step 4: Go to Settings, find the Config Variables section, and add a new option. The Variable’s key should be BOT_TOKEN and the value should be the Client Secret you copied earlier.

If everything goes well, you will now have a working Discord bot. In order to get it to run 24/7, just add a valid billing address under Heroku’s settings. You will not be charged, and will be given 1,000 free hours a month (A few hundred more than you need to host one bot…)

Mason Spring

Written by

A 15 year old with interests in Game Design and Software Development. New posts every day at approximately 6:00 PM EST.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade