diff options
| author | Fuwn <[email protected]> | 2025-08-28 12:04:37 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-08-28 12:04:37 -0700 |
| commit | 5ab3158d473af7c5b7e4f901fa187648961cb2ad (patch) | |
| tree | ac42aaa21e40118acaecbe3c15219352c9673a4a | |
| parent | feat: Add health API endpoint (diff) | |
| download | umabot-5ab3158d473af7c5b7e4f901fa187648961cb2ad.tar.xz umabot-5ab3158d473af7c5b7e4f901fa187648961cb2ad.zip | |
feat: Add user-specified roleplay post limit
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | env.example | 2 | ||||
| -rw-r--r-- | render.yaml | 2 | ||||
| -rw-r--r-- | src/umabot/config.py | 2 | ||||
| -rw-r--r-- | src/umabot/rules/roleplay_limiter.py | 44 |
5 files changed, 42 insertions, 9 deletions
@@ -161,6 +161,7 @@ The bot can be deployed on any platform that supports Python: | `ROLEPLAY_MESSAGE` | Message for roleplay removals | Customizable | | `CHECK_INTERVAL` | Seconds between checks | `60` | | `MAX_POSTS_PER_DAY` | Max posts per user per day | `3` | +| `MAX_ROLEPLAY_POSTS_PER_DAY` | Max roleplay posts per user per day | `1` | | `DRY_RUN` | Enable dry-run mode | `false` | ## Development diff --git a/env.example b/env.example index 1e19fd4..efae35a 100644 --- a/env.example +++ b/env.example @@ -20,5 +20,7 @@ ROLEPLAY_MESSAGE=Your post has been removed. Only one roleplay post is allowed p CHECK_INTERVAL=60 # Maximum number of posts a user can make in 24 hours MAX_POSTS_PER_DAY=3 +# Maximum number of roleplay posts a user can make in 24 hours +MAX_ROLEPLAY_POSTS_PER_DAY=1 # Set to true to test without actually removing posts DRY_RUN=false diff --git a/render.yaml b/render.yaml index 40ec5b2..7fc71f9 100644 --- a/render.yaml +++ b/render.yaml @@ -23,6 +23,8 @@ services: value: "60" - key: MAX_POSTS_PER_DAY value: "3" + - key: MAX_ROLEPLAY_POSTS_PER_DAY + value: "1" - key: DRY_RUN value: "false" - key: SPAM_MESSAGE 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: |