This commit is contained in:
2026-03-29 20:27:19 +01:00
parent 8fe3d1f030
commit 476eadd6c9
3 changed files with 170 additions and 5 deletions
+91 -5
View File
@@ -3,7 +3,7 @@ import praw
from config import get_reddit, Config
from update_checker import start_update_checker
BOT_VERSION = "2.2.1" # Change this for new releases
BOT_VERSION = "2.2.2" # Change this for new releases
BOT_NAME = "ModReplyBot" # Change this if bot name changes
import time
@@ -47,8 +47,14 @@ class ModReplyBot:
print(f"Chat message watcher error: {e}")
import time
time.sleep(30)
def comment_only(self, submission, comment_text):
def comment_only(self, submission, comment_text, matched_tag=None):
try:
# Check required text and prepend message if needed
if matched_tag and matched_tag in self.tag_required_text:
comment_text = self.check_required_text_and_prepend_message(
submission, comment_text, self.tag_required_text[matched_tag], matched_tag
)
comment = submission.reply(comment_text)
comment.mod.distinguish(sticky=True)
print(f"Commented (no approval) on: {submission.id}")
@@ -62,6 +68,16 @@ class ModReplyBot:
self.config_path = os.path.join(os.path.dirname(__file__), 'config', 'config.yaml')
self.triggers = []
self.comments = []
self.statuses = []
self.flair_ids = []
self.stickied = []
self.lock_post = []
self.lock_comment = []
self.trigger_required_text = []
self.tag_comments = {}
self.tag_statuses = {}
self.tag_flair_ids = {}
self.tag_required_text = {}
self.commented_posts = set()
self.commented_posts_file = os.path.join(os.path.dirname(__file__), 'DB', 'commented_posts.txt')
self.tagged_commented_posts_file = os.path.join(os.path.dirname(__file__), 'DB', 'tagged_commented_posts.txt')
@@ -77,6 +93,64 @@ class ModReplyBot:
if self.log_level == 'Debug' or (self.log_level == 'Default' and not debug_only):
print(message)
def check_required_text_and_prepend_message(self, submission, comment_text, required_text_list, matched_tag=None):
"""Check if post contains required text strings, prepend message if not."""
if not required_text_list:
return comment_text
# Get post content based on search_in
title = submission.title.lower()
body = getattr(submission, 'selftext', '').lower()
title_and_body = title + ' ' + body
print(f"[DEBUG] Checking required_text for tag '{matched_tag}', title='{title}', body='{body}'")
for req_entry in required_text_list:
req_tag = req_entry.get('tag', '').strip().lower()
print(f"[DEBUG] Checking req_entry with tag '{req_tag}'")
if req_tag and matched_tag and req_tag != matched_tag.lower():
print(f"[DEBUG] Skipping req_entry, tag mismatch")
continue # This required_text is for a different tag
text_list = req_entry.get('text', '').split(',')
def normalize_req_text(item):
item = item.strip()
# Remove surrounding quotes (single or double) for exact phrases
if (item.startswith('"') and item.endswith('"')) or (item.startswith("'") and item.endswith("'")):
item = item[1:-1]
return item.strip().lower()
text_list = [normalize_req_text(t) for t in text_list if t.strip()]
print(f"[DEBUG] Required text_list: {text_list}")
search_in = req_entry.get('search_in', 'title_and_body').lower()
if search_in == 'title':
content = title
elif search_in == 'body':
content = body
else: # title_and_body
content = title_and_body
print(f"[DEBUG] Searching in '{search_in}', content='{content}'")
# Check if any required text is present
found = False
for req_text in text_list:
if req_text in content:
print(f"[DEBUG] Found required text '{req_text}' in content")
found = True
break
if not found:
print(f"[DEBUG] Required text not found, prepending message")
# Prepend the message
message = req_entry.get('message', '').strip()
if message:
comment_text = message + '\n\n' + comment_text
else:
print(f"[DEBUG] Required text found, not prepending message")
return comment_text
def ensure_config_file(self):
import os
if not os.path.exists(self.config_path):
@@ -130,9 +204,11 @@ class ModReplyBot:
self.stickied = []
self.lock_post = []
self.lock_comment = []
self.trigger_required_text = []
self.tag_comments = {}
self.tag_statuses = {}
self.tag_flair_ids = {}
self.tag_required_text = {}
for entry in config.get('triggers', []):
self.triggers.append(entry.get('trigger', '').strip())
self.comments.append(entry.get('comment', '').strip())
@@ -145,16 +221,19 @@ class ModReplyBot:
lock_post_val = lock_post_val.lower() in ['true', '1', 'yes']
self.lock_post.append(bool(lock_post_val))
self.lock_comment.append(bool(entry.get('lock_comment', False)))
self.trigger_required_text.append(entry.get('required_text', []))
for entry in config.get('post_tags', []):
tags_str = entry.get('tag', '').strip()
comment = entry.get('comment', '').strip()
status = entry.get('status', 'enabled').strip().lower()
flair_id = entry.get('flair_id', '').strip()
required_text = entry.get('required_text', [])
tags = [t.strip().lower() for t in tags_str.split(',') if t.strip()]
for tag in tags:
self.tag_comments[tag] = comment
self.tag_statuses[tag] = status
self.tag_flair_ids[tag] = flair_id
self.tag_required_text[tag] = required_text
# Parse ignore_tags
self.ignore_tags = set()
for entry in config.get('ignore_tags', []):
@@ -332,7 +411,7 @@ class ModReplyBot:
except Exception as e:
print(f"[BACKFILL] Error setting flair '{flair_id}' for post {submission.id}: {e}")
print(f"[BACKFILL] Auto-commenting on post {submission.id} with tag '{matched_tag}'")
self.comment_only(submission, comment_text)
self.comment_only(submission, comment_text, matched_tag)
backfill_count += 1
except Exception as e:
print(f"[BACKFILL] Error during backfill: {e}")
@@ -385,7 +464,7 @@ class ModReplyBot:
except Exception as e:
print(f"[TAG WATCH] Error setting flair '{flair_id}' for post {submission.id}: {e}")
print(f"Auto-commenting on post {submission.id} with tag '{matched_tag}'")
self.comment_only(submission, comment_text)
self.comment_only(submission, comment_text, matched_tag)
except Exception as e:
print(f"Tag post watcher error: {e}")
import time
@@ -447,7 +526,7 @@ class ModReplyBot:
except Exception as e:
print(f"[MODQUEUE WATCH] Error setting flair '{flair_id}' for post {submission.id}: {e}")
print(f"Auto-commenting on filtered/removed post {submission.id} with tag '{matched_tag}' from modqueue")
self.comment_only(submission, comment_text)
self.comment_only(submission, comment_text, matched_tag)
except Exception as e:
print(f"Modqueue watcher error: {e}")
import time
@@ -546,6 +625,13 @@ class ModReplyBot:
print(f"[DEBUG] Locked post {submission.id}")
except Exception as e:
print(f"[DEBUG] Error locking post {submission.id}: {e}")
# Check required text for triggers
if trigger_idx < len(self.trigger_required_text):
comment_text = self.check_required_text_and_prepend_message(
submission, comment_text, self.trigger_required_text[trigger_idx], self.triggers[trigger_idx]
)
if status == 'enabled':
print(f"[DEBUG] Submission object: {submission}, ID: {submission.id}, Type: {type(submission)}")
comment = submission.reply(comment_text)