A Discord bot that allows users to chat with virtual characters. This project implements a character selection system with memory, affinity tracking, and customization.
- Character selection using
!select
command - Automatic user registration
- Conversation memory tracking
- Character-user relationship (affinity) tracking
- Customizable character details
- Optimized database connection pooling
- Robust error handling and retry mechanisms
- Performance improvements for AI chat
This project uses NoneBot framework and it is recommended to use pipx for installation.
For more details, check the documentation.
-
Install pipx
-
Install NoneBot scaffolding
pipx install nb-cli
-
Create a
.env
file from the template and fill in the required environment variables. -
Run the
main.py
scriptpython main.py
The following environment variables are required:
DISCORD_BOT_TOKEN
: Your Discord bot tokenOPENAI_API_KEY
: Your OpenAI API keyDATABASE_USER
: Database usernameDATABASE_PASSWORD
: Database passwordDATABASE_HOST
: Database hostDATABASE_PORT
: Database portDATABASE_NAME
: Database name
- Create your own Discord bot in the Discord Developer Portal
- Enable Message Content Intent under the Bot settings
- Use your bot's token in your local
.env
file - Run the bot locally for testing
!select <character_name>
- Select a character to chat with
The following optimizations have been implemented:
-
Database Connection Pooling:
- Improved connection validation and reconnection
- Better error handling for database operations
- Connection pool with proper resource management
-
Error Handling and Retry Mechanisms:
- Added retry with exponential backoff for OpenAI API calls
- Better error logging and categorization
- Type-safe validation for user inputs
-
Performance Optimizations:
- LRU caching for frequently used data
- Message history management to prevent memory issues
- Specific error handling for different API error types
-
Memory Management:
- Improved affinity value validation
- Automatic context pruning to maintain performance
- Better input validation