Git Product home page Git Product logo

discord.js-heroku's Introduction

discord.js-heroku

An in-depth guide on deploying your Discord.js bot on Heroku

Tip: This guide also applies to any Node.js libraries for the Discord API, such as discord.io, eris, and discordie.

⚠️ DEPRECATION NOTICE

Unfortunately, the free Heroku plan will be completely discontinued starting on November 28th, 2022. As a result, this guide is henceforth deprecated.

I am not familiar with other any reputable alternatives that can host bots for free, and thus cannot recommend switching to any in particular; if you are familiar with one, you are free to fork this repository and make the appropriate changes to this guide.

Below is the original text of the guide preserved for posterity, especially for individuals that plan on continuing to host on paid Heroku plans. Do note that although the general setup process remains correct, some aspects of the guide are outdated (i.e. minimum Node version, sample index.js), so please take that into account when referencing this guide in the future.

Finally, I'd like to thank you, the guide readers, for your continuous support over the past five years. When I first started this guide, I was a high school student that couldn't host on a dedicated server because I lacked disposable income, so I wrote the guide to help discord.js support server users in a similar situation; I never expected it to be starred and forked over a hundred times. Because of this guide, my GitHub username also shows up over a thousand times in various package.json files on GitHub and StackOverflow; it's been really gratifying to see how many hobbyist projects I've been able to kickstart through it.

Best of luck to all in the rest of their programming journeys.

— Cynthia Lin (@synicalsyntax), September 2022


About

About Heroku

Heroku is a cloud platform that offers services to host and deploy your web applications in multiple languages, such as Node.js, Ruby, Python, Java, PHP, and Go.

It's optimal for hosting a bot for several reasons:

  • Free — Heroku offers a free hosting plan, so you don't have to pay at all!

    If you plan on using the free hosting plan, you should note the following limitations:

    • Limit of 550 hours per month across the applications on your account.

    This means that once your apps have been deployed on Heroku for over 550 hours, they will go to sleep, and you can't restart them until the start of the next month when your hours are received.

    Luckily, Heroku bumps the limit to 1000 hours per month if you verify your account by adding a credit card to your account, so this limitation won't matter if you do so.

    To help colliding with this limitation, I also recommend creating a separate Heroku account for hosting your bot so your other applications don't take up its hosting time.

  • Easily deployable — You can configure Heroku in two ways that allow you to easily deploy any changes made to your bot:

    • Heroku CLI — With the power of Git, git push heroku master is all you'll ever need to do with Heroku's easy-to-use command line interface.

    • Integrate your app with GitHub for automatic deployment of your bot whenever your configured GitHub repository is updated.

  • Online web and command line interface — If you're not comfortable with using your command line interface, you can access your app through Heroku's web interface, and vice versa.

About this guide

A lot of people have run into difficulties while trying to set up their bot on Heroku, however. This guide was created to help resolve some of those issues.

Some additional notes that you should keep in mind as you follow this guide:

  • In the directions throughout this guide, $ denotes a Bash prompt and should not be included while entering commands in the command line prompt.

  • The main script of the bot will be referred to as index.js.

  • This guide is primarily uses the Heroku CLI (rather than the web interface) to interact with Heroku.

  • It's possible to run your bot in parallel with a web server! See the web branch for more information.

About this repository

This repository also contains several example files mentioned in this guide for you to use as references, including:

Getting started

Prerequisites

Before you get started, make sure you have:

  • installed Node (version >= v8.0.0) and npm (you better have, also how would you even know that your bot works? )

  • installed and configured Git on your local machine

  • created a GitHub account and repository, if you're planning on automatically deploying your bot from GitHub

  • installed the Heroku CLI

  • changed your directory path to the root directory of your bot (the one where your bot's files and scripts are located in):

    $ cd path/to/directory

Creating a package.json file

In order for Heroku to deploy your bot, you need a file called package.json that tells Heroku what dependencies to install to run your app.

If you haven't created one already, you can run npm init in the root directory of your bot to have an interactive prompt-based setup of your package.json file.

The process should look like this (you push the Enter/Return key to save your answer and move on to the next prompt):

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (discord.js-heroku)
version: (1.0.0)
description: An in-depth guide on deploying your Discord.js bot on Heroku
entry point: (index.js)
test command:
git repository: https://github.com/synicalsyntax/discord.js-heroku
keywords: heroku, discord.js
author: synicalsyntax
license: (ISC) MIT
About to write to /Users/synicalsyntax/discord.js-heroku/package.json:

{
  "name": "discord.js-heroku",
  "version": "1.0.0",
  "description": "An in-depth guide on deploying your Discord.js bot on Heroku",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/synicalsyntax/discord.js-heroku.git"
  },
  "keywords": [
    "heroku",
    "discord.js"
  ],
  "author": "synicalsyntax",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/synicalsyntax/discord.js-heroku/issues"
  },
  "homepage": "https://github.com/synicalsyntax/discord.js-heroku#readme"
}


