diff options
| author | Nathan Perry <avaglir@gmail.com> | 2019-02-16 14:52:54 -0500 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2019-02-16 14:52:54 -0500 |
| commit | 966a4934552a43d2a1de0e314bacd7bada0a6845 (patch) | |
| tree | 44a895eb5859ff0390f1008f2beed2de4b18e548 /src/commands/sound_levels.rs | |
| parent | 978945e16046fb28b2351ee637fd267c38df93ac (diff) | |
clean up project structure
- Move audio-related code into its own top-level module, separating out playback commands into their own file in `commands`.
- Rename `sound` commands module to `sound_levels`.
Diffstat (limited to 'src/commands/sound_levels.rs')
| -rw-r--r-- | src/commands/sound_levels.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/commands/sound_levels.rs b/src/commands/sound_levels.rs new file mode 100644 index 0000000..fee3e37 --- /dev/null +++ b/src/commands/sound_levels.rs @@ -0,0 +1,103 @@ +use serenity::{ + framework::standard::Args, + model::channel::Message, + prelude::*, +}; + +use crate::{ + audio::{PlayQueue, VoiceManager}, + commands::{ + send, + }, + Result, + TARGET_GUILD_ID, +}; + +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 play_queue = queue_lock.read().unwrap(); + (play_queue.volume / DEFAULT_VOLUME * 100.0) as usize + }; + + return send(msg.channel_id, &format!("volume: {}%", 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 + let adjusted_text = if vol > 3.0 { " (300% max)" } else { "" }; + + 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; + } + + send(msg.channel_id, format!("volume adjusted{}", adjusted_text), msg.tts)?; + + { + 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(()) +} |
