aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
blob: d59c4da5777c081842579410abc5c6b669cf9b48 (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
use chrono::Duration;
use serenity::{
    client::Context,
    model::{
        id::{
            ChannelId,
            MessageId,
        },
        permissions::Permissions,
    },
};
use std::process::Stdio;

use lazy_static::lazy_static;
use log::debug;
use regex::{
    Match,
    Regex,
};
use serenity::all::{
    CreateMessage,
    Message,
};
use url::Url;

use crate::{
    commands::songbird,
    Result,
    CONFIG,
};

pub async fn currently_playing(ctx: &Context, msg: &Message) -> bool {
    let (_sb, call) = songbird(ctx, msg).await.expect("no songbird");

    let call = call.lock().await;
    call.queue().current().is_some()
}

pub async fn users_listening(ctx: &Context) -> Result<bool> {
    let channel = CONFIG.discord.voice_channel().to_channel(&ctx).await?;

    let res = channel
        .guild()
        .and_then(|ch| ch.guild(&ctx))
        .map(|g| {
            (&g.voice_states)
                .into_iter()
                .any(|(_, state)| state.channel_id == Some(CONFIG.discord.voice_channel()))
        })
        .unwrap_or(false);

    Ok(res)
}

#[inline]
pub async fn send(
    ctx: &Context,
    channel: ChannelId,
    text: impl AsRef<str>,
    tts: bool,
) -> Result<()> {
    send_result(ctx, channel, text, tts).await.map(|_| ())
}

pub async fn send_result(
    ctx: &Context,
    channel: ChannelId,
    text: impl AsRef<str>,
    tts: bool,
) -> Result<MessageId> {
    let text = text.as_ref();
    debug!("sending message {:?} to channel {:?} (tts: {})", text, channel, tts);

    let result = channel.send_message(ctx, CreateMessage::default().content(text).tts(tts)).await?;
    Ok(result.id)
}

lazy_static! {
    static ref REQUIRED_PERMS: Permissions = Permissions::EMBED_LINKS
        | Permissions::ADD_REACTIONS
        | Permissions::SEND_MESSAGES
        | Permissions::SEND_TTS_MESSAGES
        | Permissions::MENTION_EVERYONE
        | Permissions::USE_EXTERNAL_EMOJIS
        | Permissions::CONNECT
        | Permissions::SPEAK
        | Permissions::CHANGE_NICKNAME
        | Permissions::USE_VAD
        | Permissions::ATTACH_FILES;
}

lazy_static! {
    pub static ref OAUTH_URL: Url = Url::parse(&format!(
        "https://discordapp.com/api/oauth2/authorize?scope=bot&permissions={}&client_id={}",
        REQUIRED_PERMS.bits(),
        CONFIG.discord.auth.client_id,
    ))
    .unwrap();
}

pub async fn ytdl_url(uri: &str) -> Result<String> {
    use serde_json::Value;
    use tokio::process::Command;

    lazy_static! {
        static ref YTDL_COMMAND: String = {
            let result = CONFIG.ytdl.clone().unwrap_or("youtube-dl".to_owned());
            log::debug!("got ytdl: {}", result);

            result
        };
    }

    let args = [
        "-f",
        "webm[abr>0]/bestaudio/best",
        "--no-playlist",
        "--print-json",
        "--skip-download",
        uri,
    ];

    let out = Command::new(&*YTDL_COMMAND).args(&args).stdin(Stdio::null()).output().await?;

    if !out.status.success() {
        return Err(anyhow::anyhow!("running ytdl: {out:?}"));
    }

    let value = serde_json::from_reader(&out.stdout[..])?;
    let mut obj = match value {
        Value::Object(obj) => obj,
        other => return Err(anyhow::anyhow!("ytdl output not object: {other:?}")),
    };

    match obj.remove("url") {
        Some(v) => match v {
            Value::String(uri) => Ok(uri),
            other => Err(anyhow::anyhow!("url not string: {other:?}")),
        },
        None => Err(anyhow::anyhow!("no url")),
    }
}
pub fn parse_times<A: AsRef<str>>(s: A) -> (Option<Duration>, Option<Duration>) {
    lazy_static! {
        static ref START_REGEX: Regex =
            Regex::new(r"(?:start|begin(?:ning)?)\s*=?\s*(?:(?P<hours>\d+)h\s?)?(?:(?P<minutes>\d+)m\s?)?(?:(?P<seconds>\d+)s?)?").unwrap();

        static ref DUR_REGEX: Regex =
            Regex::new(r"dur(?:ation)?\s*=?\s*(?:(?P<hours>\d+)h\s?)?(?:(?P<minutes>\d+)m\s?)?(?:(?P<seconds>\d+)s?)?").unwrap();

        static ref END_REGEX: Regex =
            Regex::new(r"(?:end|term(?:inate|ination)?)\s*=?\s*(?:(?P<hours>\d+)h\s?)?(?:(?P<minutes>\d+)m\s?)?(?:(?P<seconds>\d+)s?)?").unwrap();
    }

    fn parse_match(m: Option<Match>) -> u64 {
        m.and_then(|s| s.as_str().parse::<u64>().ok()).unwrap_or(0)
    }

    fn parse_captures<B: AsRef<str>>(r: &Regex, s: B) -> Option<Duration> {
        r.captures(s.as_ref()).map(|capt| {
            let hours = parse_match(capt.name("hours"));
            let minutes = parse_match(capt.name("minutes"));
            let seconds = parse_match(capt.name("seconds"));

            let result = Duration::hours(hours as i64)
                + Duration::minutes(minutes as i64)
                + Duration::seconds(seconds as i64);

            assert!(result >= Duration::zero());

            result
        })
    }

    let start_time = parse_captures(&START_REGEX, &s);
    let dur = parse_captures(&DUR_REGEX, &s);
    let end_time = parse_captures(&END_REGEX, s)
        .or_else(|| start_time.and_then(|start| dur.map(|d| start + d)));

    (start_time, end_time)
}