Is this ok? (yes)

Installing your dependencies

Running npm init won't tell specify your bot's dependencies in package.json, but you can do so by running

$ npm install <pkg> --save

This command will install the dependency with the name <pkg> in the node_modules folder while automatically adding the dependency to package.json. For example, $ npm install discord.js --save will install and add the discord.js dependency to package.json.

You'll need to do this for all the dependencies that your bot uses, since missing dependencies in your package.json file will cause problems when you try to deploy to Heroku; packages installed on your system won't be installed on Heroku unless you specify them in package.json.

Specifying the versions of Node and npm

You need to define the version of Node.js and npm that will be used to run your application on Heroku in your package.json file. You should always specify a Node version that matches the runtime you’re developing and testing with.

To find your version of Node, run:

$ node -v

To find you version of npm, run:

$ npm -v

You can now add these versions to your package.json file like in the example below:

"engines": {
  "node": "8.x",
  "npm": "*"
},

Your package.json file should now look something like:

{
  "name": "discord.js-heroku",
  "version": "1.0.0",
  "description": "An in-depth guide on deploying your Discord.js bot on Heroku",
  "main": "index.js",
  "engines": {
      "node": "8.x",
      "npm": "*"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/synicalsyntax/discord.js-heroku.git"
  },
  "keywords": [
    "heroku",
    "discord.js"
  ],
  "author": "synicalsyntax",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/synicalsyntax/discord.js-heroku/issues"
  },
  "homepage": "https://github.com/synicalsyntax/discord.js-heroku#readme"
}

Specifying a start script

What's the command that you enter in your command line interface to start your bot? If your bot's main scripts are located in index.js, chances are that command is $ node index.js. That command will also serve as the start script, which is what Heroku will run when it tries to start your bot.

To specify the start command, you need to add it to your package.json file under the scripts field, like the example below:

{
  "name": "discord.js-heroku",
  "version": "1.0.0",
  "description": "An in-depth guide on deploying your Discord.js bot on Heroku",
  "main": "index.js",
  "engines": {
      "node": "8.x",
      "npm": "*"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/synicalsyntax/discord.js-heroku.git"
  },
  "keywords": [
    "heroku",
    "discord.js"
  ],
  "author": "synicalsyntax",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/synicalsyntax/discord.js-heroku/issues"
  },
  "homepage": "https://github.com/synicalsyntax/discord.js-heroku#readme"
}

Creating a Procfile

By default, Heroku looks for a Procfile to determine what processes are run by the dynos (or containers) of your app.

You need to create a file named Procfile at the root directory of your application with the following contents:

worker: npm start

Creating a .gitignore file

You should exclude some files from being checked in to Git/version control by specifying them in a .gitignore file. One example of files that should be excluded are those in the node_modules folder; not doing so results in a build process that takes forever because the build cache can't be utilized.

Download this Node .gitignore template from your buddies at GitHub and include it in the root directory of your bot.

Committing your files

In order to deploy your application to Heroku, you need to stage and commit your files with Git.

First, initialize an empty Git repository with the git init command.

$ git init
Initialized empty Git repository in /path/to/discord.js-heroku/.git/

Next, run the git status command to make sure that you won't be accidentally checking in any extraneous files to Git (such as your node_modules folder).

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.gitignore
	LICENSE
	Procfile
	README.md
	index.js
	package-lock.json
	package.json

If you see node_modules or any extraneous files under the Untracked files section, make sure to add them to your .gitignore file like we discussed in the previous step before moving on.

Once you're ready to commit your changes, run the following command to stage and commit all of your files:

