Skip to content
This repository was archived by the owner on Jul 16, 2022. It is now read-only.

Self Hosting on Heroku

sau226 edited this page Jul 8, 2019 · 12 revisions

Welcome to the sort-of-official AwesomeBot Community self-hosting guide for Heroku™! Please read the whole NON Heroku self hosting guide before this one so you already know what you will be doing! As it says in the first few lines of that guide, you will need deep knowledge of stuff to make this work!

Requirements

  • Basic knowledge at least of Node.JS, Heroku, your OS, and the command line
  • Accept that the discord server isn't for Heroku hosting, so most people won't be able to help you. I advise you to go to https://dotjs.co if you have questions about hosting on Heroku if you have any questions about hosting GAB on Heroku (you can ping @King - Vlad#0815 for GAB questions)
  • A Heroku account
  • A credit card connected to your Heroku account if you want the bot to be able to run 24/7 (without a CC you get 550 hours/month, you get 450 hours more with a CC. Heroku dynos (processes) sleep every 30 min of inactivity, and wake up for 30 min on any activity. Hours used is how much time a dyno is active.
  • Node.JS v6+, Heroku CLI, Git CLI

Setting up a discord bot

First of all, we need to setup a discord application and tie a bot account to that application. Go to the Discord applications page and create a New App. Then fill out the App name and description fields and upload a nice looking bot avatar if wanted. After that, click on "Add Redirect" underneath "Redirect URI(s)" and enter "http://0.0.0.0:8080/login/callback". Then click Create App. After the application has been created, click "Create a Bot User". Then take note of your Client ID, Client Secret, and bot Token.

Downloading & Configuring AB

First, create a folder in which you want to host AB, go into that directory, and run git clone -b experimental https://github.com/GilbertGobbels/GAwesomeBot. You should now have GAB's files in the directory you're currently in. For the rest of the guide, we're gonna assume you stay in this directory unless told otherwise.

NB: If you wish to download a different branch, such as the GAB development branch, you may instead run git clone -b development https://github.com/GilbertGobbels/GAwesomeBot.

Open Configuration/config.json and fill in the following values as shown:

  • platform: Leave as "discord"
  • shard_count: The amount of shards the bot should be using when connecting to discord. I'd say around 4 is fine but it heavily depends on the amount of servers your bot is supposed to join. You should calculate shards based off no. of servers / 1000, or one shard per thousand servers.
  • hosting_url: http://[yourappname].herokuapp.com/ (this is usually the URL where GAB brings you when using the web interface.)
  • http_port: This doesn't matter for us. Leave it as the default.
  • https_port: This doesn't matter for us. Leave it as the default. development branch only
  • cert: This doesn't matter for us. Leave it as the default. development branch only
  • privKey: This doesn't matter for us. Leave it as the default. development branch only
  • httpsRedirect: This doesn't matter for us. Leave it as the default. development branch only
  • server_ip: Here use 0.0.0.0. This lets Heroku run it without crashing.
  • db_url: Find this when setting up mLab's MongoDB instance later, then come back and fill it in.
  • maintainers: An array containing the Discord User IDs of your bot maintainers. Maintainers are like global bot admins. Besides, they can control the maintainer console which includes options to (among others) set Wiki Maintainers, edit the bot's profile (name/avatar/playing message), set more maintainers/remove others and manage servers (force the bot to leave servers, send a message to a server, etc.). They can also create wiki articles and blog posts. Bot Maintainers have great power over your bot and control the admin panel of all servers your bot has joined. It is VERY important to keep your maintainer list as small as possible to avoid abuse of power. When finding a self-hosted bot, make sure this is the case on the instance in question. Example: ["108892284119977984"]. Note the quotation marks, they are important!
  • header_image: Set this to header-bg.jpg
  • pm_forward: Set this to true if you want GAwesomeBot to PM all maintainers all PM's that he receives. Set this to false if you don't want this.
  • oauth_link: https://discordapp.com/oauth2/authorize?&client_id=YOUR_CLIENT_ID&scope=bot&permissions=470019135 < Use that, changing YOUR_CLIENT_ID with the client ID for your bot app
  • discord_link: The link to whatever discord server you want the discord buttons on your web interface to point to.
  • donate_charities: The charities you want the donate button to point to. This value should be an array of JSON objects in this template: { donate_url: "http://example.com/link/to/charity/donation", icon_url: "http://example.com/link/to/charity/icon", name: "The charity's name", country: "If set, the charity's country code. E.g. UK or NL" }

Any other values that are not noted here are currently not working or do not require any editing. Save and close config.json, and open Configuration/auth.json. Then fill in the values like this:

  • platform.client_id: Enter the client id you noted earlier on your discord application's config page.
  • platform.client_secret: Enter the client secret you noted earlier on your discord application's config page.
  • platform.login_token: Enter the bot Token you noted earlier on your discord application's config page.
  • (optional) tokens.giphy_api_key: Fill out this form to get your own giphy api key or use the public API key "dc6zaTOxFJmzC"
  • (optional) tokens.google_api_key: Go to the Google Developer Console and click on "Project" in the top-left. Then select "Create new project". Give your project a recognizable name for you, and create the project. Then, hit "login details" and create new login details. Choose to create an API-key. Copy the API key that popped up and enter it in auth.json.
  • (optional) tokens.google_cse_key: Go to here to create a new custom search engine and fill out the form (under the 'sites to search' you can simply put in *.google.com). After creating the CSE, go to its config and click "Search engine-ID". Then copy that ID and paste it in auth.json
  • (optional) From the Google Developer Console, ensure your newly created project is selected in the top left, and then click "Library" in the left hand side menu (below "Dashboard, and above "Credentials"). Under the heading entitled "YouTube APIs", click on "YouTube Data API" and select "Enable". Go back to 'Library' and select "URL Shortener API" under the heading "Other popular APIs", and select to enable the URL Shortener API.
  • (optional) tokens.imgur_api_key Hop unto this link and fill out the form to create a new API client. You can fill in whatever in all values, apart from Authorization type. You should select "Anonymous usage without user authorization" there. After creating select the imgur client ID that appears and fill it into auth.json
  • (optional) tokens.microsoft_client_id: Follow this guide to get your Client ID and Client Secret and place both of them into auth.json in the right fields
  • (optional) tokens.microsoft_client_secret: See above
  • (optional) tokens.twitch_client_id: Take a look at twitch's annoucnement on Client ID's which contains a small guide on how to get an application registered and get its client ID. For redirect URI, the hosting_url will do. Place the client id acquired by creating the application into auth.json
  • (optional) tokens.wolfram_app_id: Sign up for wolfram if you haven't already. Then create an application and copy its app ID once created. Then paste it into auth.json. Close and save auth.json. If you don't want to use a certain feature (such as wolfram) you can leave its value as an empty string. However note that this command will not function while being advertised as working in further bot documentation and bot help.

If there is no Extensions directory in your GAwesomeBot directory, please create one (there is no need to put any files in there, but the empty directory must exist otherwise extensions will not save).

Lastly, open the .gitignore file with a text editor such as Notepad++ (Please don't use the standard Windows Notepad. Seriously. It's bad.) and remove the first two lines (which are Configuration/config.json and Configuration/auth.json).

Setting up MongoDB

MongoDB is a non-SQL database that GAB uses to store all its server and userdata in. The other guide uses a local MongoDB instance, but we will use a cloud version to make it much easier.

First, go to http://mlab.com and create an account. Then, make a free MongoDB, using a Google server when asked. Now go to your DB's page on mLab and go to the users tab. Create a username and password with all perms on the DB. At the top of the page you will see something like mongodb://<dbuser>:<dbpassword>@blah145379.mlab.com:45379/blah. Take that from your page, and change and to the user account you just created. Now put that URL starting with mongodb:// into the config. It should look like "db_url": "mongodb://<dbuser>:<dbpassword>@blah145379.mlab.com:45379", but that's just an example

Setting up Heroku

Heroku is a cloud app allowing for easy installs of this type of stuff, especially related to Node.JS, Python, and more. We will use Heroku to host our app 24/7.

First, go to your GAwesomeBot folder and create a file named exactly Procfile, with NO extension whatsoever. It should be a text file containing only web: npm start. Second, go to the Heroku website and create an account. Then, go to your command line, whether that's Terminal on Mac, CMD on Windows, or something else, and use cd PATH_HERE to navigate to your GAwesomeBot folder.

Once in there, run heroku login and sign in with your Heroku account credentials. After you've logged in, run heroku create APP_NAME_HERE. This will create a Heroku App called APP_NAME_HERE in your Heroku account and put a git remote in that local repo pointing to the Heroku App. (note: this will only work if you're using the directory you cloned this repo in... otherwise figure out how to start a local git repo in there first).

Now, do your first upload to Heroku! Type in git add ., then git commit -m "DESCRIPTION_OF_CHANGES_ to record everything in your directory. When that's done, do the honours by running git push heroku master. This will upload everything to Heroku! If in future you ever change anything and want to re-upload, do those 3 commands again (I recap: git add . > git commit -m "DESCRIPTION_OF_CHANGES_ > git push heroku master).

