diff --git a/Dockerfile b/Dockerfile index 56549aa..e84eedb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,4 +5,5 @@ COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY bot.py . COPY config.py . +ENV PYTHONUNBUFFERED=1 CMD ["python", "bot.py"] diff --git a/bot.py b/bot.py index 6e3b3e6..b32e9c3 100644 --- a/bot.py +++ b/bot.py @@ -66,45 +66,84 @@ def chat_message_watcher(reddit, subreddit_name): with open(chat_requests_file, 'a', encoding='utf-8') as f: f.write(message.id + '\n') - if hasattr(message, 'body'): - message_body = message.body.lower() - # Check if sender is a moderator - author = getattr(message, 'author', None) - if author and author in subreddit.moderator(): - # Load current config to check for triggers - config = fetch_config_from_wiki(reddit, subreddit_name, WIKI_PAGE) - posts_config = config.get('posts', []) + # Check if message has body and author + if not hasattr(message, 'body'): + continue + + author = getattr(message, 'author', None) + if not author: + continue + + # Check if sender is a moderator + try: + is_mod = author in subreddit.moderator() + except Exception as e: + print(f"[CHAT WATCH] Error checking if {author} is mod: {e}") + continue + + if not is_mod: + continue + + message_body_lower = message.body.lower() + print(f"[CHAT WATCH] Moderator '{author}' sent message: {message.body[:100]}") + + # Handle special 'reload-config' command + if 'reload-config' in message_body_lower: + print(f"[CHAT WATCH] Reload-config command detected.") + result = validate_config_from_wiki(reddit, subreddit_name, WIKI_PAGE) + if result: + print("[CHAT WATCH] Wiki config validated successfully.") + reply_text = "Config validated successfully. Config is valid YAML." + else: + print("[CHAT WATCH] Wiki config validation failed.") + reply_text = "Config validation failed. Check the wiki config YAML formatting." + try: + message.reply(reply_text) + print(f"[CHAT WATCH] Replied to message {message.id}.") + except Exception as e: + print(f"[CHAT WATCH] Error replying to message {message.id}: {e}") + continue + + # Load current config to check for triggers + config = fetch_config_from_wiki(reddit, subreddit_name, WIKI_PAGE) + posts_config = config.get('posts', []) + + trigger_found = False + + # Check if message contains any trigger + for post_config in posts_config: + if not isinstance(post_config, dict): + continue + + trigger = post_config.get('trigger', '').lower() + if trigger and trigger in message_body_lower: + trigger_found = True + print(f"[CHAT WATCH] Matched trigger '{trigger}' in message.") + + # Get posts for this trigger + trigger_posts = get_trigger_posts(reddit, subreddit_name, WIKI_PAGE, trigger) + + if trigger_posts: + print(f"[CHAT WATCH] Found {len(trigger_posts)} post(s) for trigger '{trigger}'.") + make_posts(reddit, subreddit_name, trigger_posts) + reply_text = f"Successfully posted {len(trigger_posts)} post(s) for trigger '{trigger}'." + else: + print(f"[CHAT WATCH] No posts found for trigger '{trigger}'.") + reply_text = f"No posts configured for trigger '{trigger}'." + + try: + message.reply(reply_text) + print(f"[CHAT WATCH] Replied to message {message.id}.") + except Exception as e: + print(f"[CHAT WATCH] Error replying to message {message.id}: {e}") + + # Only process the first matching trigger per message + break - # Check if message contains any trigger - for post_config in posts_config: - if not isinstance(post_config, dict): - continue - - trigger = post_config.get('trigger', '').lower() - if trigger and trigger in message_body: - print(f"[CHAT WATCH] Moderator '{author}' triggered '{trigger}'.") - - # Get posts for this trigger - trigger_posts = get_trigger_posts(reddit, subreddit_name, WIKI_PAGE, trigger) - - if trigger_posts: - print(f"[CHAT WATCH] Found {len(trigger_posts)} post(s) for trigger '{trigger}'.") - make_posts(reddit, subreddit_name, trigger_posts) - reply_text = f"Successfully posted {len(trigger_posts)} post(s) for trigger '{trigger}'." - else: - print(f"[CHAT WATCH] No posts found for trigger '{trigger}'.") - reply_text = f"No posts configured for trigger '{trigger}'." - - try: - message.reply(reply_text) - print(f"[CHAT WATCH] Replied to chat message {message.id}.") - except Exception as e: - print(f"[CHAT WATCH] Error replying to chat message {message.id}: {e}") - - # Only process the first matching trigger per message - break except Exception as e: print(f"[CHAT WATCH] Chat message watcher error: {e}") + import traceback + traceback.print_exc() time.sleep(30) diff --git a/docker-compose.yml b/docker-compose.yml index b6f4855..2e0e1ac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: - testpostbot: - image: slfhstd.uk/slfhstd/testpostbot:latest + testpostsbot: + image: slfhstd.uk/slfhstd/testpostsbot:dev env_file: - .env restart: unless-stopped