A lightweight and simple system for automating some Reddit moderating actions
Python
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
LICENSE
README.md
TODO.md
decorators.py
logging.conf
modbot.py
rules.py
testrule.py

README.md

About

Reddit modbot is a relatively lightweight script for automating reddit moderating tasks. It was written as a simpler alternative to AutoModerator by Deimos.

Advantages

  • Does not require a database or other semi-complicated setup.
  • Runs continously to avoid startup penalty
  • Defining rules is fairly simple due to simple rules files format
  • Automatically updates rules when you edit/add/remove them on disk
  • Will never re-evaluate an item, which saves time

Limitations

  • Built with single-subreddit usage in mind. Though this could likely be fixed without too many changes.
  • Rules files are not as flexible as AutoModerator rules
  • All rules are regex, which makes matching numbers slightly convoluted
  • Will never re-evaluate an item, which means it's not really possible to react to items getting downvoted or reported.

Getting started

Requirements (both available in pypi):

Run it like this:

modbot.py -r /path/to/rules/dir mysubreddit

Additionally, you may pass in the username and password for your reddit user.

Writing rules files

A rules file is a file ending in .rule, placed in the rules dir.

For every item the bot inspects, it will try to match it against each rule, in order by filename of each rule. If a rule matches, no further rules will be applied.

A rules file contains any number of line of the form "Key: value". After these lines, you may include a rule body, which should be preceded by a blank line. The purpose of this will be described later.

The value of a rule is a regular expression that will be applied to the given value of the item in question (case-insensitively).

Rule lines in the header of the rule, starting with a # character are ignored. The order of rule lines is ignored.

The header lines describe what conditions the comment or post much fullfil to match the rule - or which actions to take. All of these can be negated by adding a ! in front of the field name. In that case, the rule will only match if that condition doesn't. Possible conditions are:

Field Description
Usernamename of the author
Numreportsnumber of reports (this is not too useful currently)
Domaindomain of the post
Titletitle of post
Upvotesnumber of upvotes
Downvotesnumber of downvotes
Scoreupvotes-downvotes
Type'comment' or 'submission'
Bodycontent of comment or self-post
Bodylengthlength of body
Dayhourtime of submission on the form Tue-19 in UTC
UserageAge of author account in days
UserkarmaCombined karma of the author account

In addition to any number of these, you should include a line named "Actions", with the value being a comma seperated list of actions to take when the rule matches. Possible actions are:

Action Description>
upvoteupvote the item
logwrite an entry to the rulelogger logging logger
spammark the item as spam
removeremove the item (but don't mark as spam)
approveapprove the item
respondpost a distinguished comment, using the body of the rule as a template, using new style python string formatting with the objects "rule", "thing" and "matches"available. See below for an example.
messagemods:targetmessage the mods of the subreddit "target", using the rule body.
beepwrite a BEL character to the terminal
messageauthormessage the author, using the rule body as template.
reportreport the item
nonedo nothing
linkflair:flairclass:flairtextset the linkflair class "flairclass" and text "flairtext" on the item

Rule examples

Simple example

Domain: (quickmeme.com|qkme.me|memegenerator.net|weknowmemes.com)
Actions: log,spam,respond

Please do not submit memes to this subreddit.

***

I am an automated bot - please [contact the mods](http://www.reddit.com/message/compose?to=%2Fr%2Fmysubreddit) if you believe I made a mistake.

This rules matches any post submitted from a number of meme sites and posts a comment in the thread, as well as marks the submission as spam to train the spam filter.

Message formatting

Type: comment
Body: digg
Actions: log,messagemods
Description: Burn the witch!
Subject: Someone mentioned digg

User /u/{thing.author.name} in [this comment]({thing.permalink}?context=3) (thread *{thing.submission.title}*)

***

{thing.body}

This rule will match any comment including the word "digg" and alert the mods. Note the use of Python string formatting to include the object's content and a permalink. "thing" is either a Comment or Submission object as defined by the praw library (https://github.com/praw-dev/praw).

The "Description" line is not used, but simply there as a human-readable explanation.

Using matches in messages

Type: comment
Body: (Batman|Bruce Wayne|Dark Knight)
Actions: log,messagemods
Subject: Someone talking about batman

A user mentioned batman in [this comment]({thing.permalink}), using {matches[body][full]} to refer to him.

This rule uses the matches dict, which includes a key for each matching rule. For each rule, it includes a key named "full", containing the entire match, plus a key for each named group the regex contains (in this case none).