From 269ba151652cd21516170df1c83bc40185a4d938 Mon Sep 17 00:00:00 2001 From: latte Date: Tue, 27 Jan 2026 20:29:02 +0100 Subject: [PATCH] fix: Add permission checks and debug logging for AI moderation Improve visibility into why images might not be analyzed in some channels. Changes: - Add permission check for 'Manage Messages' before processing - Add debug logging when skipping messages (already analyzed, no media) - Add info logging when checking messages (shows channel, attachments, embeds) - Add warning when AI provider not configured - Add channel name to rate limit warnings Debug output now shows: - Which channels lack permissions - Why messages are skipped - Rate limit status per channel - AI provider configuration status Helps diagnose: 'bot niet pakt in alle chats de content van images' --- src/guardden/cogs/ai_moderation.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/guardden/cogs/ai_moderation.py b/src/guardden/cogs/ai_moderation.py index 40af90a..ea425bd 100644 --- a/src/guardden/cogs/ai_moderation.py +++ b/src/guardden/cogs/ai_moderation.py @@ -86,6 +86,11 @@ class AIModeration(commands.Cog): if not message.guild: return + # Check bot permissions in this channel + if not message.channel.permissions_for(message.guild.me).manage_messages: + logger.debug(f"Missing Manage Messages permission in #{message.channel.name}") + return + # Get config from YAML config = self.bot.config_loader if not config.get_setting("ai_moderation.enabled", True): @@ -123,8 +128,19 @@ class AIModeration(commands.Cog): # Check if should analyze (has images/embeds, not analyzed yet) if not self._should_analyze(message): + logger.debug( + f"Skipping analysis in #{message.channel.name}: " + f"already_analyzed={message.id in self._analyzed_messages}, " + f"has_media={bool(message.attachments or message.embeds)}" + ) return + # Log that we're about to check this message + logger.info( + f"Checking message from {message.author} in #{message.channel.name} " + f"({len(message.attachments)} attachments, {len(message.embeds)} embeds)" + ) + # Check rate limits (CRITICAL for cost control) max_guild_per_hour = config.get_setting("ai_moderation.max_checks_per_hour_per_guild", 25) max_user_per_hour = config.get_setting("ai_moderation.max_checks_per_user_per_hour", 5) @@ -138,12 +154,20 @@ class AIModeration(commands.Cog): if rate_limit_result["is_limited"]: logger.warning( - f"AI rate limit hit: {rate_limit_result['reason']} " + f"AI rate limit hit in #{message.channel.name}: {rate_limit_result['reason']} " f"(guild: {rate_limit_result['guild_checks_this_hour']}/{max_guild_per_hour}, " f"user: {rate_limit_result['user_checks_this_hour']}/{max_user_per_hour})" ) return + # Check if AI provider is configured + if self.bot.ai_provider is None: + logger.warning( + f"AI provider not configured but ai_moderation.enabled=true. " + f"Set GUARDDEN_AI_PROVIDER in .env to 'anthropic' or 'openai'" + ) + return + # Get AI settings sensitivity = config.get_setting("ai_moderation.sensitivity", 80) nsfw_only_filtering = config.get_setting("ai_moderation.nsfw_only_filtering", True)