diff options
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/mod.rs | 2 | ||||
| -rw-r--r-- | src/commands/moderation/actions.rs | 81 | ||||
| -rw-r--r-- | src/commands/moderation/mod.rs | 2 |
3 files changed, 85 insertions, 0 deletions
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 8c265d3..5e6419c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -15,6 +15,8 @@ pub fn to_global_commands() -> Vec<Command<Data, Report>> { general::random(), general::version(), moderation::config(), + moderation::ban_user(), + moderation::kick_user(), ] } diff --git a/src/commands/moderation/actions.rs b/src/commands/moderation/actions.rs new file mode 100644 index 0000000..1050656 --- /dev/null +++ b/src/commands/moderation/actions.rs @@ -0,0 +1,81 @@ +use crate::colors::Colors; +use crate::Context; + +use color_eyre::eyre::{eyre, Result}; +use poise::serenity_prelude::{CreateEmbed, User}; + +fn create_moderation_embed( + title: String, + user: &User, + delete_messages_days: Option<u8>, + reason: String, +) -> impl FnOnce(&mut CreateEmbed) -> &mut CreateEmbed { + let fields = [ + ("User", format!("{} ({})", user.name, user.id), false), + ("Reason", reason, false), + ( + "Deleted messages", + format!("Last {} days", delete_messages_days.unwrap_or(0)), + false, + ), + ]; + + |e: &mut CreateEmbed| e.title(title).fields(fields).color(Colors::Red) +} + +// ban a user +#[poise::command( + slash_command, + prefix_command, + default_member_permissions = "BAN_MEMBERS" +)] +pub async fn ban_user( + ctx: Context<'_>, + user: User, + delete_messages_days: Option<u8>, + reason: Option<String>, +) -> Result<()> { + let days = delete_messages_days.unwrap_or(1); + let guild = ctx + .guild() + .ok_or_else(|| eyre!("Couldn't get guild from message; Unable to ban!"))?; + + let reason = reason.unwrap_or("n/a".to_string()); + + if reason != "n/a" { + guild.ban_with_reason(ctx, &user, days, &reason).await?; + } else { + guild.ban(ctx, &user, days).await?; + } + + let embed = create_moderation_embed("User banned!".to_string(), &user, Some(days), reason); + + ctx.send(|m| m.embed(embed)).await?; + + Ok(()) +} + +// kick a user +#[poise::command( + slash_command, + prefix_command, + default_member_permissions = "KICK_MEMBERS" +)] +pub async fn kick_user(ctx: Context<'_>, user: User, reason: Option<String>) -> Result<()> { + let guild = ctx + .guild() + .ok_or_else(|| eyre!("Couldn't get guild from message; Unable to ban!"))?; + + let reason = reason.unwrap_or("n/a".to_string()); + + if reason != "n/a" { + guild.kick_with_reason(ctx, &user, &reason).await?; + } else { + guild.kick(ctx, &user).await?; + } + + let embed = create_moderation_embed("User kicked!".to_string(), &user, None, reason); + ctx.send(|m| m.embed(embed)).await?; + + Ok(()) +} diff --git a/src/commands/moderation/mod.rs b/src/commands/moderation/mod.rs index d54b3f8..def251c 100644 --- a/src/commands/moderation/mod.rs +++ b/src/commands/moderation/mod.rs @@ -1,3 +1,5 @@ +mod actions; mod config; +pub use actions::*; pub use config::config; |
