Skip to main content

Statefulness

Standard automoderators are stateless: they evaluate each post or comment in isolation without memory of past events. Modegator brings statefulness to subreddit automation by providing built-in storage actions that interface directly with a persistent Redis database.

By using storage actions, you can track user actions, count events, and store persistent flags that can be referenced in later rules, scheduled tasks, or UI actions.


How Statefulness Works

Statefulness in Modegator relies on a two-step pattern:

  1. Write State: A rule, scheduled task, or UI action writes or updates a value in storage (e.g., using increment_counter or store_value).
  2. Read & Compare State: Another rule retrieves that stored value dynamically during condition evaluation using special prefixes:
    • counter_[key]: Fetches a numeric counter from storage (evaluated as a number).
    • custom_[key]: Fetches a generic value from storage (evaluated as a string).

Placeholders (like {{author}}) can be dynamically resolved inside the key names themselves (e.g., comment_count_{{author}}), allowing you to track state for individual users.


Example: Stateful Comment Gate

Below is a complete, valid configuration demonstrating how to enforce a gate that locks link submissions for users who have not participated sufficiently in the community.

version: "1.0"
name: "Stateful Comment Gate"
description: "Locks link posts unless the user has at least 5 comments in the subreddit."

rules:
# STEP 1: Store/Increment State on Comment
- name: "Track User Comment Count"
trigger:
event: "CommentSubmit"
conditions: [] # Run always on every comment
actions:
- type: "increment_counter"
key: "comment_count_{{author}}" # Resolves to e.g., comment_count_YashArote

# STEP 2: Fetch and Check State on Post Submit
- name: "Enforce Comment Gate"
trigger:
event: "PostSubmit"
conditions:
# Check link type
- field: "post_link_type"
operator: "=="
value: "link"
# Fetch the user's specific comment counter from Redis and check if it is < 5
- field: "counter_comment_count_{{author}}"
operator: "<"
value: 5
actions:
# If they don't have 5 comments, remove the post
- type: "remove_post"
spam: false
# Reply explaining why it was removed
- type: "submit_comment"
text: "Hi u/{{author}}, link submissions are locked until you have commented at least 5 times in this community."
distinguish: true

Explanation of the Flow

  1. Step 1 (Track User Comment Count):

    • Listens to the CommentSubmit trigger.
    • For every new comment submitted by a user, the increment_counter action is triggered.
    • The key name uses the {{author}} placeholder (e.g. comment_count_YashArote). The engine increments this specific user's Redis entry by 1.
  2. Step 2 (Enforce Comment Gate):

    • Listens to the PostSubmit trigger.
    • First condition: post_link_type == "link" checks if the new post is a link.
    • Second condition: counter_comment_count_{{author}} < 5 reads the current integer counter from Redis for this author and checks if it is less than 5.
    • If both conditions match, the post is automatically removed, and a sticky reply is submitted to inform the user about the gate.

Integrating State with Scheduled Tasks

Scheduled tasks (crons) are ideal for resetting or sweeping state database entries periodically.

For example, you can schedule a nightly reset of daily interaction counts:

version: "1.0"
name: "Nightly Reset Tasks"
description: "Demonstrates sweeping state with scheduled cron tasks."

scheduled:
- name: "Reset Daily Counter"
cron: "0 0 * * *"
actions:
- type: "store_value"
key: "daily_flag"
value: "reset"