ModReplyBot 1.0.0 release

This commit is contained in:
2026-03-08 21:32:51 +00:00
parent 739230625b
commit 95410c64bf
5 changed files with 67 additions and 36 deletions
+58 -29
View File
@@ -5,51 +5,80 @@ import time
class ModBot:
def __init__(self):
import os
self.reddit = get_reddit()
self.subreddit = self.reddit.subreddit(Config.SUBREDDIT)
self.wiki_page = Config.WIKI_PAGE
self.config_path = os.path.join(os.path.dirname(__file__), 'config', 'config.yaml')
self.triggers = []
self.comments = []
self.ensure_config_file()
def fetch_wiki_config(self):
def ensure_config_file(self):
import os
if not os.path.exists(self.config_path):
default_yaml = (
'triggers:\n'
' - trigger: help\n'
' comment: |\n'
' Thank you for your report!\n'
' This post is now approved.\n'
' - trigger: question\n'
' comment: |\n'
' This post has been approved.\n'
' Your question will be answered soon.\n'
)
os.makedirs(os.path.dirname(self.config_path), exist_ok=True)
with open(self.config_path, 'w', encoding='utf-8') as f:
f.write(default_yaml)
def fetch_yaml_config(self):
import yaml
try:
wiki_content = self.subreddit.wiki[self.wiki_page].content_md
# Example Automoderator YAML format:
# triggers:
# - trigger: help
# comment: |
# Thank you for your report!
# This post is now approved.
# - trigger: question
# comment: |
# This post has been approved.
config = yaml.safe_load(wiki_content)
with open(self.config_path, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f)
self.triggers = []
self.comments = []
for entry in config.get('triggers', []):
self.triggers.append(entry.get('trigger', '').strip())
self.comments.append(entry.get('comment', '').strip())
except Exception as e:
print(f"Error fetching wiki config: {e}")
print(f"Error fetching YAML config: {e}")
def run(self):
print("ModBot started. Watching for mod reports...")
while True:
self.fetch_wiki_config()
for report in self.subreddit.mod.reports(limit=25):
self.handle_report(report)
time.sleep(30) # Poll every 30 seconds
def handle_report(self, report):
if not hasattr(report, 'mod_reports') or not report.mod_reports:
print("ModReplyBot started. Watching for comments...")
try:
self.fetch_yaml_config()
print(f"Triggers loaded: {self.triggers}")
print(f"Reddit user: {self.reddit.user.me()}")
print(f"Subreddit: {self.subreddit.display_name}")
except Exception as e:
print(f"Startup error: {e}")
return
for mod_report in report.mod_reports:
report_text = mod_report[0].lower()
for idx, trigger in enumerate(self.triggers):
if trigger.lower() in report_text:
self.approve_and_comment(report, self.comments[idx])
break
while True:
try:
self.fetch_yaml_config()
for comment in self.subreddit.stream.comments(skip_existing=True):
self.handle_comment(comment)
except Exception as e:
print(f"Main loop error: {e}")
time.sleep(5) # Poll every 5 seconds
def handle_comment(self, comment):
comment_body = comment.body.lower()
for idx, trigger in enumerate(self.triggers):
expected = f"!{trigger.lower()}"
if expected in comment_body:
# Remove the triggering comment
try:
comment.mod.remove()
print(f"Removed triggering comment: {comment.id}")
except Exception as e:
print(f"Error removing comment: {e}")
# Approve the submission and post bot's comment
submission = comment.submission
self.fetch_yaml_config()
self.approve_and_comment(submission, self.comments[idx])
break
def approve_and_comment(self, submission, comment_text):
try: