diff --git a/main.py b/main.py index 13dfffd..399714f 100644 --- a/main.py +++ b/main.py @@ -209,9 +209,9 @@ def chat_message_watcher(reddit): chat_requests_file = os.path.join(DB_DIR, 'chat_wiki_requests.txt') processed_message_ids = set() subreddit = reddit.subreddit(config.SUBREDDIT) - + print("[CHAT] Chat message watcher started") - + # Load previously processed message IDs if os.path.exists(chat_requests_file): try: @@ -220,14 +220,26 @@ def chat_message_watcher(reddit): processed_message_ids.add(line.strip()) except Exception as e: print(f"[CHAT] Error loading processed message IDs: {e}") - + + # Get the latest message timestamp at startup + last_seen_timestamp = None + try: + # Fetch the most recent message (limit=1) + latest = list(reddit.inbox.all(limit=1)) + if latest and hasattr(latest[0], 'created_utc'): + last_seen_timestamp = latest[0].created_utc + print(f"[CHAT] Last seen message timestamp at startup: {last_seen_timestamp}") + except Exception as e: + print(f"[CHAT] Error fetching latest message timestamp: {e}") + while True: try: for message in reddit.inbox.stream(): - # Skip if no ID or already processed - if not hasattr(message, 'id') or message.id in processed_message_ids: + # Skip if no ID, already processed, or message is older than startup + if (not hasattr(message, 'id') or message.id in processed_message_ids or + (last_seen_timestamp is not None and hasattr(message, 'created_utc') and message.created_utc <= last_seen_timestamp)): continue - + # Mark as processed processed_message_ids.add(message.id) try: @@ -235,16 +247,16 @@ def chat_message_watcher(reddit): f.write(message.id + '\n') except Exception as e: print(f"[CHAT] Error saving message ID: {e}") - + # Check for reload-config command if hasattr(message, 'body') and 'reload-config' in message.body.lower(): author = getattr(message, 'author', None) - + # Verify sender is a moderator try: if author and author in subreddit.moderator(): print(f"[CHAT] Moderator '{author}' requested config reload") - + # Reload wiki configuration try: wiki_config.fetch_from_wiki() @@ -253,18 +265,18 @@ def chat_message_watcher(reddit): except Exception as e: print(f"[CHAT] Failed to reload wiki config: {e}") reply_text = f"✗ Failed to reload wiki config: {e}" - + # Reply to the message try: message.reply(reply_text) print(f"[CHAT] Replied to message {message.id}") except Exception as e: - print(f"[CHAT] Error replying to message {message.id}: {e}") + print(f"[CHAT] Error replying to message {message.id}: {message.id}: {e}") else: print(f"[CHAT] Non-moderator '{author}' attempted reload-config (ignored)") except Exception as e: print(f"[CHAT] Error checking moderator status: {e}") - + # Check for repost-latest command elif hasattr(message, 'body') and 'repost-latest' in message.body.lower(): author = getattr(message, 'author', None)