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

@ -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