diff --git a/src/commands/moderation.rs b/src/commands/moderation.rs index feb07ff..c9e961b 100644 --- a/src/commands/moderation.rs +++ b/src/commands/moderation.rs @@ -7,6 +7,7 @@ use poise::serenity_prelude::{ use tracing::{debug, error, trace}; use crate::db::model::{LockedChannel, Muted}; +use crate::utils::bot::get_member; use crate::utils::macros::to_user; use crate::utils::moderation::{ ban_moderation, queue_unmute_member, respond_moderation, BanKind, ModerationKind, @@ -244,7 +245,6 @@ pub async fn mute( let guild_id = ctx.guild_id().unwrap(); let discord = ctx.discord(); - let cache = &discord.cache; let unmute_time = if !mute_duration.is_zero() { Some((now + mute_duration).timestamp() as u64) @@ -260,7 +260,7 @@ pub async fn mute( }; let result = async { - if let Some(mut member) = cache.member(guild_id, id) { + if let Some(mut member) = get_member(discord, guild_id, id).await? { let (is_currently_muted, removed_roles) = crate::utils::moderation::mute_moderation(&ctx, &mut member, mute).await?; // Prevent the bot from overriding the "take" field. diff --git a/src/utils/bot.rs b/src/utils/bot.rs index 322c080..de4e783 100644 --- a/src/utils/bot.rs +++ b/src/utils/bot.rs @@ -13,3 +13,22 @@ pub fn load_configuration() -> Configuration { pub async fn get_data_lock(ctx: &serenity::Context) -> Arc> { ctx.data.read().await.get::().unwrap().clone() } + +pub async fn get_member( + ctx: &serenity::Context, + guild_id: serenity::GuildId, + user_id: serenity::UserId, +) -> serenity::Result> { + match guild_id.member(ctx, user_id).await { + Ok(member) => Ok(Some(member)), + Err(serenity::SerenityError::Http(err)) + if matches!( + err.status_code(), + Some(serenity::http::StatusCode::NOT_FOUND) + ) => + { + Ok(None) + }, + Err(err) => Err(err), + } +} diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index 7e8b0ad..b14d6ce 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -12,6 +12,7 @@ use crate::db::database::Database; use crate::db::model::Muted; use crate::model::application::{Configuration, Mute}; use crate::serenity::SerenityError; +use crate::utils::bot::get_member; use crate::{Context, Error}; pub enum ModerationKind { @@ -94,7 +95,7 @@ pub fn queue_unmute_member( .ok_or("User was not muted.")?; // Update roles if they didn't leave the guild. - if let Some(mut member) = ctx.cache.member(guild_id, user_id) { + if let Some(mut member) = get_member(&ctx, guild_id, user_id).await? { let http = &ctx.http; if let Some(taken_roles) = db_result.taken_roles {