Changes: - Strip AI providers to image-only analysis (remove text/phishing methods) - Simplify guild models (remove BannedWord, reduce GuildSettings columns) - Create migration to drop unused tables and columns - Rewrite README for minimal bot focus - Update CLAUDE.md architecture documentation Result: -992 lines, +158 lines (net -834 lines) Cost-conscious bot ready for deployment.
3.0 KiB
3.0 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
GuardDen is a minimal, cost-conscious Discord moderation bot focused on spam detection and NSFW image filtering. Built with discord.py, PostgreSQL, and optional AI integration (Claude/OpenAI) for image analysis only. Self-hosted with Docker support.
Commands
# Install dependencies
pip install -e ".[dev,ai]"
# Run the bot
python -m guardden
# Run tests
pytest
# Run single test
pytest tests/test_automod.py::TestAutomodService::test_spam_detection
# Lint and format
ruff check src tests
ruff format src tests
# Type checking
mypy src
# Docker deployment
docker compose up -d
# Database migrations
alembic upgrade head
Architecture
src/guardden/bot.py- Main bot class (GuardDen) extendingcommands.Bot, manages lifecycle and servicessrc/guardden/config.py- Pydantic settings loaded from environment variables (prefix:GUARDDEN_)src/guardden/models/guild.py- SQLAlchemy 2.0 async models for guilds and settingssrc/guardden/services/- Business logic (database, guild config, automod, AI, rate limiting)src/guardden/cogs/- Discord command groups (automod, ai_moderation, owner)config.yml- Single YAML file for bot configuration
Key Patterns
- All database operations use async SQLAlchemy with
asyncpg - Guild configurations loaded from single
config.ymlfile (not per-guild) - Discord snowflake IDs stored as
BigIntegerin PostgreSQL - No moderation logging or strike system
- Environment variables:
GUARDDEN_DISCORD_TOKEN,GUARDDEN_DATABASE_URL, AI keys
Automod System
AutomodServiceinservices/automod.pyhandles spam detection- Checks: message rate limit → duplicate messages → mass mentions
- Spam tracking uses per-guild, per-user trackers with automatic cleanup
- Results return
AutomodResultdataclass with actions to take - Everyone gets moderated (no whitelist, no bypass for permissions)
AI Moderation System
services/ai/contains provider abstraction and implementationsAIProviderbase class defines interface:analyze_image()onlyAnthropicProviderandOpenAIProviderimplement the interfaceNullProviderused when AI is disabled (returns empty results)- Factory pattern via
create_ai_provider(provider, api_key) ImageAnalysisResultincludes NSFW categories, severity, confidence- Sensitivity setting (0-100) adjusts thresholds
- NSFW-Only Filtering (default:
True): Only sexual content is filtered - Cost Controls: Rate limiting, deduplication, file size limits, max images per message
AIRateLimiterinservices/ai_rate_limiter.pytracks usage
Adding New Cogs
- Create file in
src/guardden/cogs/ - Implement
setup(bot)async function - Add cog path to
_load_cogs()inbot.py
Adding New AI Provider
- Create
src/guardden/services/ai/newprovider.py - Implement
AIProviderabstract class - Add to factory in
services/ai/factory.py - Add config option in
config.py