From ae1a349dd8251cba0866827cab1cc2ed9215fdc2 Mon Sep 17 00:00:00 2001 From: Dani Date: Tue, 30 Sep 2025 22:24:22 -0400 Subject: [PATCH] feat: add Discord bot integration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- CLAUDE.md | 26 ++++++++++ DISCORD_SETUP.md | 112 +++++++++++++++++++++++++++++++++++++++++ README.md | 15 +++--- main.py | 33 ++++++++++-- src/discord_bot/bot.py | 11 ---- 5 files changed, 174 insertions(+), 23 deletions(-) create mode 100644 CLAUDE.md create mode 100644 DISCORD_SETUP.md diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..482f8ff --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,26 @@ + + +  Todos + +  ☒ Research VRM rendering libraries for Python + +  ☒ Set up project structure and dependencies + +  ☒ Create transparent window with draggable functionality + +  ☒ Test basic functionality and fix OpenGL issues + +  ☒ Initialize git repository and commit + +  ☒ Implement VRM model loading and rendering + +  ☐ Add sound effects on interaction + +  ☐ Create basic chat interface + +  ☐ Integrate local LLM backend + +  ☐ Implement expression changes based on LLM state + +  ☐ Create Discord bot and integrate with desktop app + diff --git a/DISCORD_SETUP.md b/DISCORD_SETUP.md new file mode 100644 index 0000000..2d71e31 --- /dev/null +++ b/DISCORD_SETUP.md @@ -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 diff --git a/README.md b/README.md index 7845a36..bd39791 100644 --- a/README.md +++ b/README.md @@ -56,15 +56,14 @@ cp .env.example .env ### Discord Setup (Optional) -1. Create a Discord bot at https://discord.com/developers/applications -2. Enable these intents: - - Message Content Intent - - Server Members Intent +**See [DISCORD_SETUP.md](DISCORD_SETUP.md) for detailed instructions.** + +Quick setup: +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` -4. Invite bot to your server with permissions: - - Send Messages - - Read Message History - - Use Slash Commands +4. Invite bot to your server using OAuth2 URL generator +5. Bot will automatically start with the desktop app! ### LLM Setup (Optional) diff --git a/main.py b/main.py index 49c004a..6555f97 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ A VRM-based AI desktop companion with Discord integration """ import sys import asyncio +import threading from PyQt6.QtWidgets import QApplication from PyQt6.QtCore import Qt 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.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(): """Main application entry point""" # Create Qt Application @@ -29,10 +55,9 @@ def main(): window = WaifuWindow(state_manager) window.show() - # Start Discord bot in background (if configured) - # TODO: Implement Discord bot integration - # discord_bot = WaifuBot(state_manager) - # asyncio.create_task(discord_bot.start()) + # Start Discord bot in background thread + discord_thread = threading.Thread(target=run_discord_bot, args=(state_manager,), daemon=True) + discord_thread.start() # Run application sys.exit(app.exec()) diff --git a/src/discord_bot/bot.py b/src/discord_bot/bot.py index 81d2837..8a484fa 100644 --- a/src/discord_bot/bot.py +++ b/src/discord_bot/bot.py @@ -66,14 +66,3 @@ class WaifuBot(commands.Bot): # Process commands 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}")