1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
use crate::colors::Colors;
use crate::Context;
use color_eyre::eyre::{eyre, Result};
use log::*;
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());
debug!("Banning user {} with reason {reason}", user.id);
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());
debug!("Kicking user {} for reason {reason}", user.id);
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(())
}
|