Skip to content
Eric Lu edited this page Nov 26, 2021 · 10 revisions

Self-Hosting Installation

The following are the steps to take to set this bot up yourself:

Part 1: Repo Setup

  1. Download the repository and save it wherever you want on your local machine.
    • In the console, download the necessary dependencies with gradle build.
  2. In the primary package (with the other Java files), create a new Config.java file with the following code:
package primary;

public class Config {
    static final String DISCORDBOTTOKEN =    // Bot's Discord token
    static final String REDDITBOTID =        // Reddit ID of the bot's owner
    static final String REDDITBOTSECRET =    // Bot's Reddit token
    static final String REDDITUSERUSERNAME = // Reddit username of the bot's owner
    static final String REDDITUSERPASSWORD = // Reddit password of the bot's owner
    static final String MONGOURI =           // Link that connects Bot to MongoDB database
}

Following the proceeding steps will get you the values you need.

Part 2: Discord API

  1. Go to the Discord Developer Console and click "New application".
  2. On the left sidebar, select "Bot".
  3. Click "Add Bot"
  4. In the "Bot" section, press "Click to Reveal Token" and copy the listed token. That token corresponds to "DISCORDBOTTOKEN" in Config.java.
  5. On the left sidebar, select "OAuth2".
  6. Under "Scopes", check off "bot" and "applications.commands".
  7. Under "Bot permissions", select the permissions you wish to give the bot.
    • At minimum, you will need to give the bot the following permissions:
      • Send Messages
      • Read Message History
      • Use Slash Commands
      • Read Messages/View Channels
      • Embed Links
    • Alternatively, you can give the bot Administrator and be done with it, although depending on the server you might not want to or be allowed to do so.
  8. After Step 7), Discord will auto generate a link to you. Go to that address. From there, you will be able to select which server(s) you'd like to add the bot to.
    • To create a brand new server to add the bot to, press the green plus button on the left sidebar on the normal Discord window ("Add a server"), then click "Create a server", input whatever server name you want and then finally click "Create".

Part 3: Reddit API

  1. Log into the Reddit account on which you wish to host the bot. (While you're at it, fill out "REDDITUSERUSERNAME" and "REDDITUSERPASSWORD" in Config.java.
  2. Go to the Reddit Application Console. Scroll down and click on "create app".
    • For the name field, put whatever you want.
    • Click on "Script for personal use. Will only have access to the developers accounts".
    • For the description field, put whatever you want.
    • Don't put anything for the "about url".
    • For "redirect uri", enter "https://reddit.com".
  3. Click on "edit" on the app you just created.
    • The string underneath "personal use script" corresponds to "REDDITBOTID" in Config.java.
    • "secret" corresponds to "REDDITBOTSECRET" in Config.java.

Part 4: Create MongoDB database

Follow this guide to create a free MongoDB Atlas cluster.

  • The URL with mongodb+srv:... corresponds to MONGOURI in "Config.java".

Part 5: Hosting Bot on AWS

(Credit to this guide; much of this part is adopted from that guide, and adjusted correspondingly for Java.)

  1. Download the corresponding .jar file (Reddit-Discord-Bot-1.0-all.jar) with gradle shadowJar.
    • Make sure your Config.java is complete before doing this.
    • After running the gradle command, the .jar file can be found in the ...\build\libs\ directory.
  2. Set up a free AWS EC2 VPS (for one year), and host your bot on it.
    • Go to the Amazon EC2 Page and sign up for an account.
    • Navigate to the Amazon web services console.
    • Click on "EC2" (top left of page).
    • Select "Launch Instance".
    • Select "Ubuntu Server 14.04 LTS (HVM), SSD Volume Type - ami-5189a661". (You are welcome to use others, but this one worked for me.)
    • Pick "General Purpose" (free tier micro)
    • Select "Review and Launch"; launch
    • You’ll then see a prompt about your "key/pair". The key pair consists of a public key that AWS stores, and a private key file that you store. They come together in a file "something.pem" which you will need in order to login to your account. Select "create new pair" and enter a title for your key/pair.
    • Download the key pair, and save it to somewhere you'll remember to find it on the computer.
    • Now you should see a message that "Your instances are launching" (it may take a few minutes before they are available.)
    • Select "View Instances." Record the "Public IPv4 address".
  3. Navigate terminal to the directory where you have your "pem" file. First you must make sure your key is not “publicly” viewable. This has to do with file permissions.
    • Do $ chmod 400 yourkeyfile.pem to check.
  4. In the terminal, enter $ ssh -i "yourkeyfile.pem" ubuntu@[Instance Public IPv4 address] to login to the key, where [Instance Public IPv4 address] is the "Public IPv4 address" you recorded earlier.
  5. In the key, download Java 15 with the following commands:
$ wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
$ sudo add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/
$ sudo apt-get install adoptopenjdk-15-hotspot
  1. In the key, download tmux with the following commands:
$ sudo apt install tmux
  • This will allow the bot to run even when the terminal is closed.
  1. Upload the Reddit-Discord-Bot-1.0-all.jar file to the server.
    • You can do this with SFTP software like Cyberduck. Log onto Cyberduck with the "Public IPv4 address" as the Server and your .pem file uploaded as the SSH Private Key, as shown below: img After logging in, it should be trivial to drag and drop the Reddit-Discord-Bot-1.0-all.jar file into Cyberduck, or direct upload the Reddit-Discord-Bot-1.0-all.jar file from its absolute path into Cyberduck.
  2. Start running the bot as follows:
$ tmux new -s bot
$ java -jar Reddit-Discord-Bot-1.0-all.jar
  • After doing the above commands, it is safe to close the terminal. The bot will now run until something happens to your AWS EC2 VPS, or you tell it to stop with $ tmux kill-session in the key terminal.