Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
fix(moderation): properly convert UserId to Member (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Axelen123 committed Jan 7, 2023
1 parent 8376eb9 commit 05ff0a2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/commands/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions src/utils/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,22 @@ pub fn load_configuration() -> Configuration {
pub async fn get_data_lock(ctx: &serenity::Context) -> Arc<RwLock<Data>> {
ctx.data.read().await.get::<Data>().unwrap().clone()
}

pub async fn get_member(
ctx: &serenity::Context,
guild_id: serenity::GuildId,
user_id: serenity::UserId,
) -> serenity::Result<Option<serenity::Member>> {
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),
}
}
3 changes: 2 additions & 1 deletion src/utils/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 05ff0a2

Please sign in to comment.