2024-03-04 22:39:36 +00:00
|
|
|
use serenity::all::{ChannelId, Context, Guild, GuildId, UserId};
|
2024-03-03 22:30:31 +00:00
|
|
|
|
|
|
|
/// Get a guild by id
|
|
|
|
pub fn get_guild(ctx: &Context, guild_id: &GuildId) -> Option<Guild> {
|
2024-03-04 22:39:36 +00:00
|
|
|
let guild = match ctx.cache.guild(guild_id) {
|
|
|
|
Some(guild) => guild,
|
|
|
|
None => {
|
|
|
|
println!("Cannot get guild!");
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
}; // TODO This is always None for some reason
|
|
|
|
|
|
|
|
println!("Got guild: {:?}", guild.name);
|
2024-03-03 22:30:31 +00:00
|
|
|
|
|
|
|
Some(guild.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the current channel id of the bot
|
2024-03-04 22:39:36 +00:00
|
|
|
pub fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<ChannelId> {
|
|
|
|
let guild = match get_guild(&ctx, guild_id){
|
|
|
|
Some(guild) => guild,
|
|
|
|
None => {
|
|
|
|
println!("Cannot get guild while getting channel id!");
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
};
|
2024-03-03 22:30:31 +00:00
|
|
|
|
|
|
|
let channel_id = guild
|
|
|
|
.voice_states
|
2024-03-04 22:39:36 +00:00
|
|
|
.get(user_id)
|
|
|
|
.and_then(|voice_state| voice_state.channel_id)?;
|
2024-03-03 22:30:31 +00:00
|
|
|
|
2024-03-04 22:39:36 +00:00
|
|
|
println!("Got vc with id: {:?}", channel_id);
|
|
|
|
|
|
|
|
Some(channel_id)
|
2024-03-03 22:30:31 +00:00
|
|
|
}
|
2024-02-24 02:05:27 +00:00
|
|
|
|
2024-03-04 22:39:36 +00:00
|
|
|
/// Check if the bot is connected to a voice channel
|
2024-02-24 02:05:27 +00:00
|
|
|
pub fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool {
|
2024-03-04 22:39:36 +00:00
|
|
|
let channel_id = get_self_vc_id(ctx, guild_id);
|
2024-02-24 02:05:27 +00:00
|
|
|
|
2024-03-04 22:39:36 +00:00
|
|
|
!channel_id.is_none()
|
2024-02-24 02:05:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 22:39:36 +00:00
|
|
|
/// Get the current channel id of the bot
|
2024-03-03 22:30:31 +00:00
|
|
|
pub fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
|
2024-03-04 22:39:36 +00:00
|
|
|
let channel_id = get_vc_id(ctx, guild_id, &ctx.cache.current_user().id)?;
|
2024-03-03 22:30:31 +00:00
|
|
|
|
|
|
|
Some(channel_id)
|
2024-02-24 02:05:27 +00:00
|
|
|
}
|