aboutsummaryrefslogtreecommitdiffstats
path: root/src/events/mod.rs
blob: fccb5ac03aebf8932fcafb0ed2ca92b8a30adcbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use chrono::{Local, Utc};
use poise::serenity_prelude as serenity;
use rand::seq::IndexedRandom;
use tracing::{info, warn};

use crate::Data;
use crate::database::{self, VoiceActivity};
use crate::error::Result;
use crate::lang;

pub async fn handle_ready(_ctx: &serenity::Context) -> Result<()> {
    info!("{}", lang::L_EV_READY);
    Ok(())
}

fn format_duration(seconds: f64) -> String {
    let total = seconds as u64;
    let hours = total / 3600;
    let minutes = (total % 3600) / 60;
    let secs = total % 60;
    if hours > 0 {
        format!("{hours}h {minutes}m {secs}s")
    } else if minutes > 0 {
        format!("{minutes}m {secs}s")
    } else {
        format!("{secs}s")
    }
}

async fn play_tts_in_channel(
    tts: std::sync::Arc<crate::tts::TtsState>,
    voice_manager: crate::voice::VoiceManager,
    guild_id: serenity::GuildId,
    channel_id: serenity::ChannelId,
    text: String,
) {
    // Never join or move the bot from a voice-state event: that would let any
    // user drag the bot (and its music/recording) into an arbitrary channel.
    // Only speak if the bot is already in the channel where the event happened.
    let Some(call) = voice_manager.get_call(guild_id) else {
        return;
    };
    let already_there = call
        .lock()
        .await
        .current_channel()
        .map(|current| current.0.get() == channel_id.get())
        .unwrap_or(false);
    if !already_there {
        return;
    }

    let audio_data = match tts.synthesize(&text).await {
        Ok(bytes) => bytes,
        Err(e) => {
            warn!("Failed to synthesize TTS: {e}");
            return;
        }
    };

    let input = crate::tts::pcm_bytes_to_input(audio_data);

    let mut call_lock = call.lock().await;
    if call_lock.current_channel().is_some() {
        call_lock.play_input(input);
    }
}

pub async fn handle_voice_state_update(
    ctx: &serenity::Context,
    data: &Data,
    old: Option<serenity::VoiceState>,
    new: &serenity::VoiceState,
) -> Result<()> {
    let log_channel = data.config.status_channel_id.map(serenity::ChannelId::new);
    let current_ts = Utc::now().timestamp();
    let current_time = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();

    let user_id = new.user_id;
    let guild_id = new.guild_id;

    // The bot's own voice state drives the recording lifecycle (catches admin
    // moves, kicks, and gateway drops that VoiceManager::connect/disconnect
    // can't see). Must run before the generic bot-skip below.
    if user_id == ctx.cache.current_user().id {
        crate::recording::on_bot_voice_state(
            &data.voice_manager,
            guild_id,
            old.as_ref().and_then(|v| v.channel_id),
            new.channel_id,
        )
        .await;
        return Ok(());
    }

    // skip bots (Python behavior)
    let member = guild_id
        .and_then(|gid| ctx.cache.guild(gid))
        .and_then(|g| g.members.get(&user_id).cloned());
    if let Some(ref m) = member
        && m.user.bot
    {
        return Ok(());
    }

    let member_name = member
        .as_ref()
        .map(|m| m.user.name.clone())
        .unwrap_or_default();
    let display_name = member
        .as_ref()
        .map(|m| m.display_name().to_string())
        .unwrap_or_default();

    // Pre-generate all 6 event phrases for this user (background, non-blocking).
    // First event per user pays ~120ms; subsequent events are instant from cache.
    {
        let tts = data.tts.clone();
        let name = display_name.clone();
        tokio::spawn(async move {
            tts.pre_generate(&name).await;
        });
    }

    let now = current_ts as f64;

    let user_id_str = user_id.to_string();
    let mut va = database::get_voice_activity(&data.db_pool, &user_id_str)
        .await?
        .unwrap_or(VoiceActivity {
            user_id: user_id_str.clone(),
            join_time: 0.0,
            total_active: 0.0,
            total_afk: 0.0,
            is_afk: 0,
            afk_start: 0.0,
            active_start: 0.0,
            channel_name: String::new(),
            last_updated: None,
        });

    let old_channel = old.as_ref().and_then(|v| v.channel_id);
    let new_channel = new.channel_id;

    let mut tts_text: Option<String> = None;
    let mut tts_channel: Option<serenity::ChannelId> = None;

    // ── JOIN ──
    if old_channel.is_none()
        && let Some(cid) = new_channel
    {
        let is_afk = new.self_mute || new.self_deaf;

        va.join_time = now;
        va.total_active = 0.0;
        va.total_afk = 0.0;
        va.is_afk = if is_afk { 1 } else { 0 };
        va.afk_start = if is_afk { now } else { 0.0 };
        va.active_start = if !is_afk { now } else { 0.0 };
        va.channel_name = guild_id
            .and_then(|gid| ctx.cache.guild(gid))
            .map(|g| {
                g.channels
                    .get(&cid)
                    .map(|c| c.name.clone())
                    .unwrap_or_default()
            })
            .unwrap_or_default();
        database::upsert_voice_activity(&data.db_pool, &va).await?;
        info!("User {user_id} joined voice");

        if let Some(lc) = log_channel {
            let msg = lang::fmt_log_ev_joined(
                &current_time,
                &member_name,
                current_ts,
                &va.channel_name,
            );
            lc.say(ctx, &msg).await.ok();
        }
        tts_text = Some(lang::fmt_ev_joined(&display_name));
        tts_channel = Some(cid);
    }
    // ── LEAVE ──
    else if old_channel.is_some() && new_channel.is_none() {
        let ocid = old_channel.unwrap();
        let channel_name = guild_id
            .and_then(|gid| ctx.cache.guild(gid))
            .and_then(|g| g.channels.get(&ocid).map(|c| c.name.clone()))
            .unwrap_or_default();

        if va.join_time > 0.0 {
            if va.is_afk == 1 {
                va.total_afk += now - va.afk_start;
            } else {
                va.total_active += (now - va.active_start).max(0.0);
            }

            let active_str = format_duration(va.total_active);
            let afk_str = format_duration(va.total_afk);

            // Accumulate to user_stats if in tracked guild
            if let Some(status_channel_id) = data.config.status_channel_id
                && let Some(gid) = guild_id {
                    let is_tracked = ctx.cache.guild(gid)
                        .map(|g| g.channels.contains_key(&serenity::ChannelId::new(status_channel_id)))
                        .unwrap_or(false);
                    if is_tracked {
                        database::add_voice_time(
                            &data.db_pool,
                            &user_id_str,
                            va.total_active,
                            va.total_afk,
                        ).await?;
                    }
                }

            if let Some(lc) = log_channel {
                let msg = lang::fmt_log_ev_left(
                    &current_time,
                    &member_name,
                    current_ts,
                    &channel_name,
                    &active_str,
                    &afk_str,
                );
                lc.say(ctx, &msg).await.ok();
            }
            info!("User {user_id} left voice");

            database::upsert_voice_activity(&data.db_pool, &va).await?;
        } else {
            if let Some(lc) = log_channel {
                let msg = lang::fmt_log_ev_left_no_data(
                    &current_time,
                    &member_name,
                    current_ts,
                    &channel_name,
                );
                lc.say(ctx, &msg).await.ok();
            }
            info!("User {user_id} left voice (no data)");
        }

        tts_text = Some(lang::fmt_ev_left(&display_name));
        tts_channel = Some(ocid);
    }
    // ── CHANNEL MOVE ──
    else if let (Some(old_cid), Some(new_cid)) = (old_channel, new_channel)
        && old_cid != new_cid
    {
        va.channel_name = guild_id
            .and_then(|gid| ctx.cache.guild(gid))
            .and_then(|g| g.channels.get(&new_cid).map(|c| c.name.clone()))
            .unwrap_or_default();
        if va.join_time > 0.0 {
            database::upsert_voice_activity(&data.db_pool, &va).await?;
        }
        info!(
            "User {user_id} moved voice channel: {old_cid} -> {new_cid} ({})",
            va.channel_name
        );
    }
    // ── AFK STATE CHANGE (only for tracked users already in channel) ──
    else if new_channel.is_some() && va.join_time > 0.0 {
        let is_afk = new.self_mute || new.self_deaf;
        let was_afk = va.is_afk == 1;

        if was_afk && !is_afk {
            // Return from AFK
            let afk_duration = now - va.afk_start;
            va.total_afk += afk_duration;
            va.is_afk = 0;
            va.afk_start = 0.0;
            va.active_start = now;
            database::upsert_voice_activity(&data.db_pool, &va).await?;

            let afk_str = format_duration(afk_duration);

            if let Some(lc) = log_channel {
                let msg = lang::fmt_log_ev_activity(
                    &current_time,
                    &member_name,
                    current_ts,
                    &va.channel_name,
                    &afk_str,
                );
                lc.say(ctx, &msg).await.ok();
            }
            info!("User {user_id} returned from AFK");
        } else if !was_afk && is_afk {
            // Went AFK
            let active_duration = if va.active_start > 0.0 {
                (now - va.active_start).max(0.0)
            } else {
                0.0
            };
            va.total_active += active_duration;
            va.is_afk = 1;
            va.afk_start = now;
            va.active_start = 0.0;
            database::upsert_voice_activity(&data.db_pool, &va).await?;

            let active_str = format_duration(active_duration);
            let activity = if new.self_mute {
                lang::L_EV_WORD_MUTED
            } else {
                lang::L_EV_WORD_DEAFENED
            };

            if let Some(lc) = log_channel {
                let msg = lang::fmt_log_ev_unactivity(
                    &current_time,
                    &member_name,
                    current_ts,
                    &va.channel_name,
                    activity,
                    &active_str,
                );
                lc.say(ctx, &msg).await.ok();
            }
            info!("User {user_id} went AFK");
        }
    }

    // ── TTS for mute/deafen (deaf takes priority with early return) ──
    if old.as_ref().is_some_and(|v| v.self_deaf) != new.self_deaf {
        let phrase = if new.self_deaf {
            lang::fmt_ev_deafened(&display_name)
        } else {
            lang::fmt_ev_undeafened(&display_name)
        };
        if let Some(gid) = guild_id
            && let Some(cid) = tts_channel.or(new_channel)
        {
            tokio::spawn(play_tts_in_channel(
                data.tts.clone(),
                data.voice_manager.clone(),
                gid,
                cid,
                phrase,
            ));
        }
        return Ok(());
    }

    if old.as_ref().is_some_and(|v| v.self_mute) != new.self_mute {
        let phrase = if new.self_mute {
            lang::fmt_ev_muted(&display_name)
        } else {
            lang::fmt_ev_unmuted(&display_name)
        };
        if let Some(gid) = guild_id
            && let Some(cid) = tts_channel.or(new_channel)
        {
            tokio::spawn(play_tts_in_channel(
                data.tts.clone(),
                data.voice_manager.clone(),
                gid,
                cid,
                phrase,
            ));
        }
        return Ok(());
    }

    // ── Play join/leave TTS if no mute/deafen change ──
    if let (Some(text), Some(cid)) = (tts_text, tts_channel)
        && let Some(gid) = guild_id
    {
        tokio::spawn(play_tts_in_channel(
            data.tts.clone(),
            data.voice_manager.clone(),
            gid,
            cid,
            text,
        ));
    }

    Ok(())
}

