summaryrefslogtreecommitdiff
path: root/src/commands/sound.rs
blob: bfdab78a9f79f57b0f1195bc6b722c7e9cb7b5a7 (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
use super::*;
use serenity::framework::standard::Args;

pub const DEFAULT_VOLUME: f32 = 0.10;

pub fn mute(ctx: &mut Context, _: &Message, _: Args) -> Result<()> {
    let mgr_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
    let mut manager = mgr_lock.lock();

    manager.get_mut(*TARGET_GUILD_ID)
        .map(|handler| {
            if handler.self_mute {
                trace!("Already muted.")
            } else {
                handler.mute(true);
                trace!("Muted");
            }
        });

    Ok(())
}

pub fn unmute(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> {
    let mgr_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
    let mut manager = mgr_lock.lock();

    manager.get_mut(*TARGET_GUILD_ID)
        .map(|handler| {
            if !handler.self_mute {
                trace!("Already unmuted.")
            } else {
                handler.mute(false);
                trace!("Unmuted");
                let _ = send(msg.channel_id, "REEEEEEEEEEEEEE", msg.tts);
            }
        });

    Ok(())
}

pub fn volume(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
    if args.len() == 0 {
        let vol = {
            let queue_lock = ctx.data.lock().get::<PlayQueue>().cloned().unwrap();
            let mut play_queue = queue_lock.read().unwrap();
            (play_queue.volume / DEFAULT_VOLUME * 100.0) as usize
        };

        return send(msg.channel_id, &format!("Volume: {}/100", vol), msg.tts);
    }

    let vol: usize = match args.single::<f32>() {
        Ok(vol) if vol.is_nan() => return send(msg.channel_id, "you're a fuck", msg.tts),
        Ok(vol) => vol as usize,
        Err(_) => return send(msg.channel_id, "???????", msg.tts),
    };

    let mut vol: f32 = (vol as f32)/100.0;  // force aliasing to reasonable values

    if vol > 3.0 {
        vol = 3.0;
    }

    if vol < 0.0 {
        vol = 0.0;
    }

    let queue_lock = ctx.data.lock().get::<PlayQueue>().cloned().unwrap();

    {
        let mut play_queue = queue_lock.write().unwrap();
        play_queue.volume = vol * DEFAULT_VOLUME;
    }

    {
        let play_queue = queue_lock.read().unwrap();

        let current_item = match play_queue.playing {
            Some(ref x) => x,
            None => return Ok(()),
        };

        let mut audio = current_item.audio.lock();
        audio.volume(play_queue.volume);
    }

    Ok(())
}