feat: add Discord bot integration

- Discord bot runs in background thread alongside desktop app
- State synchronization between Discord and desktop waifu
- Commands: !hello, !status
- Responds to mentions and DMs
- Complete setup guide in DISCORD_SETUP.md
- Graceful fallback if no token configured

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-30 22:24:22 -04:00
parent 337a681df3
commit ae1a349dd8
5 changed files with 174 additions and 23 deletions

26
CLAUDE.md Normal file
View File

@@ -0,0 +1,26 @@
&nbsp; Todos
&nbsp; ☒ Research VRM rendering libraries for Python
&nbsp; ☒ Set up project structure and dependencies
&nbsp; ☒ Create transparent window with draggable functionality
&nbsp; ☒ Test basic functionality and fix OpenGL issues
&nbsp; ☒ Initialize git repository and commit
&nbsp; ☒ Implement VRM model loading and rendering
&nbsp; ☐ Add sound effects on interaction
&nbsp; ☐ Create basic chat interface
&nbsp; ☐ Integrate local LLM backend
&nbsp; ☐ Implement expression changes based on LLM state
&nbsp; ☐ Create Discord bot and integrate with desktop app

112
DISCORD_SETUP.md Normal file
View File

@@ -0,0 +1,112 @@
# Discord Bot Setup Guide
## Step 1: Create Discord Application
1. Go to https://discord.com/developers/applications
2. Click "New Application"
3. Name it (e.g., "Desktop Waifu")
4. Click "Create"
## Step 2: Create Bot User
1. In your application, go to the "Bot" tab
2. Click "Add Bot"
3. Confirm by clicking "Yes, do it!"
## Step 3: Configure Bot Settings
### Bot Permissions
Under the "Bot" tab:
- Enable "MESSAGE CONTENT INTENT" (required to read messages)
- Enable "SERVER MEMBERS INTENT" (optional, for member events)
- Enable "PRESENCE INTENT" (optional, for presence updates)
### Bot Token
1. Under "TOKEN", click "Reset Token"
2. Copy the token (you'll need this for `.env`)
3. **NEVER share this token publicly!**
## Step 4: Invite Bot to Your Server
1. Go to "OAuth2" > "URL Generator"
2. Select scopes:
- `bot`
- `applications.commands`
3. Select bot permissions:
- Send Messages
- Read Message History
- Use Slash Commands
- Read Messages/View Channels
- Embed Links
- Attach Files
4. Copy the generated URL at the bottom
5. Open it in your browser
6. Select your server and authorize
## Step 5: Configure Application
1. Create `.env` file in project root:
```bash
cp .env.example .env
```
2. Edit `.env` and add your bot token:
```
DISCORD_BOT_TOKEN=YOUR_TOKEN_HERE
```
## Step 6: Test the Bot
1. Run the application:
```bash
python main.py
```
2. In Discord, try these commands:
- `!hello` - Bot will greet you
- `!status` - Check waifu's current mood
- `@BotName your message` - Mention the bot to chat
- Send a DM to the bot
## Available Commands
- `!hello` - Say hello to the waifu
- `!status` - Check current emotional state
## Features
### Automatic Responses
The bot will respond to:
- **Mentions** - When you @mention the bot in any channel
- **DMs** - When you send a direct message to the bot
### State Synchronization
The bot shares state with the desktop app:
- Emotions sync between Discord and desktop
- Conversation history is tracked
- Interactions update the desktop waifu in real-time
## Troubleshooting
### Bot doesn't respond
- Check that MESSAGE CONTENT INTENT is enabled
- Verify bot has "Send Messages" permission in the channel
- Check console for error messages
### Bot won't start
- Verify DISCORD_BOT_TOKEN is set in `.env`
- Check that token is valid (not expired/reset)
- Ensure discord.py is installed: `pip install discord.py`
### Bot joins but shows offline
- This is normal for Python bots
- They appear offline but will still respond to messages
## Security Notes
- **Never commit your `.env` file** to git (it's in `.gitignore`)
- **Never share your bot token** publicly
- If token is compromised, reset it in Discord Developer Portal
- Keep the bot token secret like a password

View File

@@ -56,15 +56,14 @@ cp .env.example .env
### Discord Setup (Optional) ### Discord Setup (Optional)
1. Create a Discord bot at https://discord.com/developers/applications **See [DISCORD_SETUP.md](DISCORD_SETUP.md) for detailed instructions.**
2. Enable these intents:
- Message Content Intent Quick setup:
- Server Members Intent 1. Create bot at https://discord.com/developers/applications
2. Enable "Message Content Intent" in Bot settings
3. Copy bot token to `DISCORD_BOT_TOKEN` in `.env` 3. Copy bot token to `DISCORD_BOT_TOKEN` in `.env`
4. Invite bot to your server with permissions: 4. Invite bot to your server using OAuth2 URL generator
- Send Messages 5. Bot will automatically start with the desktop app!
- Read Message History
- Use Slash Commands
### LLM Setup (Optional) ### LLM Setup (Optional)

33
main.py
View File

@@ -4,6 +4,7 @@ A VRM-based AI desktop companion with Discord integration
""" """
import sys import sys
import asyncio import asyncio
import threading
from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import Qt from PyQt6.QtCore import Qt
from dotenv import load_dotenv from dotenv import load_dotenv
@@ -16,6 +17,31 @@ from src.ui.waifu_window import WaifuWindow
from src.discord_bot.bot import WaifuBot from src.discord_bot.bot import WaifuBot
from src.core.state_manager import StateManager from src.core.state_manager import StateManager
def run_discord_bot(state_manager: StateManager):
"""Run Discord bot in a separate thread"""
import os
token = os.getenv('DISCORD_BOT_TOKEN')
if not token:
print("Discord bot disabled: DISCORD_BOT_TOKEN not set in .env file")
return
# Create new event loop for this thread
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Create and start bot
bot = WaifuBot(state_manager)
try:
print("Starting Discord bot...")
loop.run_until_complete(bot.start(token))
except KeyboardInterrupt:
print("Discord bot shutting down...")
loop.run_until_complete(bot.close())
except Exception as e:
print(f"Discord bot error: {e}")
finally:
loop.close()
def main(): def main():
"""Main application entry point""" """Main application entry point"""
# Create Qt Application # Create Qt Application
@@ -29,10 +55,9 @@ def main():
window = WaifuWindow(state_manager) window = WaifuWindow(state_manager)
window.show() window.show()
# Start Discord bot in background (if configured) # Start Discord bot in background thread
# TODO: Implement Discord bot integration discord_thread = threading.Thread(target=run_discord_bot, args=(state_manager,), daemon=True)
# discord_bot = WaifuBot(state_manager) discord_thread.start()
# asyncio.create_task(discord_bot.start())
# Run application # Run application
sys.exit(app.exec()) sys.exit(app.exec())

View File

@@ -66,14 +66,3 @@ class WaifuBot(commands.Bot):
# Process commands # Process commands
await self.process_commands(message) await self.process_commands(message)
async def start_bot(self):
"""Start the Discord bot"""
token = os.getenv('DISCORD_BOT_TOKEN')
if not token:
print("Warning: DISCORD_BOT_TOKEN not set in .env file")
return
try:
await self.start(token)
except Exception as e:
print(f"Error starting Discord bot: {e}")