diff --git a/src/commands/moderation/actions.rs b/src/commands/moderation/actions.rs index 86e013d..0513238 100644 --- a/src/commands/moderation/actions.rs +++ b/src/commands/moderation/actions.rs @@ -3,7 +3,7 @@ use crate::{consts::COLORS, Context}; use async_trait::async_trait; use color_eyre::eyre::{eyre, Context as _, Result}; use log::*; -use poise::serenity_prelude::{CacheHttp, Http, Member, Timestamp}; +use poise::serenity_prelude::{CacheHttp, GuildId, Http, Timestamp, User}; type Fields<'a> = Vec<(&'a str, String, bool)>; @@ -14,7 +14,8 @@ pub trait ModActionInfo { async fn run_action( &self, http: impl CacheHttp + AsRef, - user: &Member, + user: &User, + guild_id: &GuildId, reason: String, ) -> Result<()>; } @@ -75,13 +76,8 @@ impl ModAction { } /// public facing message - pub async fn reply( - &self, - ctx: &Context<'_>, - user: &Member, - dm_user: Option, - ) -> Result<()> { - let mut resp = format!("{} {}!", self.data.description(), user.user.name); + pub async fn reply(&self, ctx: &Context<'_>, user: &User, dm_user: Option) -> Result<()> { + let mut resp = format!("{} {}!", self.data.description(), user.name); if dm_user.unwrap_or_default() { resp = format!("{resp} (user notified with direct message)"); @@ -92,23 +88,22 @@ impl ModAction { Ok(()) } - pub async fn dm_user(&self, ctx: &Context<'_>, user: &Member) -> Result<()> { - let guild = ctx.http().get_guild(*user.guild_id.as_u64()).await?; + pub async fn dm_user(&self, ctx: &Context<'_>, user: &User, guild_id: &GuildId) -> Result<()> { + let guild = ctx.http().get_guild(*guild_id.as_u64()).await?; let title = format!("{} from {}!", self.data.description(), guild.name); - user.user - .dm(ctx, |m| { - m.embed(|e| { - e.title(title).color(COLORS["red"]); + user.dm(ctx, |m| { + m.embed(|e| { + e.title(title).color(COLORS["red"]); - if let Some(reason) = &self.reason { - e.description(format!("Reason: {}", reason)); - } + if let Some(reason) = &self.reason { + e.description(format!("Reason: {}", reason)); + } - e - }) + e }) - .await?; + }) + .await?; Ok(()) } @@ -116,7 +111,7 @@ impl ModAction { pub async fn handle( &self, ctx: &Context<'_>, - user: &Member, + user: &User, quiet: Option, dm_user: Option, handle_reply: bool, @@ -127,13 +122,19 @@ impl ModAction { ctx.defer().await?; } + let guild_id = ctx + .guild_id() + .ok_or_else(|| eyre!("Couldn't get GuildId for context!"))?; let actual_reason = self.reason.clone().unwrap_or("".to_string()); if dm_user.unwrap_or_default() { - self.dm_user(ctx, user).await?; + self.dm_user(ctx, user, &guild_id).await?; } - self.data.run_action(ctx, user, actual_reason).await?; + self.data + .run_action(ctx, user, &guild_id, actual_reason) + .await?; + self.log_action(ctx).await?; if handle_reply { @@ -167,12 +168,14 @@ impl ModActionInfo for Ban { async fn run_action( &self, http: impl CacheHttp + AsRef, - user: &Member, + user: &User, + guild_id: &GuildId, reason: String, ) -> Result<()> { debug!("Banning user {user} with reason: \"{reason}\""); - user.ban_with_reason(http, self.purge_messages_days, reason) + guild_id + .ban_with_reason(http, user, self.purge_messages_days, reason) .await?; Ok(()) @@ -199,7 +202,8 @@ impl ModActionInfo for Timeout { async fn run_action( &self, http: impl CacheHttp + AsRef, - user: &Member, + user: &User, + guild_id: &GuildId, reason: String, ) -> Result<()> { todo!() @@ -221,12 +225,13 @@ impl ModActionInfo for Kick { async fn run_action( &self, http: impl CacheHttp + AsRef, - user: &Member, + user: &User, + guild_id: &GuildId, reason: String, ) -> Result<()> { debug!("Kicked user {user} with reason: \"{reason}\""); - user.kick_with_reason(http, &reason).await?; + guild_id.kick_with_reason(http, user, &reason).await?; Ok(()) } diff --git a/src/commands/moderation/mod.rs b/src/commands/moderation/mod.rs index c3b862e..21259f7 100644 --- a/src/commands/moderation/mod.rs +++ b/src/commands/moderation/mod.rs @@ -2,7 +2,7 @@ use crate::Context; use std::error::Error; use color_eyre::eyre::{eyre, Result}; -use poise::serenity_prelude::{ArgumentConvert, ChannelId, GuildId, Member}; +use poise::serenity_prelude::{ArgumentConvert, ChannelId, GuildId, User}; pub mod actions; use actions::{Ban, Kick, ModAction}; @@ -40,7 +40,7 @@ where )] pub async fn ban_user( ctx: Context<'_>, - #[description = "User to ban"] user: Member, + #[description = "User to ban"] user: User, #[description = "Reason to ban"] reason: Option, #[description = "Number of days to purge their messages from (defaults to 0)"] purge_messages_days: Option, @@ -84,7 +84,7 @@ pub async fn mass_ban( .ok_or_else(|| eyre!("Couldn't get GuildId!"))?; let dmd = purge_messages_days.unwrap_or(0); - let users: Vec = split_argument(&ctx, Some(gid), None, users).await?; + let users: Vec = split_argument(&ctx, Some(gid), None, users).await?; for user in &users { let action = ModAction { @@ -114,7 +114,7 @@ pub async fn mass_ban( )] pub async fn kick_user( ctx: Context<'_>, - #[description = "User to kick"] user: Member, + #[description = "User to kick"] user: User, #[description = "Reason to kick"] reason: Option, #[description = "If true, the reply from the bot will be ephemeral"] quiet: Option, #[description = "If true, the affected user will be sent a DM"] dm_user: Option, @@ -148,7 +148,7 @@ pub async fn mass_kick( let gid = ctx .guild_id() .ok_or_else(|| eyre!("Couldn't get GuildId!"))?; - let users: Vec = split_argument(&ctx, Some(gid), None, users).await?; + let users: Vec = split_argument(&ctx, Some(gid), None, users).await?; for user in &users { let action = ModAction {