Python script for a reddit bot that does the following:
- Scans one or more reddit threads for user contributions (top-level comments and replies to top-level comments)
- Sorts the results to determine:
- The most helpful users (measured by the number of replies they've made to other top-level comments)
- The most helpful users who have not received replies to their own top-level comments
- Replies to the thread with the results
- Configure the bot using environment variables for your use case (see full ist of options below).
# On local machine
export comment_mode='edit'
export posts='[ { "subreddit":"abc", "post_regex":"Hello world" }, { "subreddit":"Zebra", "post_regex":"Fancy thread title" } ]'
# On Heroku
heroku config:set client_id=foobar
heroku config:set client_secret=zebra
- Run the bot (optionally supplying command-line arguments, which override your environment variables):
$ python3 main.py --debug True --reply_threshold 5
client_id
: The ID of your bot (see bot_creds_example.png).client_secret
: The secret of your bot (see bot_creds_example.png).user_agent
: User agent passed to PRAW (likely the name of your bot or a description of the specific application).username
: The username of your bot's reddit account.password
: The password for your bot's account.
posts
: A JSON string that describes one or more threads that the bot will operate on. Can be a single object describing 1 thread, or a list of objects describing multiple threads. Meaningful attributes are:subreddit
: The subreddit the bot will search for the the thread matchingpost_regex
.post_regex
: A python regex to match the reddit post your bot will scan.post_id
: Choose a specific post to scan (instead of using thesubreddit
andpost_regex
).- Examples:
export posts='[{ "subreddit": "pics", "post_regex": "Discussion thread" }, { "subreddit": "politics", "post_regex": "Daily chat" }]' export posts='[{ "post_id": "abc123" }]' export posts='{ "post_id": "foobar" }'
reply_threshold
: The minimum number of replies to a top-level comment before the bot will stop including it in the results.
comment_mode
: Eithernew
, which will delete the bot's previous comment and post a new one, oredit
, which will edit the bot's previous comment.interval
: The interval (in seconds) the bot will wait between scans. Note: this only affects the resulting message that the bot produces. Actual scheduling is handled by the Heroku Scheduler.mode
: Eitherprint
, which will print results to the command line, orpost
, which will post results as a Reddit comment.
debug
: Prints various debug statements that show the bot's progress.answer_username
: Only include answers from 1 user.question_username
: Only include questions from 1 user.print_answers
: Prints scanned answers on the command-line.print_questions
: Prints scanned questions on the command-line.
The script uses PRAW to interact with reddit's API.
The script relies on Heroku Scheduler to run the script every 10 minutes.
More replies to top-level comments => more helpful.
Any question with fewer than reply_threshold
replies could use some love.