Bot Logs

You can access your bot logs by going to the top right and pressing "More", then "View Logs".

Restarting your Bot

You can restart your bot by pressing "More" at the top right of your screen, then pressing "Restart All Dynos" and confirming in the dialog.

Fixing an issue where your bot crashes

If under your Bot logs it keeps crashing for "failing to connect to the web process within 60 seconds", go to the left, press "Resources". Then, under "Free Dynos", press the "Pencil" icon next to the "web" dyno/process and change the slider to OFF. Press confirm. After that, press the "pencil" icon again, but this time for the "worker" dyno/process and change that to ON. Press confirm, restart the bot (look above), and it shouldn't crash anymore.
A different fix for the crash can be found in the next paragraph.

If your bot crashes for another reason, mention me in the GAB server (look below).

Fixing the Web Server on Heroku

The bot should now run on Heroku! However, if you want the web dashboard to work, you must follow these steps. First, go to the Web folder and open WebServer.js in a text editor. Find line 189 and select everything until //Setup socket.io for dashboard. Replace what you've selected with this:

	 // Open web interface
    var newHttpsPort = process.env.PORT || config.httpsPort;
    function requireHTTPS(req, res, next) {
        if (!req.secure) {
            if (process.env.NODE && ~process.env.NODE.indexOf("heroku")){
                return res.redirect('https://'+ req.hostname + req.url);
            }else{
                return res.redirect('https://'+ req.hostname + ":" + newHttpsPort + req.url);
            }
        }
        next();
    }
    if (config.cert && config.privKey && config.httpsPort) {
        if (config.httpsRedirect) {
            app.use(requireHTTPS);
        }
        const privKey = fs.readFileSync(config.privKey, "utf8", function(err) {if (err) winston.error(err)});
        const cert = fs.readFileSync(config.cert, "utf8", function(err) {if (err) winston.error(err)});
        const credentials = {
            key: privKey,
            cert: cert
        }
        var httpsServer = https.createServer(credentials, app);
        httpsServer.listen(config.httpsPort, () => {
            winston.info(`Opened https web interface on ${config.server_ip}:${config.httpsPort}`);
        });
    }
    var newPort = process.env.PORT || config.httpPort;
    const server = app.listen(newPort, config.server_ip, () => {
        winston.info(`Opened http web interface on ${config.server_ip}:${newPort}`);
        process.setMaxListeners(0);
    });

