Files
discord-music-bot/src/utils.rs
2025-04-20 00:49:17 +08:00

25 lines
673 B
Rust

// src/utils.rs
use serenity::{
client::Context,
model::{
id::{GuildId, UserId},
voice::VoiceState,
},
};
use tracing::instrument;
// Helper function to find the voice state of a user in a specific guild
// using the cached data. Returns None if the user is not in a voice channel
// in that guild, or if the guild is not in the cache.
#[instrument(skip(ctx))]
pub async fn get_voice_state(
ctx: &Context,
guild_id: GuildId,
user_id: UserId
) -> Option<VoiceState> {
ctx.cache.guild(guild_id).await
// Get the guild from cache, then look up the user's voice state in that guild
.and_then(|guild| guild.voice_states.get(&user_id).cloned())
}