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,19 +37,20 @@ 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 {
@ -59,12 +60,6 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
"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))
}

View file

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

View file

@ -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<Guild> {
Some(guild.clone())
}
pub async fn request_guild(ctx: &Context, guild_id: &GuildId) -> Result<Guild, Error> {
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<ChannelId, Error> {
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<ChannelId> {
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<Channe
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;
get_vc_id(ctx, guild_id, &user_id).await