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

fix(moderation): properly convert UserId to Member #50

Merged
merged 1 commit into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved
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