Abstract user penalization

This commit is contained in:
2025-08-15 00:41:15 -03:00
parent 7eb2a99b69
commit b9410bedd7
3 changed files with 66 additions and 57 deletions
+58 -47
View File
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{error::Error, sync::Arc};
use async_trait::async_trait;
use itertools::Itertools;
@@ -15,16 +15,16 @@ use crate::{
groups::calls::{
components::{CodeOfConductButton, NeedHelpButton},
embeds, get_display_profile,
moderation::{Action, escape_message},
moderation::{Action, Rule, escape_message},
},
state::State,
};
#[derive(Debug)]
pub struct MessageRedirector;
pub struct CallMessageHandler;
#[async_trait]
impl OnMessageHandler<State> for MessageRedirector {
impl OnMessageHandler<State> for CallMessageHandler {
async fn on_message(&self, event: &MessageCreate, rt: Arc<Runtime<State>>) -> EventResult {
if event.author.bot || event.author.system.unwrap_or(false) {
return Ok(());
@@ -142,23 +142,6 @@ impl OnMessageHandler<State> for MessageRedirector {
user
};
let reason = format!(
"You broke the following rules during a call:\n{}",
rules
.iter()
.map(|rule| format!("- {}", rule.name()))
.join("\n")
);
let new_incident = user
.incident(
reason.clone(),
message.data.clone(),
Some(event.author.id.get()),
&rt.state.db,
)
.await?;
rt.http
.create_message(event.channel_id)
.content(
@@ -174,32 +157,7 @@ impl OnMessageHandler<State> for MessageRedirector {
))
.await?;
let dm_channel = rt
.http
.create_private_channel(event.author.id)
.await?
.model()
.await?;
let embed = if new_incident.is_first_incident {
embeds::first_warning_embed(&rt.app, &event.0, rules)
} else if new_incident.counting_incidents == 0 {
embeds::repeat_warning_embed(&rt.app, &event.0, rules)
} else if new_incident.counting_incidents == 1 {
embeds::first_ban_embed(&rt.app, &event.0, rules)
} else {
embeds::final_ban_embed(&rt.app, &event.0, rules)
};
rt.http
.create_message(dm_channel.id)
.embeds(&[embed])
.components(&[ActionRow::new(&[
(None, Arc::new(CodeOfConductButton)),
(None, Arc::new(NeedHelpButton)),
])
.build()])
.await?;
penalize_user(user, message, rules, &rt).await?;
} else {
rt.http
.execute_webhook(Id::new(webhook_id), &webhook_token)
@@ -220,3 +178,56 @@ impl OnMessageHandler<State> for MessageRedirector {
Ok(())
}
}
async fn penalize_user(
user: User,
message: Message,
broken_rules: Vec<Arc<dyn Rule>>,
rt: &Runtime<State>,
) -> Result<(), Box<dyn Error>> {
let reason = format!(
"You broke the following rules during a call:\n{}",
broken_rules
.iter()
.map(|rule| format!("- {}", rule.name()))
.join("\n")
);
let new_incident = user
.incident(
reason.clone(),
message.data.clone(),
Some(user.id),
&rt.state.db,
)
.await?;
let dm_channel = rt
.http
.create_private_channel(Id::new(user.id))
.await?
.model()
.await?;
let embed = if new_incident.is_first_incident {
embeds::first_warning_embed(&rt.app, &message, broken_rules)
} else if new_incident.counting_incidents == 0 {
embeds::repeat_warning_embed(&rt.app, &message, broken_rules)
} else if new_incident.counting_incidents == 1 {
embeds::first_ban_embed(&rt.app, &message, broken_rules)
} else {
embeds::final_ban_embed(&rt.app, &message, broken_rules)
};
rt.http
.create_message(dm_channel.id)
.embeds(&[embed])
.components(&[ActionRow::new(&[
(None, Arc::new(CodeOfConductButton)),
(None, Arc::new(NeedHelpButton)),
])
.build()])
.await?;
Ok(())
}