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
+6 -8
View File
@@ -1,15 +1,13 @@
use std::sync::Arc;
use twilight_model::{
channel::{Message, message::Embed},
oauth::Application,
};
use twilight_model::{channel::message::Embed, oauth::Application};
use twilight_util::builder::embed::{
EmbedAuthorBuilder, EmbedBuilder, EmbedFieldBuilder, EmbedFooterBuilder, ImageSource,
};
use crate::{
constants::{COLOR_RED, COLOR_YELLOW},
database::models::Message,
discord::images,
groups::calls::moderation::Rule,
utils::ellipse,
@@ -35,7 +33,7 @@ pub fn first_warning_embed(
)
.title("Warning")
.description("You broke [Payphone's code of conduct](https://payphone.lat/code-of-conduct) during a call. Read the code of conduct to prevent getting banned.")
.field(EmbedFieldBuilder::new("What message broke the rules?", ellipse(message.content.clone(), 500)))
.field(EmbedFieldBuilder::new("What message broke the rules?", ellipse(message.data.content.clone(), 500)))
.field(EmbedFieldBuilder::new(
"What rules did you break?",
rules_broken
@@ -78,7 +76,7 @@ pub fn repeat_warning_embed(
)
.title("Long Time no See!")
.description("You broke [Payphone's code of conduct](https://payphone.lat/code-of-conduct) during a call. It's not your first offense, yet you've gone for a long time with a good record so we're making this a warning instead. In case you've forgotten the rules, consider this a reminder to re-read them.")
.field(EmbedFieldBuilder::new("What message broke the rules?", ellipse(message.content.clone(), 500)))
.field(EmbedFieldBuilder::new("What message broke the rules?", ellipse(message.data.content.clone(), 500)))
.field(EmbedFieldBuilder::new(
"What rules did you break?",
rules_broken
@@ -121,7 +119,7 @@ pub fn first_ban_embed(
)
.title("Banned")
.description("You broke [Payphone's code of conduct](https://payphone.lat/code-of-conduct) during a call. This is your first offense, and as such, you are being issued a ban. This ban will last for 24 hours, next one will be permanent.")
.field(EmbedFieldBuilder::new("What message broke the rules?", ellipse(message.content.clone(), 500)))
.field(EmbedFieldBuilder::new("What message broke the rules?", ellipse(message.data.content.clone(), 500)))
.field(EmbedFieldBuilder::new(
"What rules did you break?",
rules_broken
@@ -164,7 +162,7 @@ pub fn final_ban_embed(
)
.title("Permanently Banned")
.description("You broke [Payphone's code of conduct](https://payphone.lat/code-of-conduct) during a call. This is your final offense, and as such, you are being issued a permanent ban.")
.field(EmbedFieldBuilder::new("What message broke the rules?", ellipse(message.content.clone(), 500)))
.field(EmbedFieldBuilder::new("What message broke the rules?", ellipse(message.data.content.clone(), 500)))
.field(EmbedFieldBuilder::new(
"What rules did you break?",
rules_broken
+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(())
}