Add 'Detect and Join Conversation' button to Web UI and CLI

- Added /autonomous/join-conversation API endpoint in api.py
- Added 'Detect and Join Conversation' button to Web UI under Autonomous Actions
- Added 'autonomous join-conversation' command to CLI tool (miku-cli.py)
- Updated miku_detect_and_join_conversation_for_server to support force=True parameter
- When force=True: skips time limit, activity checks, and random chance
- Force mode uses last 10 user messages regardless of age
- Manual triggers via Web UI/CLI now work even with old messages
This commit is contained in:
2025-12-10 14:57:59 +02:00
parent 76aaf6c437
commit 65e6c3e7ea
4 changed files with 82 additions and 18 deletions

View File

@@ -209,8 +209,14 @@ async def miku_engage_random_user_for_server(guild_id: int):
except Exception as e:
print(f"⚠️ Failed to engage user: {e}")
async def miku_detect_and_join_conversation_for_server(guild_id: int):
"""Miku detects and joins conversations in a specific server"""
async def miku_detect_and_join_conversation_for_server(guild_id: int, force: bool = False):
"""Miku detects and joins conversations in a specific server
Args:
guild_id: The server ID
force: If True, bypass activity checks and random chance (for manual triggers)
"""
print(f"🔍 [Join Conv] Called for server {guild_id} (force={force})")
server_config = server_manager.get_server_config(guild_id)
if not server_config:
print(f"⚠️ No config found for server {guild_id}")
@@ -224,25 +230,41 @@ async def miku_detect_and_join_conversation_for_server(guild_id: int):
# Fetch last 20 messages (for filtering)
try:
messages = [msg async for msg in channel.history(limit=20)]
print(f"📜 [Join Conv] Fetched {len(messages)} messages from history")
except Exception as e:
print(f"⚠️ Failed to fetch channel history for server {guild_id}: {e}")
return
# Filter to messages in last 10 minutes from real users (not bots)
recent_msgs = [
msg for msg in messages
if not msg.author.bot
and (datetime.now(msg.created_at.tzinfo) - msg.created_at).total_seconds() < 600
]
# Filter messages based on force mode
if force:
# When forced, use messages from real users (no time limit) - but limit to last 10
recent_msgs = [msg for msg in messages if not msg.author.bot][:10]
print(f"📊 [Join Conv] Force mode: Using last {len(recent_msgs)} messages from users (no time limit)")
else:
# Normal mode: Filter to messages in last 10 minutes from real users (not bots)
recent_msgs = [
msg for msg in messages
if not msg.author.bot
and (datetime.now(msg.created_at.tzinfo) - msg.created_at).total_seconds() < 600
]
print(f"📊 [Join Conv] Found {len(recent_msgs)} recent messages from users (last 10 min)")
user_ids = set(msg.author.id for msg in recent_msgs)
if len(recent_msgs) < 5 or len(user_ids) < 2:
# Not enough activity
return
if not force:
if len(recent_msgs) < 5 or len(user_ids) < 2:
# Not enough activity
print(f"⚠️ [Join Conv] Not enough activity: {len(recent_msgs)} messages, {len(user_ids)} users (need 5+ messages, 2+ users)")
return
if random.random() > 0.5:
return # 50% chance to engage
if random.random() > 0.5:
print(f"🎲 [Join Conv] Random chance failed (50% chance)")
return # 50% chance to engage
else:
print(f"✅ [Join Conv] Force mode - bypassing activity checks")
if len(recent_msgs) < 1:
print(f"⚠️ [Join Conv] No messages found in channel history")
return
# Use last 10 messages for context (oldest to newest)
convo_lines = reversed(recent_msgs[:10])
@@ -374,10 +396,14 @@ async def miku_engage_random_user():
for guild_id in server_manager.servers:
await miku_engage_random_user_for_server(guild_id)
async def miku_detect_and_join_conversation():
"""Legacy function - now runs for all servers"""
async def miku_detect_and_join_conversation(force: bool = False):
"""Legacy function - now runs for all servers
Args:
force: If True, bypass activity checks and random chance (for manual triggers)
"""
for guild_id in server_manager.servers:
await miku_detect_and_join_conversation_for_server(guild_id)
await miku_detect_and_join_conversation_for_server(guild_id, force=force)
async def share_miku_tweet():
"""Legacy function - now runs for all servers"""