aboutsummaryrefslogtreecommitdiff
path: root/src/commands/sound_levels.rs
blob: 2b9048e4f2287db56c35ed3c8b8726b3db3a25d0 (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
use std::error::Error;

use serenity::all::{
    Context,
    Message,
};

use crate::{
    commands::playback::songbird,
    util,
    PoiseContext,
};

/// Mute audio (don't pause).
#[poise::command(prefix_command, guild_only, category = "playback")]
pub async fn mute(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
    let (_sb, call) = songbird(ctx).await?;

    let mut call = call.lock().await;
    call.mute(true).await?;

    Ok(())
}

/// Unmute audio.
#[poise::command(prefix_command, guild_only, category = "playback")]
pub async fn unmute(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
    let (_sb, call) = songbird(ctx).await?;

    let mut call = call.lock().await;
    call.mute(true).await?;

    Ok(())
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
struct PercentArg(f64);

lazy_static::lazy_static! {
    static ref ARG_PCT: regex::Regex = regex::Regex::new(r#"(\d+(?:\.\d+)?)\s*%?(.*)"#).unwrap();
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, thiserror::Error)]
#[error("expected a number with an optional trailing %")]
struct NonPctError;

#[poise::async_trait]
impl<'a> poise::PopArgument<'a> for PercentArg {
    async fn pop_from(
        args: &'a str,
        attachment_index: usize,
        _ctx: &Context,
        _msg: &Message,
    ) -> Result<(&'a str, usize, Self), (Box<dyn Error + Send + Sync>, Option<String>)> {
        let Some(mtch) = ARG_PCT.captures(args) else {
            return Err((Box::new(NonPctError), None));
        };

        let pct = mtch.get(1).unwrap().as_str().parse::<f64>().unwrap();
        let prop = pct / 100.;
        let rest = mtch.get(2).unwrap().as_str();

        Ok((rest, attachment_index, Self(prop)))
    }
}

/// Set volume by percent.
#[poise::command(prefix_command, guild_only, category = "playback")]
pub async fn volume(ctx: PoiseContext<'_>, volume: Option<PercentArg>) -> anyhow::Result<()> {
    let Some(volume) = volume else {
        let cur_vol = util::volume(ctx).await * 100.;

        util::reply(ctx, format!("{cur_vol:.0}%")).await?;
        return Ok(());
    };

    {
        let data = ctx.serenity_context().data.read().await;
        let vol = data.get::<crate::bot::VolumeKey>().unwrap();
        vol.insert(util::guild_id(ctx)?, volume.0);
    }

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

    call.queue().modify_queue(|q| {
        for elt in q {
            elt.set_volume(volume.0 as _)?;
        }

        anyhow::Ok(())
    })
}