Features: - Core moderation: warn, kick, ban, timeout, strike system - Automod: banned words filter, scam detection, anti-spam, link filtering - AI moderation: Claude/OpenAI integration, NSFW detection, phishing analysis - Verification system: button, captcha, math, emoji challenges - Rate limiting system with configurable scopes - Event logging: joins, leaves, message edits/deletes, voice activity - Per-guild configuration with caching - Docker deployment support Bug fixes applied: - Fixed await on session.delete() in guild_config.py - Fixed memory leak in AI moderation message tracking (use deque) - Added error handling to bot shutdown - Added error handling to timeout command - Removed unused Literal import - Added prefix validation - Added image analysis limit (3 per message) - Fixed test mock for SQLAlchemy model
3.8 KiB
3.8 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 Discord moderation bot built with discord.py, PostgreSQL, and optional AI integration (Claude/OpenAI). 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_verification.py::TestVerificationService::test_verify_correct
# Lint and format
ruff check src tests
ruff format src tests
# Type checking
mypy src
# Docker deployment
docker compose up -d
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/- SQLAlchemy 2.0 async models for PostgreSQLsrc/guardden/services/- Business logic (database, guild config, automod, AI, verification, rate limiting)src/guardden/cogs/- Discord command groups (events, moderation, admin, automod, ai_moderation, verification)
Key Patterns
- All database operations use async SQLAlchemy with
asyncpg - Guild configurations are cached in
GuildConfigService._cache - Discord snowflake IDs stored as
BigIntegerin PostgreSQL - Moderation actions logged to
ModerationLogtable with automatic strike escalation - Environment variables:
GUARDDEN_DISCORD_TOKEN,GUARDDEN_DATABASE_URL
Automod System
AutomodServiceinservices/automod.pyhandles rule-based content filtering- Checks run in order: banned words → scam links → spam → invite links
- Spam tracking uses per-guild, per-user trackers with automatic cleanup
- Scam detection uses compiled regex patterns in
SCAM_PATTERNSlist - Results return
AutomodResultdataclass with actions to take
AI Moderation System
services/ai/contains provider abstraction and implementationsAIProviderbase class defines interface:moderate_text(),analyze_image(),analyze_phishing()AnthropicProviderandOpenAIProviderimplement the interfaceNullProviderused when AI is disabled (returns empty results)- Factory pattern via
create_ai_provider(provider, api_key) ModerationResultincludes severity scoring based on confidence + category weights- Sensitivity setting (0-100) adjusts thresholds per guild
Verification System
VerificationServiceinservices/verification.pymanages challenges- Challenge types: button, captcha, math, emoji (via
ChallengeGeneratorclasses) PendingVerificationtracks user challenges with expiry and attempt limits- Discord UI components in
cogs/verification.py:VerifyButton,EmojiButton,CaptchaModal - Background task cleans up expired verifications every 5 minutes
Rate Limiting System
RateLimiterinservices/ratelimit.pyprovides general-purpose rate limiting- Scopes: USER (global), MEMBER (per-guild), CHANNEL, GUILD
@ratelimit()decorator for easy command rate limitingget_rate_limiter()returns singleton instance- Default limits configured for commands, moderation, verification, messages
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
Adding New Challenge Type
- Create new
ChallengeGeneratorsubclass inservices/verification.py - Add to
ChallengeTypeenum - Register in
VerificationService._generators - Create corresponding UI components in
cogs/verification.pyif needed