Reddit bot hosting, PRAW or snoowrap on a loop
A Reddit bot watches a stream and acts on what it sees, which means it has to be running when the post appears. Push the repository, set the credentials, deploy: PRAW on Python or snoowrap on Node.js, around the clock in its own sandbox.
PRAW, on Python
import os
import praw
reddit = praw.Reddit(
client_id=os.environ["REDDIT_CLIENT_ID"],
client_secret=os.environ["REDDIT_CLIENT_SECRET"],
username=os.environ["REDDIT_USERNAME"],
password=os.environ["REDDIT_PASSWORD"],
user_agent="my-bot/1.0 by u/myaccount",
)
for submission in reddit.subreddit("test").stream.submissions(skip_existing=True):
if "hello" in submission.title.lower():
submission.reply("hello from FadeHost")praw in requirements.txt, the file saved as bot.py, the five values in the environment. The credentials come from a script app in your Reddit account's app preferences, created by the account the bot posts as.
snoowrap, on Node.js
const Snoowrap = require("snoowrap");
const r = new Snoowrap({
userAgent: "my-bot/1.0 by u/myaccount",
clientId: process.env.REDDIT_CLIENT_ID,
clientSecret: process.env.REDDIT_CLIENT_SECRET,
username: process.env.REDDIT_USERNAME,
password: process.env.REDDIT_PASSWORD,
});
setInterval(async () => {
const posts = await r.getSubreddit("test").getNew({ limit: 10 });
// decide and reply
}, 60000);snoowrap is the Node.js equivalent and queues requests for you. Both libraries talk to Reddit over HTTPS, which the free plan's outbound list allows.
The user agent and the rate limit
Reddit wants a user agent that says what the bot is and who runs it, and it returns the rate limit in every response. PRAW waits for you when you let it; snoowrap queues. The one thing neither does for you is remember what it already answered, so keep the ids you have handled in a file under /data or in a managed database. A redeploy replaces the code, not the disk, so the bot does not start the conversation again.
What gets a bot banned, and by whom
Reddit suspends the account, not the host, so the rules worth following are Reddit's API terms and each subreddit's own. Identify the bot, stay inside the rate limit, do not answer every thread and honour removal requests from moderators. Our terms also cover apps that break the rules of the platform they connect to.
Frequently asked questions
Which credentials does a Reddit bot need?
A script app gives you a client id and a client secret; with the bot account's own username and password that is enough for PRAW and snoowrap to log in. All four go in environment variables, never in the repository.
Does a Reddit bot fit the free plan?
Yes. A stream watcher idles in a few tens of megabytes and moves almost no traffic. The 1 GB daily traffic limit on the free plan is not something a comment bot comes near.
How do I stop it replying twice?
Write the ids you have acted on to a file under /data, or to a managed database, and check it before replying. /data survives redeploys and restarts.
Can a free bot reach the API it needs?
A free app's outbound traffic follows a fixed list: DNS, HTTP and HTTPS on the usual ports, the database ports, and some game and voice ports. Anything that calls an API over HTTPS is covered. A paid app may reach everything, or whatever list you give it, and the Firewall tab names what was refused.
What happens when I push a commit?
With auto-deploy on, every push to the branch you chose redeploys the bot: install, build if there is one, start. The live console shows each step and the crash doctor explains a failed start in plain words.
What does the free plan include?
One bot or app per verified account: 256 MB of memory, a quarter of a vCPU, 3 GB of disk under /data and 1 GB of traffic a day, with deploys on push, the live console, the crash doctor and rollback. The process stays on; only a web address sleeps when nobody is using it. A free app runs in whichever region has room for it. A paid plan from $2 a month gives the bot 1 GB and a whole vCPU, lets you pick the region and drops the traffic limit.