aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-08-28 12:04:37 -0700
committerFuwn <[email protected]>2025-08-28 12:04:37 -0700
commit5ab3158d473af7c5b7e4f901fa187648961cb2ad (patch)
treeac42aaa21e40118acaecbe3c15219352c9673a4a /src
parentfeat: Add health API endpoint (diff)
downloadumabot-5ab3158d473af7c5b7e4f901fa187648961cb2ad.tar.xz
umabot-5ab3158d473af7c5b7e4f901fa187648961cb2ad.zip
feat: Add user-specified roleplay post limit
Diffstat (limited to 'src')
-rw-r--r--src/umabot/config.py2
-rw-r--r--src/umabot/rules/roleplay_limiter.py44
2 files changed, 37 insertions, 9 deletions
diff --git a/src/umabot/config.py b/src/umabot/config.py
index 93ebf54..f239891 100644
--- a/src/umabot/config.py
+++ b/src/umabot/config.py
@@ -29,6 +29,7 @@ class Config:
# Bot settings
check_interval: int = 60 # seconds
max_posts_per_day: int = 3
+ max_roleplay_posts_per_day: int = 1
dry_run: bool = False
@classmethod
@@ -51,6 +52,7 @@ class Config:
),
check_interval=int(os.getenv("CHECK_INTERVAL", "60")),
max_posts_per_day=int(os.getenv("MAX_POSTS_PER_DAY", "3")),
+ max_roleplay_posts_per_day=int(os.getenv("MAX_ROLEPLAY_POSTS_PER_DAY", "1")),
dry_run=os.getenv("DRY_RUN", "false").lower() == "true",
)
diff --git a/src/umabot/rules/roleplay_limiter.py b/src/umabot/rules/roleplay_limiter.py
index 2b88079..6819303 100644
--- a/src/umabot/rules/roleplay_limiter.py
+++ b/src/umabot/rules/roleplay_limiter.py
@@ -1,21 +1,24 @@
"""Roleplay post limiter rule."""
-from typing import Set
+import time
+from typing import Dict, List
import praw.models
from .base import Rule
class RoleplayLimiter(Rule):
- """Limits users to one roleplay post."""
+ """Limits users to a configurable number of roleplay posts per day."""
def __init__(self, config):
"""Initialize the roleplay limiter."""
super().__init__(config)
- self.roleplay_users: Set[str] = set()
+ self.user_roleplay_posts: Dict[str, List[float]] = {}
+ self.max_roleplay_posts = config.max_roleplay_posts_per_day
+ self.time_window = 24 * 60 * 60 # 24 hours in seconds
self.roleplay_flair = "Roleplay"
def should_remove(self, submission: praw.models.Submission) -> bool:
- """Check if a user has already posted a roleplay post."""
+ """Check if a user has posted too many roleplay posts."""
if not submission.author:
return False
@@ -24,22 +27,45 @@ class RoleplayLimiter(Rule):
return False
username = submission.author.name
+ current_time = time.time()
- # Check if user has already posted a roleplay post
- if username in self.roleplay_users:
+ # Clean old posts from tracking
+ self._clean_old_posts(username, current_time)
+
+ # Count current roleplay posts in the time window
+ if username not in self.user_roleplay_posts:
+ self.user_roleplay_posts[username] = []
+
+ post_count = len(self.user_roleplay_posts[username])
+
+ # Add current post to tracking
+ self.user_roleplay_posts[username].append(current_time)
+
+ # Check if this post exceeds the limit
+ if post_count >= self.max_roleplay_posts:
self.logger.info(
- f"User {username} has already posted a roleplay post"
+ f"User {username} has posted {post_count + 1} roleplay posts in 24 hours "
+ f"(limit: {self.max_roleplay_posts})"
)
return True
- # Add user to the set of roleplay posters
- self.roleplay_users.add(username)
return False
def get_removal_message(self, submission: praw.models.Submission) -> str:
"""Get the roleplay removal message."""
return self.config.roleplay_message
+ def _clean_old_posts(self, username: str, current_time: float) -> None:
+ """Remove roleplay posts older than the time window from tracking."""
+ if username not in self.user_roleplay_posts:
+ return
+
+ cutoff_time = current_time - self.time_window
+ self.user_roleplay_posts[username] = [
+ post_time for post_time in self.user_roleplay_posts[username]
+ if post_time > cutoff_time
+ ]
+
def _is_roleplay_post(self, submission: praw.models.Submission) -> bool:
"""Check if a submission has the roleplay flair."""
try: