aboutsummaryrefslogtreecommitdiffstats
path: root/src/commands/music.rs
blob: 245eb886d6fa0d4683332ea6b6dee1879d68531d (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
use super::utilities::{FileType, extract_urls, file_type};
use poise::serenity_prelude as serenity;

use tracing::{error, info};

use super::autocomplete::audio_file;
use crate::Data;
use crate::copyparty::{html_decode, percent_decode};
use crate::error::{Error, Result};
use crate::lang;
use crate::music::{EnqueueOutcome, QueuePosition, SourceKind, TrackRequest, classify_url};

type Context<'a> = poise::Context<'a, Data, Error>;

// ── Source classification ──────────────────────────────────────────

/// Music playback and queue management.
#[poise::command(slash_command, subcommands("grajcuj", "skip", "queue", "stop", "join"))]
pub async fn music(_ctx: Context<'_>) -> Result<()> {
    Ok(())
}

/// Play music from Copyparty, URL, or file upload
#[poise::command(slash_command)]
pub async fn grajcuj(
    ctx: Context<'_>,
    #[description = "Plik do odtworzenia"]
    #[autocomplete = "audio_file"]
    melodia: Option<String>,
    #[description = "Bezpośredni plik z twojego systemu"] file: Option<serenity::Attachment>,
    #[description = "Chujowa kompresja!?"] compressed: Option<bool>,
    #[description = "Gdzie dodać utwór w kolejce (domyślnie: last)"] position: Option<
        QueuePosition,
    >,
) -> Result<()> {
    play_command(ctx, melodia, file, compressed, position).await
}

async fn play_command(
    ctx: Context<'_>,
    melodia: Option<String>,
    file: Option<serenity::Attachment>,
    compressed: Option<bool>,
    position: Option<QueuePosition>,
) -> Result<()> {
    let compressed = compressed.unwrap_or(false);
    let position = position.unwrap_or(QueuePosition::Last);
    eprintln!(
        "[DEBUG] play() reached, melodia={:?}, file={:?}, compressed={compressed}, position={position:?}",
        melodia,
        file.as_ref().map(|f| &f.filename)
    );
    let result = play_impl(ctx, melodia, file, compressed, position).await;
    if let Err(e) = &result {
        eprintln!("[DEBUG] play() error: {e}");
        error!("play command failed: {e}");
    }
    result
}

struct ResolvedSource {
    url: String,
    display_name: String,
    kind: SourceKind,
}

fn clean_name(raw: &str) -> String {
    html_decode(&percent_decode(raw))
}

// ── URL classification helpers ──────────────────────────────────────

async fn resolve_source(
    ctx: Context<'_>,
    melodia: Option<String>,
    file: Option<serenity::Attachment>,
) -> Result<ResolvedSource> {
    // ── File attachment ──────────────────────────────────────────
    // Discord attachment URLs are direct media file URLs — ffmpeg streams
    // them without a full download.
    if let Some(file) = file {
        return Ok(ResolvedSource {
            url: file.url.clone(),
            display_name: file.filename.clone(),
            kind: SourceKind::Stream,
        });
    }

    let name = melodia.ok_or_else(|| Error::Custom(lang::L_MUSIC_NEED_NAME.into()))?;

    // ── Autocomplete cache (per-user: (UserId, filename) → SearchHit) ─
    if let Some(hit) = ctx.data().ac_lookup(ctx.author().id, &name).await {
        let path = hit.url_path().unwrap_or("");
        let path = if path.starts_with('/') {
            path.to_string()
        } else {
            format!("/{path}")
        };
        let url = ctx.data().copyparty.build_url(&path);
        let display_name = clean_name(hit.filename().unwrap_or(&name));
        return Ok(ResolvedSource {
            url,
            display_name,
            kind: SourceKind::Stream,
        });
    }

    // ── Direct Copyparty path (starts with '/') ──────────────────
    if name.starts_with('/') {
        let url = ctx.data().copyparty.build_url(&name);
        let display_name = clean_name(name.rsplit('/').next().unwrap_or(&name));
        return Ok(ResolvedSource {
            url,
            display_name,
            kind: SourceKind::Stream,
        });
    }

    // ── Direct HTTP(S) URL ───────────────────────────────────────
    if name.starts_with("http://") || name.starts_with("https://") {
        let url = name.clone();
        let display_name = clean_name(url.rsplit('/').next().unwrap_or(&url));

        // yt-dlp platforms → download; everything else (radio, HLS,
        // playlists, Copyparty, direct media) → ffmpeg stream.
        let kind = classify_url(&url);
        return Ok(ResolvedSource {
            url,
            display_name,
            kind,
        });
    }

    // ── Fallback: search Copyparty ───────────────────────────────
    info!(query = %name, "searching copyparty for audio");
    let results = ctx
        .data()
        .copyparty
        .search(
            &name,
            5,
            &[
                "mp3", "wav", "ogg", "flac", "m4a", "opus", "aac", "wma", "oga", "mp4", "webm",
                "mkv", "avi", "mov", "flv", "wmv", "m4v", "mpg", "mpeg", "3gp", "ts",
            ],
        )
        .await
        .map_err(|e| {
            error!(query = %name, error = %e, "copyparty search failed");
            Error::Custom(lang::fmt_music_search_failed(&e))
        })?;
    let hit = results
        .first()
        .ok_or_else(|| Error::Custom(lang::L_MUSIC_NO_AUDIO_ON_CP.into()))?;
    let path = hit
        .url_path()
        .ok_or_else(|| Error::Custom(lang::L_MUSIC_NO_URL_IN_RESULT.into()))?;
    let path = if path.starts_with('/') {
        path.to_string()
    } else {
        format!("/{path}")
    };
    let url = ctx.data().copyparty.build_url(&path);
    let display_name = clean_name(hit.filename().unwrap_or("unknown"));
    Ok(ResolvedSource {
        url,
        display_name,
        kind: SourceKind::Stream,
    })
}

// ── Playlist resolution is handled by ffmpeg natively ──────────────
// ffmpeg reads `.m3u`/`.m3u8`/`.pls` playlists directly (HLS segment
// fetching, PLS/M3U entry parsing) — no manual resolution needed.
// This mirrors the Python bot's `FFmpegPCMAudio(source, ...)` approach.

async fn play_impl(
    ctx: Context<'_>,
    melodia: Option<String>,
    file: Option<serenity::Attachment>,
    compressed: bool,
    position: QueuePosition,
) -> Result<()> {
    ctx.defer().await?;

    let guild_id = ctx.guild_id().ok_or_else(lang::err_not_in_guild)?;

    let channel_id = ctx
        .serenity_context()
        .cache
        .guild(guild_id)
        .and_then(|g| {
            g.voice_states
                .get(&ctx.author().id)
                .and_then(|vs| vs.channel_id)
        })
        .ok_or_else(lang::err_not_in_voice)?;

    let file_reupload = file.as_ref().map(|f| (f.url.clone(), f.filename.clone()));
    let src = resolve_source(ctx, melodia, file).await?;

    let request = TrackRequest {
        url: src.url,
        display_name: src.display_name.clone(),
        kind: src.kind,
        compressed,
        reupload: file_reupload.clone(),
        requester: Some(ctx.author().id),
        silent: false,
    };

    let outcome = ctx
        .data()
        .music
        .enqueue(
            guild_id.get(),
            channel_id,
            Some(ctx.channel_id()),
            request,
            position,
        )
        .await?;

    match outcome {
        EnqueueOutcome::Started => {
            send_music_msg(ctx, &src.display_name, file_reupload).await?;
        }
        EnqueueOutcome::Queued { position } => {
            ctx.say(lang::fmt_music_queued(&src.display_name, position))
                .await?;
        }
    }
    Ok(())
}

/// Skip the current track and play the next queued one
#[poise::command(slash_command)]
pub async fn skip(ctx: Context<'_>) -> Result<()> {
    ctx.defer().await?;
    let guild_id = ctx.guild_id().ok_or_else(lang::err_not_in_guild)?;
    let played = ctx.data().music.skip(guild_id.get()).await?;
    let content = if played {
        lang::L_MUSIC_SKIPPED
    } else {
        lang::L_MUSIC_QUEUE_EMPTY
    };
    ctx.send(
        poise::CreateReply::default()
            .content(content)
            .ephemeral(true),
    )
    .await?;
    Ok(())
}

/// Show the current track and the pending queue
#[poise::command(slash_command)]
pub async fn queue(ctx: Context<'_>) -> Result<()> {
    let guild_id = ctx.guild_id().ok_or_else(lang::err_not_in_guild)?;
    let snapshot = ctx.data().music.snapshot(guild_id.get()).await;

    if snapshot.current.is_none() && snapshot.upcoming.is_empty() {
        ctx.say(lang::L_MUSIC_QUEUE_EMPTY).await?;
        return Ok(());
    }

    let who = |requester: Option<serenity::UserId>| {
        requester
            .map(|id| format!(" <@{}>", id.get()))
            .unwrap_or_default()
    };

    let mut lines = Vec::with_capacity(snapshot.upcoming.len() + 2);
    lines.push(lang::L_MUSIC_QUEUE_HEADER.to_string());
    if let Some(current) = &snapshot.current {
        lines.push(lang::fmt_music_queue_current(
            &current.display_name,
            &who(current.requester),
        ));
    }
    for (index, item) in snapshot.upcoming.iter().enumerate() {
        lines.push(lang::fmt_music_queue_item(
            index + 1,
            &item.display_name,
            &who(item.requester),
        ));
    }

    ctx.say(lines.join("\n")).await?;
    Ok(())
}

/// Stop playback and disconnect from voice
#[poise::command(slash_command)]
pub async fn stop(ctx: Context<'_>) -> Result<()> {
    let guild_id = ctx.guild_id().ok_or_else(lang::err_not_in_guild)?;
    ctx.data().music.stop(guild_id.get()).await;
    ctx.data().voice_manager.disconnect(guild_id).await?;
    ctx.send(
        poise::CreateReply::default()
            .content(lang::L_MUSIC_STOPPED)
            .ephemeral(true),
    )
    .await?;
    Ok(())
}

/// Join your voice channel
#[poise::command(slash_command)]
pub async fn join(ctx: Context<'_>) -> Result<()> {
    ctx.defer().await?;
    let guild_id = ctx.guild_id().ok_or_else(lang::err_not_in_guild)?;

    let channel_id = ctx.serenity_context().cache.guild(guild_id).and_then(|g| {
        g.voice_states
            .get(&ctx.author().id)
            .and_then(|vs| vs.channel_id)
    });

    match channel_id {
        Some(cid) => match ctx.data().voice_manager.connect(guild_id, cid).await {
            Ok(_) => {
                ctx.say(lang::fmt_voice_joined(&cid)).await?;
            }
            Err(e) => {
                ctx.say(lang::fmt_voice_join_failed(&e)).await?;
            }
        },
        None => {
            ctx.say(lang::L_VOICE_USER_NOT_IN_VC).await?;
        }
    }
    Ok(())
}

/// Send the "playing" message, optionally reuploading a user-provided file
/// so other channel members can download it.
async fn send_music_msg(
    ctx: Context<'_>,
    display_name: &str,
    file_reupload: Option<(String, String)>,
) -> Result<()> {
    if let Some((url, filename)) = file_reupload {
        match ctx.data().http_client.get(&url).send().await {
            Ok(resp) => match resp.bytes().await {
                Ok(bytes) => {
                    if ctx
                        .send(
                            poise::CreateReply::default()
                                .content(lang::fmt_music_playing(display_name))
                                .attachment(serenity::CreateAttachment::bytes(
                                    bytes.to_vec(),
                                    filename.clone(),
                                )),
                        )
                        .await
                        .is_ok()
                    {
                        return Ok(());
                    }
                    error!("send_music_msg: ctx.send with attachment failed, falling back");
                }
                Err(e) => error!("send_music_msg: failed to read file bytes: {e}"),
            },
            Err(e) => error!("send_music_msg: failed to download file: {e}"),
        }
    }
    ctx.say(lang::fmt_music_playing(display_name)).await?;
    Ok(())
}

/// Right-click a message → play the first audio/video file, or first URL.
#[poise::command(context_menu_command = "Odtwórz")]
pub async fn message_grajcuj(ctx: Context<'_>, message: serenity::Message) -> Result<()> {
    // Files > links: prefer audio/video attachments first.
    if let Some(att) = message
        .attachments
        .iter()
        .find(|a| matches!(file_type(&a.filename), FileType::Audio | FileType::Video))
    {
        return play_impl(ctx, None, Some(att.clone()), false, QueuePosition::Last).await;
    }
    // Fallback: first URL in message content.
    if let Some(url) = extract_urls(&message.content).into_iter().next() {
        return play_impl(ctx, Some(url), None, false, QueuePosition::Last).await;
    }
    ctx.say(lang::L_MUSIC_NO_AUDIO_ATTACHMENTS).await?;
    Ok(())
}

pub fn commands() -> Vec<poise::Command<Data, Error>> {
    vec![music(), message_grajcuj()]
}