chore: clippy improvements

This commit is contained in:
Miguel da Mota 2024-03-09 20:09:17 +01:00
parent e1cf394362
commit 9c1f6bee6d
6 changed files with 143 additions and 92 deletions

View file

@ -1,52 +1,47 @@
use std::collections::HashMap;
use serenity::all::{ChannelId, Context, Guild, GuildId, PartialGuild, UserId, VoiceState};
use std::collections::HashMap;
/// Request a guild by id, get it from cache
pub fn request_guild(ctx: &Context, guild_id: &GuildId) -> Guild {
let guild = match guild_id.to_guild_cached(&ctx.cache) {
match guild_id.to_guild_cached(&ctx.cache) {
Some(guild) => guild.clone(),
None => {
panic!("Cannot get guild with id {:?}!", guild_id);
}
};
guild
}
}
/// Request a guild by id, get it from Discord, not from cache, this is a partial guild
pub async fn request_partial_guild(ctx: &Context, guild_id: &GuildId) -> PartialGuild {
let guild = match ctx.http.get_guild(*guild_id).await {
match ctx.http.get_guild(*guild_id).await {
Ok(guild) => guild,
Err(error) => {
panic!("error whilest getting guild from Discord {}", error);
}
};
guild
}
}
/// Get the voice channel id of a user
pub async fn get_vc_id(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> Option<ChannelId> {
let guild = request_guild(&ctx, guild_id);
let guild = request_guild(ctx, guild_id);
guild
.voice_states
.get(user_id)
.and_then(|voice_state| voice_state.channel_id)
}
/// Check if the bot is connected to a voice channel
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_none()
channel_id.await.is_some()
}
/// Check if a user is connected to a voice channel
pub async fn is_user_connected_to_vc(ctx: &Context, guild_id: &GuildId, user_id: &UserId) -> bool {
let channel_id = get_vc_id(ctx, guild_id, user_id).await;
!channel_id.is_none()
channel_id.is_some()
}
/// Get the voice channel id of the bot
@ -57,15 +52,17 @@ pub async fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option<Channel
}
/// Get all voice states of a guild
pub async fn get_voice_states(ctx: &Context, guild_id: &GuildId) -> HashMap<UserId, VoiceState>{
pub async fn get_voice_states(ctx: &Context, guild_id: &GuildId) -> HashMap<UserId, VoiceState> {
let guild = request_guild(ctx, guild_id);
let voice_states = guild.voice_states.clone();
voice_states
guild.voice_states.clone()
}
/// Get the amount of members in a voice channel
pub async fn get_amount_of_members_in_vc(ctx: &Context, guild_id: &GuildId, channel_id: &ChannelId,) -> usize {
pub async fn get_amount_of_members_in_vc(
ctx: &Context,
guild_id: &GuildId,
channel_id: &ChannelId,
) -> usize {
let voice_states = get_voice_states(ctx, guild_id).await;
let amount = voice_states
.iter()