From fe467f60d99efa54f2ef64606e7d39b9b06d7294 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Wed, 8 May 2024 10:28:04 -0400 Subject: update all deps --- src/util.rs | 120 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 52 deletions(-) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index e9b6203..ed5fd54 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,79 +6,95 @@ use serenity::{ MessageId, }, permissions::Permissions, - } + }, }; -use url::Url; use lazy_static::lazy_static; use log::debug; +use serenity::{ + all::CreateMessage, + futures::{ + AsyncReadExt, + StreamExt, + }, +}; +use url::Url; use crate::{ - CONFIG, audio::PlayQueue, Result, + CONFIG, }; -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; +pub async fn currently_playing(ctx: &Context) -> bool { + let queue_lock = { + let data = ctx.data.read().await; + data.get::().cloned().unwrap() + }; + + let play_queue = queue_lock.read().unwrap(); + play_queue.playing.is_some() } -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() - } +pub async fn users_listening(ctx: &Context) -> Result { + let channel = CONFIG.discord.voice_channel().to_channel(&ctx).await?; - fn users_listening(&self) -> Result { - let channel = CONFIG.discord.voice_channel().to_channel(self)?; - let res = channel.guild() - .and_then(|ch| ch.read().guild(self)) - .map(|g| (&g.read().voice_states) + let res = channel + .guild() + .and_then(|ch| ch.guild(&ctx)) + .map(|g| { + (&g.voice_states) .into_iter() - .any(|(_, state)| state.channel_id == Some(CONFIG.discord.voice_channel()))) - .unwrap_or(false); + .any(|(_, state)| state.channel_id == Some(CONFIG.discord.voice_channel())) + }) + .unwrap_or(false); - Ok(res) - } + Ok(res) +} + +#[inline] +pub async fn send( + ctx: &Context, + channel: ChannelId, + text: impl AsRef, + tts: bool, +) -> Result<()> { + send_result(ctx, channel, text, tts).await.map(|_| ()) +} - #[inline] - fn send>(&self, channel: ChannelId, text: A, tts: bool) -> Result<()> { - self.send_result(channel, text, tts).map(|_| ()) - } +pub async fn send_result( + ctx: &Context, + channel: ChannelId, + text: impl AsRef, + tts: bool, +) -> Result { + let text = text.as_ref(); + debug!("sending message {:?} to channel {:?} (tts: {})", text, channel, tts); - #[inline] - fn send_result>(&self, channel: ChannelId, text: A, tts: bool) -> Result { - let text = text.as_ref(); - debug!("sending message {:?} to channel {:?} (tts: {})", text, channel, tts); - let result = channel.send_message(self, |m| m.content(text).tts(tts))?; - Ok(result.id) - } + let result = channel.send_message(ctx, CreateMessage::default().content(text).tts(tts)).await?; + Ok(result.id) } lazy_static! { - static ref REQUIRED_PERMS: Permissions = Permissions::EMBED_LINKS | - Permissions::READ_MESSAGES | - Permissions::ADD_REACTIONS | - Permissions::SEND_MESSAGES | - Permissions::SEND_TTS_MESSAGES | - Permissions::MENTION_EVERYONE | - Permissions::USE_EXTERNAL_EMOJIS | - Permissions::CONNECT | - Permissions::SPEAK | - Permissions::CHANGE_NICKNAME | - Permissions::USE_VAD | - Permissions::ATTACH_FILES; + static ref REQUIRED_PERMS: Permissions = Permissions::EMBED_LINKS + | Permissions::READ_MESSAGES + | Permissions::ADD_REACTIONS + | Permissions::SEND_MESSAGES + | Permissions::SEND_TTS_MESSAGES + | Permissions::MENTION_EVERYONE + | Permissions::USE_EXTERNAL_EMOJIS + | Permissions::CONNECT + | Permissions::SPEAK + | Permissions::CHANGE_NICKNAME + | Permissions::USE_VAD + | Permissions::ATTACH_FILES; } lazy_static! { - pub static ref OAUTH_URL: Url = Url::parse( - &format!( - "https://discordapp.com/api/oauth2/authorize?scope=bot&permissions={}&client_id={}", - REQUIRED_PERMS.bits(), CONFIG.discord.auth.client_id, - ) - ).unwrap(); + pub static ref OAUTH_URL: Url = Url::parse(&format!( + "https://discordapp.com/api/oauth2/authorize?scope=bot&permissions={}&client_id={}", + REQUIRED_PERMS.bits(), + CONFIG.discord.auth.client_id, + )) + .unwrap(); } -- cgit v1.3.1