// Setup socket.io for dashboard

Now your web dashboard should run on Heroku! Enjoy! Thanks to BrownZFilmZ#9832 for this snippet of code.

Installing packages & Running GAB

You've just uploaded GAB to Heroku and Heroku will have automatically started your bot!

Creating a first wiki article

We all know some links in the GAB Web Interface are broken. And while fixes are on the way, it's best to create a wiki article simply called "Home" to hotfix most problems.

Setting wiki contributors

Talking about the wiki, writing one on your own is hard and time consuming. That's why you can set additional wiki contributors in the maintainer's console. These wiki contributors will be able to edit, delete, and create any wiki articles. While you're at it, why not set some bot maintainers? Apart from wiki managing, bot maintainers can also create blog posts for all your users to read (or ignore). Do note however that Bot Maintainers have a lot of power, and the position should not be taken lightly.

Check out the maintainer's console

There's a lot in there to play around with, have fun! (Warning: side effects of playing around with dangerous maintainer settings may include but are not limited to; permanent loss of hearing, spontaneous combustion of hosting machine, and a spooky curse)

Alright, I think that's it. I hope you've managed to setup your own instance of GAB running on a free Heroku Dyno. If you have any problems, or feel the need to point out I'm an idiot and missed something, do so in the community discord (go to https://dotjs.co and DM dotJS for Heroku stuff and mentioning @King - Vlad#0815 for normal GAB stuff)

Have fun self-hosting, AwesomeUsers!