From b2ba381e44c617d8896197650c2e605f98f28121 Mon Sep 17 00:00:00 2001 From: moonleay Date: Wed, 6 Mar 2024 23:30:39 +0100 Subject: [PATCH] fix: fixed not being able to get guild struct, continued work on play command --- src/commands/play.rs | 39 +++++++++++++++++---------------------- src/main.rs | 1 + src/util/user_util.rs | 30 +++++++++++++----------------- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/commands/play.rs b/src/commands/play.rs index feb170d..3102f5a 100644 --- a/src/commands/play.rs +++ b/src/commands/play.rs @@ -16,7 +16,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { None } }); - + if query.is_none() { return Embed::create(username, "Error 400", "There is no query provided"); } @@ -37,33 +37,28 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed { .expect("Cannot get Songbird.") .clone(); + let self_channel = user_util::get_self_vc_id(ctx, &guild_id).await; 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).await; - if self_channel.is_err() { - return Embed::create( - username, - "I am not in a VC.", - "Connect me to a VC to start playing music.", - ); + if self_channel.is_none() { + // Connect to VC + manager + .join(*guild_id, connect_to) + .await + .expect("Cannot connect>..."); } - let self_channel = self_channel.unwrap(); + } + let self_channel = self_channel.expect("Cannot get self channel"); - // Check if user is in the same VC as the bot - if self_channel != connect_to { - return Embed::create( - username, - "You are not in my VC.", - "Connect to my VC to control the music.", - ); - } - // Connect to VC - manager - .join(*guild_id, connect_to) - .await - .expect("Cannot connect>..."); + // Check if user is in the same VC as the bot + if self_channel != connect_to { + return Embed::create( + username, + "You are not in my VC.", + "Connect to my VC to control the music.", + ); } Embed::create(username, "Searching...", format!("Looking for {:?}", query)) diff --git a/src/main.rs b/src/main.rs index 5c533d4..ed4f368 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,7 @@ async fn main() { .activity(activity) .register_songbird() .type_map_insert::(HttpClient::new()) + .intents(GatewayIntents::all()) .await .expect("Error creating client"); diff --git a/src/util/user_util.rs b/src/util/user_util.rs index 3f70c58..ef0c742 100644 --- a/src/util/user_util.rs +++ b/src/util/user_util.rs @@ -1,5 +1,4 @@ -use futures::future::BoxFuture; -use serenity::all::{ChannelId, Context, Guild, GuildId, PartialGuild, UserId}; +use serenity::all::{CacheHttp, ChannelId, Context, Guild, GuildId, GuildRef, PartialGuild, UserId}; use serenity::Error; /// Get a guild by id @@ -17,15 +16,15 @@ pub fn get_guild_cached(ctx: &Context, guild_id: &GuildId) -> Option { 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); +pub fn request_guild(ctx: &Context, guild_id: &GuildId) -> Guild { + let guild = match guild_id.to_guild_cached(&ctx.cache) { + Some(guild) => guild.clone(), + None => { + panic!("Cannot get guild with id {:?}!", guild_id); } }; - - Ok(guild.id.to_guild_cached(ctx).unwrap().clone()) + + guild } /// Request a guild by id, get it from Discord, not from cache, this is a partial guild @@ -60,16 +59,13 @@ pub fn get_vc_id_cached(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> 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 +pub async fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option { + let guild = request_guild(&ctx, guild_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 @@ -82,7 +78,7 @@ pub fn is_self_connected_to_vc_cached(ctx: &Context, guild_id: &GuildId) -> bool 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() + !channel_id.await.is_none() } /// Get the current channel id of the bot @@ -92,7 +88,7 @@ pub fn get_self_vc_id_cached(ctx: &Context, guild_id: &GuildId) -> Option Result { +pub async fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option { let user_id = ctx.cache.current_user().id; get_vc_id(ctx, guild_id, &user_id).await