$ git add . && git commit -m 'Initial commit'
[master (root-commit) 4a7552a] Initial commit
 7 files changed, 660 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 LICENSE
 create mode 100644 Procfile
 create mode 100644 README.md
 create mode 100644 index.js
 create mode 100644 package-lock.json
 create mode 100644 package.json

To double check if you successfully made your commit or not, you can use the git log command to review your Git commit history.

$ git log
commit 4a7552a7292c4faf23bdfbc6135ef3e13ed31487 (HEAD -> master)
Author: synicalsyntax
Date:   Sat Apr 14 11:11:49 2018 -0700

    Initial commit

Creating a Heroku application

If you haven't done so already, sign up for a Heroku account and verify it.

To create a new Heroku application, login with your Heroku account credentials when you run:

$ heroku login

Now create an app with name your-app-name by running:

$ heroku create your-app-name

If your-app-name is available, Heroku will create an app under that name.

Finally, add a Git remote named heroku pointing to Heroku:

$ git remote add heroku https://git.heroku.com/your-app-name.git
$ git remote -v
heroku	https://git.heroku.com/your-app-name.git (fetch)
heroku	https://git.heroku.com/your-app-name.git (push)

Integrating Heroku with GitHub

Note: This step is required if you plan on automatically deploying your bot every time you push changes to a GitHub repository.

Adding a Git remote

To push new commits and changes to a GitHub repository, you'll need to first add a Git remote by running:

$ git remote add origin https://github.com/your-username/your-repo-name.git

If your remote was added successfully, running $ git remote -v should give you the following output:

git remote -v
origin  https://github.com/your-username/your-repo-name.git (fetch)
origin  https://github.com/your-username/your-repo-name.git (push)

You can now push commits to your GitHub repository by running:

$ git push origin master

Automatically deploying with GitHub

Follow Heroku's guide on integration with GitHub and enable automatic deploys to deploy your bot whenever you push to your GitHub repository.

Testing your setup

