diff --git a/src/commands/play.rs b/src/commands/play.rs index fffaf21..feb170d 100644 --- a/src/commands/play.rs +++ b/src/commands/play.rs @@ -30,18 +30,18 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { println!("Guild ID: {:?}", guild_id); - let connect_to = get_vc_id(ctx, &guild_id, &command.user.id).expect("Cannot get channel id"); + let connect_to = get_vc_id(ctx, &guild_id, &command.user.id).await.expect("Cannot get channel id"); let manager = &songbird::get(ctx) .await .expect("Cannot get Songbird.") .clone(); - if !user_util::is_self_connected_to_vc(ctx, &guild_id) { + if !user_util::is_self_connected_to_vc(ctx, &guild_id).await { // self is connected to vc, check if user is in same vc - let self_channel = user_util::get_self_vc_id(ctx, &guild_id); + let self_channel = user_util::get_self_vc_id(ctx, &guild_id).await; - if self_channel.is_none() { + if self_channel.is_err() { return Embed::create( username, "I am not in a VC.", diff --git a/src/commands/stop.rs b/src/commands/stop.rs index aeb7715..002d6f9 100644 --- a/src/commands/stop.rs +++ b/src/commands/stop.rs @@ -14,7 +14,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { } }; - if !user_util::is_self_connected_to_vc(ctx, guild_id) { + if !user_util::is_self_connected_to_vc_cached(ctx, guild_id) { // Bot is not connectd to vc; no need to dc return Embed::create( username, diff --git a/src/util/user_util.rs b/src/util/user_util.rs index 46a91b0..8bfd6ad 100644 --- a/src/util/user_util.rs +++ b/src/util/user_util.rs @@ -1,23 +1,48 @@ -use serenity::all::{ChannelId, Context, Guild, GuildId, UserId}; +use futures::future::BoxFuture; +use serenity::all::{ChannelId, Context, Guild, GuildId, PartialGuild, UserId}; +use serenity::Error; /// Get a guild by id -pub fn get_guild(ctx: &Context, guild_id: &GuildId) -> Option { +pub fn get_guild_cached(ctx: &Context, guild_id: &GuildId) -> Option { let guild = match ctx.cache.guild(guild_id) { Some(guild) => guild, None => { - println!("Cannot get guild!"); + println!("Cannot get guild with id {:?}!", guild_id); return None } - }; // TODO This is always None for some reason + }; println!("Got guild: {:?}", guild.name); Some(guild.clone()) } +pub async fn request_guild(ctx: &Context, guild_id: &GuildId) -> Result { + let guild = match ctx.http.get_guild(*guild_id).await { + Ok(guild) => guild, + Err(error) => { + return Err(error); + } + }; + + Ok(guild.id.to_guild_cached(ctx).unwrap().clone()) +} + +/// Request a guild by id, get it from Discord, not from cache, this is a partial guild +pub async fn request_partial_guild(ctx: &Context, guild_id: &GuildId) -> PartialGuild { + let guild = match ctx.http.get_guild(*guild_id).await { + Ok(guild) => guild, + Err(error) => { + panic!("error whilest getting guild from Discord {}", error); + } + }; + + guild +} + /// Get the current channel id of the bot -pub fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option { - let guild = match get_guild(&ctx, guild_id){ +pub fn get_vc_id_cached(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option { + let guild = match get_guild_cached(&ctx, guild_id){ Some(guild) => guild, None => { println!("Cannot get guild while getting channel id!"); @@ -35,16 +60,39 @@ pub fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option< Some(channel_id) } +pub async fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Result { + let guild = request_guild(&ctx, guild_id).await?; + + let channel_id = guild + .voice_states + .get(user_id) + .and_then(|voice_state| voice_state.channel_id) + .expect("Cannot get channel id"); + + Ok(channel_id) +} + /// Check if the bot is connected to a voice channel -pub fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool { - let channel_id = get_self_vc_id(ctx, guild_id); +pub fn is_self_connected_to_vc_cached(ctx: &Context, guild_id: &GuildId) -> bool { + let channel_id = get_self_vc_id_cached(ctx, guild_id); !channel_id.is_none() } +pub async fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool { + let channel_id = get_self_vc_id(ctx, guild_id); + + !channel_id.await.is_err() +} + /// Get the current channel id of the bot -pub fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option { - let channel_id = get_vc_id(ctx, guild_id, &ctx.cache.current_user().id)?; +pub fn get_self_vc_id_cached(ctx: &Context, guild_id: &GuildId) -> Option { + let channel_id = get_vc_id_cached(ctx, guild_id, &ctx.cache.current_user().id)?; Some(channel_id) } + +pub async fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Result { + let channel_id = get_vc_id(ctx, guild_id, &ctx.cache.current_user().id).await; + channel_id // This throws errors in main.rs for some reason +}