This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Moonlark is a multi-functional chatbot built on Python 3.11+ and the Nonebot2 framework. It supports multiple chat platforms (QQ, Discord) through various adapters (OneBot V11/V12, QQ official adapter). The project follows a plugin-based architecture with 60+ custom plugins located in src/plugins/.
# Install dependencies using Poetry
poetry install
# Copy environment template
cp .env.template .env
# Edit .env with your configuration# Start the bot
poetry run nb run
# Or use nb-cli directly
nb run# Format code with Ruff
poetry run ruff format .
# Lint code
poetry run ruff check .
# Run pre-commit hooks manually
poetry run pre-commit run --all-files# Run tests with nonebug
poetry run nb test
# Run specific test file
poetry run nb test tests/test_name.py# Create new migration
poetry run nb orm revision --autogenerate -m "description"
# Apply migrations
poetry run nb orm upgrade
# Rollback migration
poetry run nb orm downgrade -1# Generate help documentation
poetry run nb run --script larkhelp-generate
# Initialize larkcave hashes
poetry run nb run --script larkcave-init-hashMoonlark uses a modular plugin architecture. All custom plugins are in src/plugins/ and are registered in src/pyproject.toml.
Core Infrastructure Plugins (use these in new plugins):
- LarkUser (
nonebot_plugin_larkuser): User information and registration system - LarkUtils (
nonebot_plugin_larkutils): User ID and group ID utilities - LarkLang (
nonebot_plugin_larklang): Localization/i18n system - Render (
nonebot_plugin_render): Jinja2 template rendering - LocalStore: File storage (via nonebot-plugin-localstore)
- ORM: Database operations (via nonebot-plugin-orm with SQLAlchemy)
- Alconna: Command parsing and message sending (via nonebot-plugin-alconna)
- HtmlRender: Markdown to image rendering (via nonebot-plugin-htmlrender)
Typical plugin layout:
nonebot_plugin_example/
├── __init__.py # Plugin metadata and exports
├── config.py # Plugin configuration
├── models.py # ORM models
├── help.yaml # Help documentation
├── matchers/ # Command handlers
└── utils/ # Utility functions
All user-facing text must be localized using LarkLang:
- Base language files:
src/lang/zh_hans/(Chinese Simplified - source) - Crowdin-managed:
src/lang/en_us/,src/lang/zh_tw/(DO NOT edit directly) - Use LarkLang's translation functions in code
- Default: SQLite (
database.db) - Configured via
SQLALCHEMY_DATABASE_URLin.env - Use nonebot-plugin-orm for all database operations
- Migrations managed with Alembic in
migrations/
- Async Operations: Use async/await for I/O operations (file, network, database)
- File Encoding: Always specify
encoding="utf-8"when opening files (Windows compatibility) - Storage: Use LocalStore for file storage, ORM for structured data
- Commands: Use Alconna for command parsing
- User Data: Access user info through LarkUser, not directly
- Localization: All user-visible text must use LarkLang
- Python 3.11+ syntax
- Line length: 120 characters
- Formatter: Ruff (configured in
pyproject.toml) - Type hints required for function parameters (ANN001)
- Line endings: LF (Unix-style)
- Don't use
git add -Aorgit add .(stage specific files) - Don't edit Crowdin-managed files (
src/lang/en_us/*,src/lang/zh_tw/*,README_eng.md,README_zho.md) - Don't store BLOB data in ORM (use LocalStore instead)
- Don't bypass LarkUser to get user information
- Don't use blocking I/O operations
- Test framework: pytest with pytest-asyncio
- Test files:
tests/ - Use
nonebugfor Nonebot plugin testing - Async mode: auto (configured in
pyproject.toml)
Key variables in .env:
SQLALCHEMY_DATABASE_URL: Database connection stringOPENAI_API_KEY,OPENAI_BASE_URL: OpenAI API configurationMODEL_OVERRIDE: JSON mapping for model overrides per applicationWOLFRAM_API_KEY: Wolfram Alpha APIBAIDU_API_KEY,BAIDU_SECRET_KEY: Baidu translation APISENTRY_DSN: Error tracking
Supported chat platforms:
- OneBot V11 (QQ)
- OneBot V12
- QQ Official (custom fork:
github.com/Moonlark-Dev/adapter-qq)
Moonlark/
├── src/
│ ├── plugins/ # 60+ custom plugins
│ ├── lang/ # Localization files
│ ├── static/ # Static assets
│ └── templates/ # Jinja2 templates
├── migrations/ # Alembic database migrations
├── tests/ # Test files
├── docs/ # Documentation
├── pyproject.toml # Root project config
├── src/pyproject.toml # Custom plugins package config
└── .env # Environment configuration (not in git)
AGPL-3.0 - All contributions must be compatible with this license.