Build a Reddit Moderator Bot With Standard Library + Code.xyz in 10 Minutes
Tags: javascript, node, webdev, showdev
Published: 2018-10-16 07:04:33

---
title: Build a Reddit Moderator Bot With Standard Library + Code.xyz in 10 Minutes
published: true
description: Build a Reddit Bot in 10 minutes
tags: javascript, node, webdev, showdev
---

[If you're a subreddit moderator, chances are you've received requests](https://medium.com/@eileenzhong/build-a-reddit-moderator-bot-with-standard-library-code-xyz-in-10-minutes-d85d878d4a0d) from community members to delete an inappropriate comment or ban an ill-mannered user. But it's 2018 going on 2019 - we're in the midst of the biggest tech era and spoiled with things like AI, automation and bots. We no longer need to perform menial tasks like monitoring subreddits. Instead, I'll teach you in 3 easy steps how to build a Reddit bot that will automatically check a subreddit for flagged comments. 

![Community members can use "!delete" or "!ban" to request bot moderation](https://res.cloudinary.com/eileenstdlib/image/upload/v1539630949/Screen_Shot_2018-10-12_at_3.52.34_PM.png)

Whether you have no programming knowledge or 15 years of professional experience, you'll be able to deploy this bot with [Standard Library](https://www.stdlib.com) in under 10 minutes.

If you're not familiar with [Standard Library](https://www.stdlib.com), we're a serverless platform that enables **everyone** (yes, *everyone*! Calling all PMs, designers, marketing teams, sales people, oh and engineers… 🙋) to build, ship, and integrate scalable APIs in a matter of seconds. With the help of our online editor, [Code.xyz](https://code.xyz), you can do all of this right from your browser. 

![Standard Library](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631205/standard-library-logo-dark.png)

# **Requirements**
- [Reddit](https://www.reddit.com) moderator account (for the subreddit you'd like to monitor)
- [Standard Library](https://www.stdlib.com) account (free)

# **Step 1: Get Reddit bot template**
*Estimated time: 30 seconds*
Head over to [Code.xyz](https://code.xyz) in your browser and select `reddit-moderator-bot by @eileenzhong` (that's me! 🤓👋) in the Community API Sources tab:

![Select "reddit-moderator-bot" by @eileenzhong](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631295/Screen_Shot_2018-10-11_at_3.01.12_PM.png)

Name your API:

![Name your API](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631296/Screen_Shot_2018-10-11_at_3.03.43_PM.png)

Once done, you'll conveniently land in [Code.xyz](https://code.xyz) (Standard Library's in-browser text editor) with the Reddit bot project code. 
# **Step 2: Copy and paste**
*Estimated time: 2-5 minutes*
This next step involves expert knowledge of the copy-and-paste skill to set some required environment variables. You'll notice there are three sets of variables listed in your `env.json` file: `local`, `dev`, and `release`. We'll be deploying our code to the dev environment, so we only need to worry about the `dev` variables for this tutorial! 

All fields are found in `env.json`:

![Open env.json](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631377/Screen_Shot_2018-10-11_at_3.13.11_PM.png)

To retrieve your `STDLIB_LIBRARY_TOKEN`, right click between the quotation marks and select "Insert Library Token…":

![Insert library token by accessing the context menu](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631453/Screen_Shot_2018-10-12_at_3.09.06_PM.png)

Keys prefaced with `REDDIT_`-* can be found on [Reddit](https://www.reddit.com/prefs/apps) (`REDDIT_USERNAME` and `REDDIT_PASSWORD` are your moderator account's username and password, respectively).
If you don't already have a Reddit app, register for one [here](https://www.reddit.com/prefs/apps) with your moderator account:

![Register a Reddit app](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631551/1_5TGHBJGNptiFxHs3oyDaqw.png)

Copy the `REDDIT_CLIENT_ID` and `REDDIT_SECRET_KEY`:
![Click edit to view private Reddit credentials](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631581/Screen_Shot_2018-10-11_at_3.25.50_PM.png)
![Retrieve your REDDIT_CLIENT_ID and REDDIT_SECRET_KEY](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631581/Screen_Shot_2018-10-11_at_3.26.04_PM.png)

Paste both into `env.json`:
![Filled out env.json](https://res.cloudinary.com/eileenstdlib/image/upload/v1539660124/Screen_Shot_2018-10-15_at_8.17.21_PM.png)

When your `env.json` file is properly filled out like the above screenshot, the bot is ready for some preliminary testing. 
Fill out the required parameters (definitions and screenshots below):
```
**Parameter definitions**

subreddit: the name of the subreddit you are moderating
threshold: what score do you want the comment to have in order to perform the requested action? 
allowBanPermission: true (allow ban requests) / false (ignore ban requests)
```

![Click Debug to launch the parameter editor](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631711/Screen_Shot_2018-10-12_at_9.27.26_AM.png)

Then click the "Run" button, which will do 2 things: *execute* and *deploy* the program.
![Custom parameters](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631711/Screen_Shot_2018-10-12_at_9.44.16_AM.png)

Congratulations, you've deployed an API to Standard Library! 😎 🎉 🎊
# **Step 3: Set it and forget it**
*Estimated time: 1 minute*

![Customize the task schedule](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631711/Screen_Shot_2018-10-12_at_9.44.16_AM.png)
![Schedule the task](https://res.cloudinary.com/eileenstdlib/image/upload/v1539631781/Screen_Shot_2018-10-12_at_9.57.31_AM.png)

The final step is to put your bot to work. You can do this in [Code.xyz](https://code.xyz) by scheduling a task, which will automatically execute the API you just created according to the schedule you choose.

*Suggestion: depending on subreddit traffic, you might increase or decrease the frequency of bot invocation. A good start is to have the bot do a scan once every 5 minutes.*

Once you click the "Schedule Task" button in the bottom right corner, your Reddit bot will scan `r/{subreddit}` to **reply to any new !ban/!delete requests** and **perform these tasks on any comments that meet the score threshold** according to the schedule you have chosen.

That's it! 🌟 Now imagine… if it was this quick and painless to deploy a Reddit moderator bot, what else can [Standard Library](https://www.stdlib.com) do for you? Feel free to reach out with any ideas or questions: **eileen@stdlib.com**.


---

*Eileen is a software engineer at Standard Library.*