aboutsummaryrefslogtreecommitdiffstats
path: root/src/events/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/events/mod.rs29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/events/mod.rs b/src/events/mod.rs
index d166993..82b6f19 100644
--- a/src/events/mod.rs
+++ b/src/events/mod.rs
@@ -130,8 +130,7 @@ pub async fn handle_voice_state_update(
130 let mut tts_channel: Option<serenity::ChannelId> = None; 130 let mut tts_channel: Option<serenity::ChannelId> = None;
131 131
132 // ── JOIN ── 132 // ── JOIN ──
133 if old_channel.is_none() && new_channel.is_some() { 133 if old_channel.is_none() && let Some(cid) = new_channel {
134 let cid = new_channel.unwrap();
135 let is_afk = new.self_mute || new.self_deaf; 134 let is_afk = new.self_mute || new.self_deaf;
136 135
137 va.join_time = now; 136 va.join_time = now;
@@ -332,11 +331,18 @@ pub async fn handle_message(
332 let has_keyword = content_lower.contains(&data.config.assistant_keyword); 331 let has_keyword = content_lower.contains(&data.config.assistant_keyword);
333 332
334 if is_mention || has_keyword { 333 if is_mention || has_keyword {
334 // Spawn the LLM call in a background task so it doesn't block the
335 // event loop. Discord interactions must be answered within 3 s; a
336 // 5–10 s OpenRouter call would make every subsequent interaction
337 // expire with "Unknown interaction".
338 let http = ctx.http.clone();
339 let channel_id = new_message.channel_id;
340 let _reply_to_id = new_message.id;
341 let bot_id = ctx.cache.current_user().id;
335 let (api_url, api_key, model) = data.config.resolve_llm(); 342 let (api_url, api_key, model) = data.config.resolve_llm();
336 let client = crate::openrouter::OpenRouterClient::new(api_url, api_key, model); 343 let assistant_prompt = lang::ASSISTANT_SYSTEM_PROMPT.to_string();
337 let assistant_prompt = lang::ASSISTANT_SYSTEM_PROMPT;
338 344
339 let bot_id = ctx.cache.current_user().id; 345 // Build message history synchronously (fast path).
340 let mut messages = Vec::new(); 346 let mut messages = Vec::new();
341 let mut current_msg = Some(new_message.clone()); 347 let mut current_msg = Some(new_message.clone());
342 for _ in 0..5 { 348 for _ in 0..5 {
@@ -346,9 +352,9 @@ pub async fn handle_message(
346 } else { 352 } else {
347 "user" 353 "user"
348 }; 354 };
349 messages.push((role.into(), msg.content.clone())); 355 messages.push((role.to_string(), msg.content.clone()));
350 current_msg = match msg.message_reference.and_then(|r| r.message_id) { 356 current_msg = match msg.message_reference.and_then(|r| r.message_id) {
351 Some(ref_id) => msg.channel_id.message(ctx, ref_id).await.ok(), 357 Some(ref_id) => channel_id.message(&http, ref_id).await.ok(),
352 None => None, 358 None => None,
353 }; 359 };
354 } else { 360 } else {
@@ -357,9 +363,12 @@ pub async fn handle_message(
357 } 363 }
358 messages.reverse(); 364 messages.reverse();
359 365
360 if let Ok(response) = client.chat_with_history(assistant_prompt, messages).await { 366 tokio::spawn(async move {
361 new_message.reply(ctx, response).await.ok(); 367 let client = crate::openrouter::OpenRouterClient::new(api_url, api_key, model);
362 } 368 if let Ok(response) = client.chat_with_history(&assistant_prompt, messages).await {
369 let _ = channel_id.say(&http, response).await;
370 }
371 });
363 } 372 }
364 373
365 Ok(()) 374 Ok(())