Skip to content

Commit 2114bed

Browse files
committed
Add support ban/pardon
1 parent 04faac4 commit 2114bed

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ pub fn all() -> Vec<Command> {
4343
general!(stars),
4444
general!(tag),
4545
moderation!(set_welcome),
46+
moderation!(support_ban, support_ban),
47+
moderation!(support_ban, support_unban),
4648
]
4749
}

src/commands/moderation/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod set_welcome;
2+
pub mod support_ban;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#![allow(clippy::unreadable_literal)]
2+
3+
use poise::serenity_prelude::{Member, RoleId};
4+
5+
use crate::{Context, Error};
6+
7+
const ROLE_ID: RoleId = RoleId::new(1149435921301786634);
8+
const HELPER_ID: RoleId = RoleId::new(1391519658917761106);
9+
10+
#[poise::command(
11+
slash_command,
12+
prefix_command,
13+
check = "require_helper",
14+
ephemeral,
15+
guild_only
16+
)]
17+
pub async fn support_ban(ctx: Context<'_>, member: Member) -> Result<(), Error> {
18+
if member.roles.contains(&ROLE_ID) {
19+
ctx.say(format!(
20+
"❌ `{}` is already banned from support.",
21+
member.user.tag()
22+
))
23+
.await?;
24+
25+
return Ok(());
26+
}
27+
28+
member.add_role(ctx.http(), ROLE_ID).await?;
29+
ctx.say(format!("✅ Banned `{}` from support!", &member.user.tag()))
30+
.await?;
31+
32+
Ok(())
33+
}
34+
35+
#[poise::command(
36+
slash_command,
37+
prefix_command,
38+
check = "require_helper",
39+
ephemeral,
40+
guild_only
41+
)]
42+
pub async fn support_unban(ctx: Context<'_>, member: Member) -> Result<(), Error> {
43+
if !member.roles.contains(&ROLE_ID) {
44+
ctx.say(format!(
45+
"❌ `{}` is not banned from support.",
46+
member.user.tag()
47+
))
48+
.await?;
49+
50+
return Ok(());
51+
}
52+
53+
member.remove_role(ctx.http(), ROLE_ID).await?;
54+
ctx.say(format!(
55+
"✅ Unbanned `{}` from support!",
56+
&member.user.tag()
57+
))
58+
.await?;
59+
60+
Ok(())
61+
}
62+
63+
async fn require_helper(ctx: Context<'_>) -> Result<bool, Error> {
64+
let Some(member) = ctx.author_member().await else {
65+
return Ok(false);
66+
};
67+
68+
let is_helper = member.roles.contains(&HELPER_ID);
69+
let is_moderator = member.permissions.unwrap_or_default().manage_roles();
70+
71+
Ok(is_helper || is_moderator)
72+
}

src/handlers/error.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ pub async fn handle(error: FrameworkError<'_, Data, Error>) {
8989
}
9090
}
9191

92+
FrameworkError::CommandCheckFailed { error, ctx, .. } => {
93+
if let Some(error) = error {
94+
// just log - it's probably best if people don't find out when they're breaking the perm checking
95+
log::error!(
96+
"Error checking permissions for {}:\n{error:?}",
97+
ctx.command().name
98+
);
99+
} else if let poise::Context::Application(ctx) = ctx {
100+
// only show for application commands - for prefix commands there is no way to hide the response and avoid spam
101+
ctx.send(
102+
poise::CreateReply::default()
103+
.content("❌ You're not allowed to use this command.")
104+
.ephemeral(true),
105+
)
106+
.await
107+
.ok();
108+
}
109+
}
110+
92111
error => {
93112
if let Err(e) = poise::builtins::on_error(error).await {
94113
error!("Unhandled error occurred:\n{e:#?}");

0 commit comments

Comments
 (0)