diff options
| author | Nathan Perry <np@nathanperry.dev> | 2019-11-17 21:31:28 -0500 |
|---|---|---|
| committer | Nathan Perry <np@nathanperry.dev> | 2019-11-17 21:31:28 -0500 |
| commit | 2a38c282dd57c2051a568549d62c80d6036e8920 (patch) | |
| tree | ec25f84dda5cdb100ae093b0a690ef349636b4dc /src/util.rs | |
| parent | 479bb8d584b138054acc6567b72cb3076832e79c (diff) | |
most restructuring done
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 55 |
1 files changed, 54 insertions, 1 deletions
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<bool>;
+ fn send<A: AsRef<str>>(&self, channel: ChannelId, text: A, tts: bool) -> Result<()>;
+ fn send_result<A: AsRef<str>>(&self, channel: ChannelId, text: A, tts: bool) -> Result<MessageId>;
+}
+
+impl CtxExt for Context {
+ fn currently_playing(&self) -> bool {
+ let queue_lock = self.data.read().get::<PlayQueue>().cloned().unwrap();
+ let play_queue = queue_lock.read().unwrap();
+ play_queue.playing.is_some()
+ }
+
+ fn users_listening(&self) -> Result<bool> {
+ let channel_id = ChannelId(must_env_lookup::<u64>("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<A: AsRef<str>>(&self, channel: ChannelId, text: A, tts: bool) -> Result<()> {
+ self.send_result(channel, text, tts).map(|_| ())
+ }
+
+ #[inline]
+ fn send_result<A: AsRef<str>>(&self, channel: ChannelId, text: A, tts: bool) -> Result<MessageId> {
+ 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 |
|
