Files
Modreplybot/modbot.py
T

66 lines
2.3 KiB
Python
Raw Normal View History

2026-03-08 18:08:24 +00:00
import praw
from config import get_reddit, Config
import time
class ModBot:
def __init__(self):
self.reddit = get_reddit()
self.subreddit = self.reddit.subreddit(Config.SUBREDDIT)
self.wiki_page = Config.WIKI_PAGE
self.triggers = []
self.comments = []
def fetch_wiki_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)
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}")
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:
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
def approve_and_comment(self, submission, comment_text):
try:
submission.mod.approve()
comment = submission.reply(comment_text)
comment.mod.distinguish(sticky=True)
print(f"Approved and commented on: {submission.id}")
except Exception as e:
print(f"Error approving/commenting: {e}")
if __name__ == "__main__":
bot = ModBot()
bot.run()