fix: fixed not being able to get guild struct, continued work on play command

This commit is contained in:
moonleay 2024-03-06 23:30:39 +01:00
parent af50d54729
commit b2ba381e44
Signed by: moonleay
GPG key ID: 82667543CCD715FB
3 changed files with 31 additions and 39 deletions

View file

@ -37,33 +37,28 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
.expect("Cannot get Songbird.") .expect("Cannot get Songbird.")
.clone(); .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 { if !user_util::is_self_connected_to_vc(ctx, &guild_id).await {
// self is connected to vc, check if user is in same vc // 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() { if self_channel.is_none() {
return Embed::create( // Connect to VC
username, manager
"I am not in a VC.", .join(*guild_id, connect_to)
"Connect me to a VC to start playing music.", .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 // Check if user is in the same VC as the bot
if self_channel != connect_to { if self_channel != connect_to {
return Embed::create( return Embed::create(
username, username,
"You are not in my VC.", "You are not in my VC.",
"Connect to my VC to control the music.", "Connect to my VC to control the music.",
); );
}
// Connect to VC
manager
.join(*guild_id, connect_to)
.await
.expect("Cannot connect>...");
} }
Embed::create(username, "Searching...", format!("Looking for {:?}", query)) Embed::create(username, "Searching...", format!("Looking for {:?}", query))

View file

@ -91,6 +91,7 @@ async fn main() {
.activity(activity) .activity(activity)
.register_songbird() .register_songbird()
.type_map_insert::<HttpKey>(HttpClient::new()) .type_map_insert::<HttpKey>(HttpClient::new())
.intents(GatewayIntents::all())
.await .await
.expect("Error creating client"); .expect("Error creating client");

View file

@ -1,5 +1,4 @@
use futures::future::BoxFuture; use serenity::all::{CacheHttp, ChannelId, Context, Guild, GuildId, GuildRef, PartialGuild, UserId};
use serenity::all::{ChannelId, Context, Guild, GuildId, PartialGuild, UserId};
use serenity::Error; use serenity::Error;
/// Get a guild by id /// Get a guild by id
@ -17,15 +16,15 @@ pub fn get_guild_cached(ctx: &Context, guild_id: &GuildId) -> Option<Guild> {
Some(guild.clone()) Some(guild.clone())
} }
pub async fn request_guild(ctx: &Context, guild_id: &GuildId) -> Result<Guild, Error> { pub fn request_guild(ctx: &Context, guild_id: &GuildId) -> Guild {
let guild = match ctx.http.get_guild(*guild_id).await { let guild = match guild_id.to_guild_cached(&ctx.cache) {
Ok(guild) => guild, Some(guild) => guild.clone(),
Err(error) => { None => {
return Err(error); 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 /// 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) Some(channel_id)
} }
pub async fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Result<ChannelId, Error> { pub async fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<ChannelId> {
let guild = request_guild(&ctx, guild_id).await?; let guild = request_guild(&ctx, guild_id);
guild
let channel_id = guild
.voice_states .voice_states
.get(user_id) .get(user_id)
.and_then(|voice_state| voice_state.channel_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 /// 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 { pub async fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool {
let channel_id = get_self_vc_id(ctx, guild_id); 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 /// 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<Channe
Some(channel_id) Some(channel_id)
} }
pub async fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Result<ChannelId, Error> { pub async fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
let user_id = ctx.cache.current_user().id; let user_id = ctx.cache.current_user().id;
get_vc_id(ctx, guild_id, &user_id).await get_vc_id(ctx, guild_id, &user_id).await