aboutsummaryrefslogtreecommitdiff
path: root/src/audio/mod.rs
blob: a7f3e8359054f447419a4ff678a87582dffb3015 (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
use std::sync::Arc;

use either::Either;
use typemap::Key;
use chrono::Duration;

use serenity::{
    model::{
        id::ChannelId,
    },
    prelude::*,
    client::bridge::voice::ClientVoiceManager,
    voice::LockedAudio,
};

use crate::{
    must_env_lookup,
    Result,
};

pub use self::timeutil::parse_times;
pub use self::ytdl::ytdl;
pub use self::play_queue::PlayQueue;

mod timeutil;
mod ytdl;
mod play_queue;

pub trait CtxExt {
    fn currently_playing(&self) -> bool;
    fn users_listening(&self) -> Result<bool>;
}

impl CtxExt for Context {
    fn currently_playing(&self) -> bool {
        let queue_lock = self.data.lock().get::<PlayQueue>().cloned().unwrap();
        let play_queue = queue_lock.read().unwrap();
        play_queue.playing.is_none()
    }

    fn users_listening(&self) -> Result<bool> {
        let channel_id = ChannelId(must_env_lookup::<u64>("VOICE_CHANNEL"));
        let channel = channel_id.to_channel()?;
        let res = channel.guild()
            .and_then(|ch| ch.read().guild())
            .map(|g| (&g.read().voice_states)
                .into_iter()
                .any(|(_, state)| state.channel_id == Some(channel_id)))
            .unwrap_or(false);

        Ok(res)
    }
}

pub struct VoiceManager;

impl Key for VoiceManager {
    type Value = Arc<Mutex<ClientVoiceManager>>;
}

impl VoiceManager {
    pub fn register(c: &mut Client) {
        let mut data = c.data.lock();
        data.insert::<VoiceManager>(Arc::clone(&c.voice_manager));
    }
}

#[derive(Clone, Debug)]
pub struct PlayArgs {
    pub data: Either<String, Vec<u8>>,
    pub initiator: String,
    pub sender_channel: ChannelId,
    pub start: Option<Duration>,
    pub end: Option<Duration>,
}

#[derive(Clone)]
pub struct CurrentItem {
    pub init_args: PlayArgs,
    pub audio: LockedAudio,
}