pub async fn handle_message(
    ctx: &serenity::Context,
    data: &Data,
    new_message: &serenity::Message,
) -> Result<()> {
    // Ignore own messages and other bots
    if new_message.author.id == ctx.cache.current_user().id || new_message.author.bot {
        return Ok(());
    }

    // Track message count in the tracked guild
    if let Some(status_channel_id) = data.config.status_channel_id
        && let Some(guild_id) = new_message.guild_id {
            // Check if the status channel is in the same guild as the message
            let is_tracked_guild = ctx.cache.guild(guild_id)
                .map(|g| g.channels.contains_key(&serenity::ChannelId::new(status_channel_id)))
                .unwrap_or(false);
            if is_tracked_guild {
                database::increment_message_count(&data.db_pool, &new_message.author.id.to_string())
                    .await?;
            }
        }

    let content_lower = new_message.content.to_lowercase();
    let message_guild_id = new_message.guild_id.map(|guild| guild.get() as i64);
    let response_to_send = {
        let triggers = data.triggers.lock().await;
        let mut chosen = None;
        for (trigger, keywords, responses) in triggers.iter() {
            // Triggers belong to one guild and never fire in DMs. Rows with
            // guild_id = 0 predate scoping and keep their old global behaviour.
            if trigger.guild_id != 0 && message_guild_id != Some(trigger.guild_id) {
                continue;
            }
            if keywords.iter().any(|k| content_lower.contains(k)) {
                chosen = responses.choose(&mut rand::rng()).cloned();
                break;
            }
        }
        chosen
    };
    if let Some(response) = response_to_send {
        new_message.channel_id.say(ctx, response).await.ok();
    }

    let is_mention = new_message.mentions_user_id(ctx.cache.current_user().id);
    let has_keyword = content_lower.contains(&data.config.assistant_keyword);

    if is_mention || has_keyword {
        // Spawn the LLM call in a background task so it doesn't block the
        // event loop. Discord interactions must be answered within 3 s; a
        // 5–10 s OpenRouter call would make every subsequent interaction
        // expire with "Unknown interaction".
        let http = ctx.http.clone();
        let channel_id = new_message.channel_id;
        let reply_to_id = new_message.id;
        let bot_id = ctx.cache.current_user().id;
        let (api_url, api_key, model) = data.config.resolve_llm();
        let assistant_prompt = lang::ASSISTANT_SYSTEM_PROMPT.to_string();

        // Build message history synchronously (fast path).
        let mut messages = Vec::new();
        let mut current_msg = Some(new_message.clone());
        for _ in 0..5 {
            if let Some(msg) = current_msg {
                let role = if msg.author.id == bot_id {
                    "assistant"
                } else {
                    "user"
                };
                messages.push((role.to_string(), msg.content.clone()));
                current_msg = match msg.message_reference.and_then(|r| r.message_id) {
                    Some(ref_id) => channel_id.message(&http, ref_id).await.ok(),
                    None => None,
                };
            } else {
                break;
            }
        }
        messages.reverse();

        tokio::spawn(async move {
            let client = crate::openrouter::OpenRouterClient::new(api_url, api_key, model);
            if let Ok(response) = client.chat_with_history(&assistant_prompt, messages).await {
                let _ = channel_id.send_message(&http, poise::serenity_prelude::CreateMessage::new().content(response).reference_message((channel_id, reply_to_id))).await;
            }
        });
    }

    Ok(())
}