From 966a4934552a43d2a1de0e314bacd7bada0a6845 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sat, 16 Feb 2019 14:52:54 -0500 Subject: 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`. --- src/audio/mod.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/audio/mod.rs (limited to 'src/audio/mod.rs') diff --git a/src/audio/mod.rs b/src/audio/mod.rs new file mode 100644 index 0000000..a7f3e83 --- /dev/null +++ b/src/audio/mod.rs @@ -0,0 +1,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; +} + +impl CtxExt for Context { + fn currently_playing(&self) -> bool { + let queue_lock = self.data.lock().get::().cloned().unwrap(); + let play_queue = queue_lock.read().unwrap(); + play_queue.playing.is_none() + } + + fn users_listening(&self) -> Result { + let channel_id = ChannelId(must_env_lookup::("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>; +} + +impl VoiceManager { + pub fn register(c: &mut Client) { + let mut data = c.data.lock(); + data.insert::(Arc::clone(&c.voice_manager)); + } +} + +#[derive(Clone, Debug)] +pub struct PlayArgs { + pub data: Either>, + pub initiator: String, + pub sender_channel: ChannelId, + pub start: Option, + pub end: Option, +} + +#[derive(Clone)] +pub struct CurrentItem { + pub init_args: PlayArgs, + pub audio: LockedAudio, +} -- cgit v1.3.1