Note: This step is not required (especially if you haven't downloaded the Heroku CLI), but it's highly recommended.

You should build your application locally to test if you've set up it correctly.

You can do so by running npm install to install all your dependencies and then starting your app locally by running:

$ heroku local

The Heroku CLI will now run your app at http://localhost:5000/; if no errors are encountered, you're on the right track!

Deploying your app

If you're reading this part of the guide, you should have:

  • developed a functioning Discord bot

  • setup your repository for Heroku deployment

If all goes well, you can now deploy your app to Heroku by running:

$ git push heroku master

If the app is deployed successfully, congratulations! You've finished setting up, deploying to, and hosting your bot on Heroku!

If you have some questions/feedback about this guide, you can message me on Discord at synicalsyntax#9944. Hope you found this guide helpful! :)

Additional resources

License

MIT License

Copyright (c) 2017 Cynthia Lin

discord.js-heroku's People

Contributors

synicalsyntax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

discord.js-heroku's Issues

Change the default branch

Hey!
This guide has really helped me, so thanks for doing it!
Even that, I faced a problem: GitHub recently changed the default branch from master to main, but your guide still says master. I think it could be a good idea to change "master" to "main" or, at least, advice both possibilities.
Thanks!

Remove Express Server & Sleep Workaround

You have a bit of code that refreshes the web worker to keep the app awake, but if you had read the heroku docs, you would know that if you disable the web worker, put a custom name in the procfile and a custom start command, it will naturally not sleep.

Heroku only sleeps apps because its default action is for web hosting, which doesn't need to be awake 24/7.
But if you disable that and use a custom worker it will stay up 24/7 until you run out of hours.

In fact by doing this you are using more resources hosting the web dyno and connecting to it.

PR Incoming

Error R10

Hello, after launching my heroku app it's giving me this log:
2018-04-06T11:46:07.785538+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-04-06T11:46:07.785690+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-04-06T11:46:07.918173+00:00 heroku[web.1]: State changed from starting to crashed
2018-04-06T11:46:07.899719+00:00 heroku[web.1]: Process exited with status 137

Is there a way to fix this?

Disable sleep, don't work around it.

Instead of

setInterval(() => {
 http.get('http://discordjs-heroku.herokuapp.com');
}, 900000);

Just change the text in the Procfile to

BOT: node index.js

And DISABLE the WEB Dyno on heroku, and ENABLE the BOT Dyno on heroku.
Using the npm start script and having the WEB Dyno enabled will tell heroku it is a website and that it can sleep.

This is the official way to disable sleeping and wont use excess resources loading the website every 15 mins.

bot

the bot keeps leaving the vc whenever i request a song because of the code
what do i do

cannot find module discord.js

Hello,

I have followed your tutorial and the deploy works fine but when the app run it says the discord.js module could not be found. Seens like the server is not running npm install before npm start.

Here is my package.json

{
  "name": "reign-bdo-discord-bot",
  "version": "0.0.1",
  "requires": true,
  "lockfileVersion": 1,
  "engines": {
    "node": "8.x",
    "npm": "*"
  },
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "ygordanniel",
  "license": "MIT",
  "dependencies": {
    "async-limiter": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
      "integrity": "sha1-ePrtjD0HSrgfIrTphdeehzj3IPg="
    },
    "discord.js": {
      "version": "11.3.0",
      "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.3.0.tgz",
      "integrity": "sha1-DT4q4ShG5OACvb3+PLJl4r8kvzI=",
      "requires": {
        "long": "3.2.0",
        "prism-media": "0.0.1",
        "snekfetch": "3.6.4",
        "tweetnacl": "1.0.0",
        "ws": "4.0.0"
      }
    },
    "long": {
      "version": "3.2.0",
      "resolved": "https://registry.npmjs.org/long/-/long-3.2.0.tgz",
      "integrity": "sha1-2CG3E4yhy1gcFymQ7xTbIAtcR0s="
    },
    "moment": {
      "version": "2.20.1",
      "resolved": "https://registry.npmjs.org/moment/-/moment-2.20.1.tgz",
      "integrity": "sha512-Yh9y73JRljxW5QxN08Fner68eFLxM5ynNOAw2LbIB1YAGeQzZT8QFSUvkAz609Zf+IHhhaUxqZK8dG3W/+HEvg=="
    },
    "prism-media": {
      "version": "0.0.1",
      "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-0.0.1.tgz",
      "integrity": "sha1-o0JcnKvVDRxsAuVDlBoRiVZnvRA="
    },
    "safe-buffer": {
      "version": "5.1.1",
      "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
      "integrity": "sha1-iTMSr2myEj3vcfV4iQAWce6yyFM="
    },
    "snekfetch": {
      "version": "3.6.4",
      "resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-3.6.4.tgz",
      "integrity": "sha1-0T6AphbYkvPTjarkKJ9NJYpkUSA="
    },
    "tweetnacl": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.0.tgz",
      "integrity": "sha1-cT2LgY2kIGh0C/aDhtBHnmb8ins="
    },
    "ultron": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz",
      "integrity": "sha1-n+FTahCmZKZSZqHjzPhf02MCvJw="
    },
    "ws": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/ws/-/ws-4.0.0.tgz",
      "integrity": "sha1-v+HaTAjuuXgLmG4OTRDszXNFmZ8=",
      "requires": {
        "async-limiter": "1.0.0",
        "safe-buffer": "5.1.1",
        "ultron": "1.1.1"
      }
    }
  }
}

Let me know if you need more information.

Bot shutsdown after 60 seconds

2018-09-23T08:14:48.963688+00:00 heroku[web.1]: State changed from crashed to starting
2018-09-23T08:14:52.000000+00:00 app[api]: Build succeeded
2018-09-23T08:14:54.586216+00:00 heroku[web.1]: Starting process with command `npm start`
2018-09-23T08:14:57.743158+00:00 app[web.1]: 
2018-09-23T08:14:57.743175+00:00 app[web.1]: > [email protected] start /app
2018-09-23T08:14:57.743176+00:00 app[web.1]: > node index.js
2018-09-23T08:14:57.743178+00:00 app[web.1]: 
2018-09-23T08:14:59.914132+00:00 app[web.1]: AUIB system online
2018-09-23T08:14:59.915952+00:00 app[web.1]: Found guild: Another Chance
2018-09-23T08:14:59.918248+00:00 app[web.1]: Found guild: /r/SpaceboyRoss
2018-09-23T08:15:55.143852+00:00 heroku[web.1]: State changed from starting to crashed
2018-09-23T08:15:55.005857+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-09-23T08:15:55.005857+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-09-23T08:15:55.125602+00:00 heroku[web.1]: Process exited with status 137

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.