From 2a38c282dd57c2051a568549d62c80d6036e8920 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sun, 17 Nov 2019 21:31:28 -0500 Subject: most restructuring done --- src/util.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index 85ec6d7..1e478d3 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,9 +3,62 @@ use std::{ str::FromStr, }; -use serenity::model::permissions::Permissions; +use serenity::{ + client::Context, + model::{ + id::{ + ChannelId, + MessageId, + }, + permissions::Permissions, + } +}; use url::Url; +use crate::{ + audio::PlayQueue, + Result, +}; + +pub trait CtxExt { + fn currently_playing(&self) -> bool; + fn users_listening(&self) -> Result; + fn send>(&self, channel: ChannelId, text: A, tts: bool) -> Result<()>; + fn send_result>(&self, channel: ChannelId, text: A, tts: bool) -> Result; +} + +impl CtxExt for Context { + fn currently_playing(&self) -> bool { + let queue_lock = self.data.read().get::().cloned().unwrap(); + let play_queue = queue_lock.read().unwrap(); + play_queue.playing.is_some() + } + + fn users_listening(&self) -> Result { + let channel_id = ChannelId(must_env_lookup::("VOICE_CHANNEL")); + let channel = channel_id.to_channel(self)?; + let res = channel.guild() + .and_then(|ch| ch.read().guild(self)) + .map(|g| (&g.read().voice_states) + .into_iter() + .any(|(_, state)| state.channel_id == Some(channel_id))) + .unwrap_or(false); + + Ok(res) + } + + #[inline] + fn send>(&self, channel: ChannelId, text: A, tts: bool) -> Result<()> { + self.send_result(channel, text, tts).map(|_| ()) + } + + #[inline] + fn send_result>(&self, channel: ChannelId, text: A, tts: bool) -> Result { + let result = channel.send_message(self, |m| m.content(text.as_ref()).tts(tts))?; + Ok(result.id) + } +} + lazy_static! { static ref REQUIRED_PERMS: Permissions = Permissions::EMBED_LINKS | Permissions::READ_MESSAGES | -- cgit v1.3.1