aboutsummaryrefslogtreecommitdiff
path: root/src/commands/playback.rs
blob: 7ecef4766664d797c37831732e5df758809f724e (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
use log::{
    debug,
    error,
    info,
    warn,
};
use serenity::{
    framework::standard::{
        macros::{
            command,
            group,
        },
        Args,
        CommandError,
        CommandResult,
    },
    model::channel::Message,
    prelude::*,
};
use songbird::{
    input::YoutubeDl,
    Call,
    Songbird,
};
use std::sync::Arc;
use tap::Conv;

use crate::{
    bot::HttpKey,
    commands::sound_levels::*,
    util,
    CONFIG,
};

#[group]
#[commands(skip, pause, resume, list, die, mute, unmute, play)]
#[only_in(guild)]
struct Playback;

pub async fn songbird(
    ctx: &Context,
    msg: &Message,
) -> Result<(Arc<Songbird>, Arc<Mutex<Call>>), CommandError> {
    let Some(gid) = msg.guild_id else {
        return Err(anyhow::anyhow!("no guild id").into());
    };

    let sb = songbird::get(ctx).await.expect("acquiring songbird handle");
    let call = sb.get_or_insert(gid);

    Ok((sb, call))
}

pub async fn _play(ctx: &Context, msg: &Message, url: &str) -> CommandResult {
    use url::{
        Host,
        Url,
    };

    debug!("playing '{}'", url);
    if !url.starts_with("http") {
        warn!("got bad url argument to play: {}", url);
        util::send(ctx, msg.channel_id, "bAD LiNk", msg.tts).await?;
        return Ok(());
    }

    let url = match Url::parse(url) {
        Err(e) => {
            error!("bad url: {}", e);
            util::send(ctx, msg.channel_id, "INVALID URL", msg.tts).await?;
            return Ok(());
        },
        Ok(u) => u,
    };

    let host = url.host().and_then(|u| match u {
        Host::Domain(h) => Some(h.to_owned()),
        _ => None,
    });

    if host.map(|h| h.to_lowercase().contains("imgur")).unwrap_or(false) {
        info!("detected imgur link");

        if msg.author.id.get() == 106160362109272064 {
            util::send(ctx, msg.channel_id, "fuck you conway", true).await?;
        } else {
            util::send(ctx, msg.channel_id, "IMGUR IS BAD, YOU TRASH CAN MAN", msg.tts).await?;
        }

        return Ok(());
    }

    let (_sb, call) = songbird(ctx, msg).await?;
    let mut call = call.lock().await;

    if call.current_channel().is_none() {
        call.join(CONFIG.discord.voice_channel()).await?;
    }

    let client = {
        let data = ctx.data.read().await;
        data.get::<HttpKey>().unwrap().clone()
    };

    let input = YoutubeDl::new_ytdl_like("yt-dlp", client.clone(), url.conv::<String>());
    call.enqueue_input(input.into()).await;

    Ok(())
}

#[command]
pub async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
    if args.len() == 0 {
        return _resume(ctx, msg).await;
    }

    let url = match args.single::<String>() {
        Ok(url) => url,
        Err(e) => {
            error!("unable to parse url from args: {}", e);
            return util::send(ctx, msg.channel_id, "BAD LINK", msg.tts)
                .await
                .map_err(CommandError::from);
        },
    };

    _play(ctx, msg, &url).await
}

#[command]
pub async fn pause(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
    let (_sb, call) = songbird(ctx, msg).await?;

    let call = call.lock().await;
    call.queue().pause()?;

    Ok(())
}

#[command]
#[aliases("continue")]
pub async fn resume(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
    _resume(ctx, msg).await
}

async fn _resume(ctx: &Context, msg: &Message) -> CommandResult {
    let (_sb, call) = songbird(ctx, msg).await?;

    let call = call.lock().await;
    call.queue().resume()?;

    Ok(())
}

#[command]
#[aliases("next")]
pub async fn skip(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
    let (_sb, call) = songbird(ctx, msg).await?;

    let call = call.lock().await;
    call.queue().skip()?;

    Ok(())
}

#[command]
#[aliases("sudoku", "fuckoff", "stop")]
pub async fn die(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
    let (_sb, call) = songbird(ctx, msg).await?;

    let call = call.lock().await;
    call.queue().stop();

    Ok(())
}

#[command]
#[aliases("queue")]
pub async fn list(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
    let (_sb, call) = songbird(ctx, msg).await?;

    let call = call.lock().await;
    let queue = call.queue();

    util::send(ctx, msg.channel_id, "(command fix work-in-progress)", msg.tts).await?;

    for track in queue.current_queue().into_iter() {
        let info = track.get_info().await?;

        util::send(ctx, msg.channel_id, format!("track playing for {:?}", info.play_time), msg.tts)
            .await?;
    }

    Ok(())
}