3.7 KiB
3.7 KiB
Discord Sync Integration
This document explains how to set up the Discord OAuth integration between your Flutter app and Discord bot.
Overview
The integration allows users to:
- Log in with their Discord account
- Sync conversations between the Flutter app and Discord bot
- Import conversations from Discord to the Flutter app
- Export conversations from the Flutter app to Discord
Setup Instructions
1. Discord Developer Portal Setup
- Go to the Discord Developer Portal
- Click "New Application" and give it a name (e.g., "OpenRouter GUI")
- Go to the "OAuth2" section
- Add a redirect URL:
openroutergui://auth
- Copy the "Client ID" - you'll need this for the Flutter app
2. Flutter App Setup
-
Open
lib/services/discord_oauth_service.dart
-
Replace
YOUR_DISCORD_CLIENT_ID
with the Client ID from the Discord Developer Portal:static const String clientId = 'YOUR_DISCORD_CLIENT_ID';
-
Open
lib/services/sync_service.dart
-
Replace
YOUR_BOT_API_URL
with the URL where your Discord bot's API will be running:static const String botApiUrl = 'YOUR_BOT_API_URL';
3. Discord Bot Setup
-
Copy the
discord_bot_sync_api.py
file to your Discord bot project -
Install the required dependencies:
pip install fastapi uvicorn pydantic
-
Add the following code to your main bot file (e.g.,
bot.py
):import threading import uvicorn def run_api(): uvicorn.run("discord_bot_sync_api:app", host="0.0.0.0", port=8000) # Start the API in a separate thread api_thread = threading.Thread(target=run_api) api_thread.daemon = True api_thread.start()
-
Modify your
ai_cog.py
file to integrate with the sync API:from discord_bot_sync_api import save_discord_conversation, load_conversations, user_conversations # In your _get_ai_response method, after getting the response: messages = conversation_history[user_id] save_discord_conversation(str(user_id), messages, settings["model"]) # Add a command to view sync status: @commands.command(name="aisync") async def ai_sync_status(self, ctx: commands.Context): user_id = str(ctx.author.id) if user_id not in user_conversations or not user_conversations[user_id]: await ctx.reply("You don't have any synced conversations.") return synced_count = len(user_conversations[user_id]) await ctx.reply(f"You have {synced_count} synced conversations that can be accessed from the Flutter app.")
4. Network Configuration
- Make sure your Discord bot's API is accessible from the internet
- You can use a service like ngrok for testing:
ngrok http 8000
- Use the ngrok URL as your
YOUR_BOT_API_URL
in the Flutter app
Usage
- In the Flutter app, go to Settings > Discord Integration
- Click "Login with Discord" to authenticate
- Use the "Sync Conversations" button to sync conversations
- Use the "Import from Discord" button to import conversations from Discord
Troubleshooting
- Authentication Issues: Make sure the Client ID is correct and the redirect URL is properly configured
- Sync Issues: Check that the bot API URL is accessible and the API is running
- Import/Export Issues: Verify that the Discord bot has saved conversations to sync
Security Considerations
- The integration uses Discord OAuth for authentication, ensuring only authorized users can access their conversations
- All API requests require a valid Discord token
- The API verifies the token with Discord for each request
- Consider adding rate limiting and additional security measures for production use