diff --git a/Cargo.lock b/Cargo.lock index 200beb9..dc44b9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2013,7 +2013,7 @@ checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" [[package]] name = "payphone" -version = "4.4.24" +version = "4.4.31" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 1a6385a..516777b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "payphone" -version = "4.4.24" +version = "4.4.31" edition = "2024" default-run = "payphone" diff --git a/src/database/models.rs b/src/database/models.rs index f65976a..e9cbcf9 100644 --- a/src/database/models.rs +++ b/src/database/models.rs @@ -1331,38 +1331,29 @@ impl Message { propagation_message_id: u64, session: &Session, ) -> Result, DatabaseError> { - let message_by_propagation_id = - MessageByPropagationId::get_by_propagation_message_id(propagation_message_id, session) - .await?; + let result = session + .query_unpaged( + r#" + SELECT + call_id, + message_id, + author_id, + propagation_message_id, + data + FROM + payphone.messages + WHERE + propagation_message_id = ? + LIMIT 1; + "#, + ( + u64_to_i64(propagation_message_id), + ), + ) + .await? + .into_rows_result()?; - if let Some(message_by_propagation_id) = message_by_propagation_id { - let result = session - .query_unpaged( - r#" - SELECT - call_id, - message_id, - author_id, - propagation_message_id, - data - FROM - payphone.messages - WHERE - call_id = ? AND message_id = ? - LIMIT 1; - "#, - ( - u64_to_i64(message_by_propagation_id.call_id), - u64_to_i64(message_by_propagation_id.message_id), - ), - ) - .await? - .into_rows_result()?; - - return get_single_row_from_result(&result).await; - } - - Ok(None) + return get_single_row_from_result(&result).await; } pub async fn get_last_by_author_id( @@ -1438,61 +1429,6 @@ impl Message { } } -pub struct MessageByPropagationId { - pub call_id: u64, - pub message_id: u64, - pub propagation_message_id: u64, -} - -impl<'a> Object<'a> for MessageByPropagationId { - type Row = (i64, i64, i64); - - fn from_row(row: Self::Row) -> Self { - Self { - call_id: i64_to_u64(row.0), - message_id: i64_to_u64(row.1), - propagation_message_id: i64_to_u64(row.2), - } - } - - fn to_row(value: Self) -> Self::Row { - ( - u64_to_i64(value.call_id), - u64_to_i64(value.message_id), - u64_to_i64(value.propagation_message_id), - ) - } -} - -impl MessageByPropagationId { - pub async fn get_by_propagation_message_id( - propagation_message_id: u64, - session: &Session, - ) -> Result, DatabaseError> { - let propagation_message_id = u64_to_i64(propagation_message_id); - - let result = session - .query_unpaged( - r#" - SELECT - call_id, - message_id, - propagation_message_id - FROM - payphone.messages_by_propagation_id - WHERE - propagation_message_id = ? - LIMIT 1; - "#, - (propagation_message_id,), - ) - .await? - .into_rows_result()?; - - get_single_row_from_result(&result).await - } -} - pub struct MessageByAuthorId { pub call_id: u64, pub message_id: u64, diff --git a/src/groups/calls/events.rs b/src/groups/calls/events.rs index 1b6be1c..87ad649 100644 --- a/src/groups/calls/events.rs +++ b/src/groups/calls/events.rs @@ -31,7 +31,7 @@ use crate::{ components::{CodeOfConductButton, NeedHelpButton}, embeds, masks::{get_mask_avatar_url, get_mask_display_name}, - moderation::{escape_message, moderate_message, Action, Rule}, + moderation::{Action, Rule, escape_message, moderate_message}, }, state::State, utils::{ellipse, i64_to_u64}, @@ -226,7 +226,9 @@ impl OnMessageHandler for CallMessageHandler { opposite_channel.id, channel.id, call.id, - opposite_channel.call_id.map_or("None".into(), |i| i.to_string()) + opposite_channel + .call_id + .map_or("None".into(), |i| i.to_string()) ); return Ok(()); } @@ -290,6 +292,7 @@ impl OnMessageHandler for CallMessageHandler { if rules .iter() + .filter(|rule| !rule.ignore_for_moderators() || !user.is_moderator) .any(|rule| rule.action() == &Action::BlockAndPenalty) { rt.http @@ -320,66 +323,63 @@ impl OnMessageHandler for CallMessageHandler { } else { let mut embeds = vec![]; - if let Some(replying_to) = replying_to { - if let Some((propagation_channel_id, propagation_message_id)) = replying_to + if let Some(replying_to) = replying_to + && let Some((propagation_channel_id, propagation_message_id)) = replying_to .data .propagation_channel_id .zip(replying_to.data.propagation_message_id) - { - let (display_name, avatar_url) = - if let Some(mask) = &replying_to.data.author_mask { - ( - if i64_to_u64(propagation_channel_id) != event.channel_id.get() { - get_mask_display_name(mask) - } else { - format!( - "{} ({})", - get_mask_display_name(mask), - replying_to.data.author_name - ) - }, - get_mask_avatar_url(mask).expect("Failed to get mask avatar URL"), - ) + { + let (display_name, avatar_url) = if let Some(mask) = &replying_to.data.author_mask { + ( + if i64_to_u64(propagation_channel_id) != event.channel_id.get() { + get_mask_display_name(mask) } else { - ( - replying_to.data.author_name.clone(), - replying_to.data.author_avatar_url.clone(), + format!( + "{} ({})", + get_mask_display_name(mask), + replying_to.data.author_name ) - }; + }, + get_mask_avatar_url(mask).expect("Failed to get mask avatar URL"), + ) + } else { + ( + replying_to.data.author_name.clone(), + replying_to.data.author_avatar_url.clone(), + ) + }; - let message_link = if i64_to_u64(propagation_channel_id) == channel.id { - format!( - "https://discord.com/channels/{}/{}/{}", - opposite_channel.server_id, opposite_channel.id, replying_to.message_id + let message_link = if i64_to_u64(propagation_channel_id) == channel.id { + format!( + "https://discord.com/channels/{}/{}/{}", + opposite_channel.server_id, opposite_channel.id, replying_to.message_id + ) + } else { + format!( + "https://discord.com/channels/{}/{}/{}", + channel.server_id, propagation_channel_id, propagation_message_id + ) + }; + + let replying_to_user = User::get_by_id(replying_to.author_id, &rt.state.db).await?; + + embeds.push( + EmbedBuilder::new() + .author( + EmbedAuthorBuilder::new(format!("Replying to {display_name}",)) + .icon_url(ImageSource::url(avatar_url).unwrap()) + .url(message_link), ) - } else { - format!( - "https://discord.com/channels/{}/{}/{}", - channel.server_id, propagation_channel_id, propagation_message_id - ) - }; - - let replying_to_user = - User::get_by_id(replying_to.author_id, &rt.state.db).await?; - - embeds.push( - EmbedBuilder::new() - .author( - EmbedAuthorBuilder::new(format!("Replying to {display_name}",)) - .icon_url(ImageSource::url(avatar_url).unwrap()) - .url(message_link), - ) - .color(if replying_to.data.author_mask.is_some() { - COLOR_WHITE - } else { - replying_to_user - .and_then(|i| i.color.map(|c| c as u32)) - .unwrap_or(COLOR_WHITE) - }) - .description(ellipse(moderate_message(&replying_to.data.content).0, 100)) - .build(), - ); - } + .color(if replying_to.data.author_mask.is_some() { + COLOR_WHITE + } else { + replying_to_user + .and_then(|i| i.color.map(|c| c as u32)) + .unwrap_or(COLOR_WHITE) + }) + .description(ellipse(moderate_message(&replying_to.data.content).0, 100)) + .build(), + ); } if participant.is_masked { @@ -440,14 +440,12 @@ impl OnMessageHandler for CallMessageHandler { error: ApiError::General(error), .. } = e.kind() - { - if error.code == 10015 { + && error.code == 10015 { log::debug!("Webhook not found, updating..."); opposite_channel .update_webhook(&rt.state.db, &rt.http) .await?; } - } return Ok(()); } } @@ -624,8 +622,8 @@ impl OnMessageDeleteHandler for CallMessageDeleteHandler { return Ok(()); }; - if let Some(propagation_message_id) = message.data.propagation_message_id { - if let Some((webhook_id, webhook_token)) = opposite_channel + if let Some(propagation_message_id) = message.data.propagation_message_id + && let Some((webhook_id, webhook_token)) = opposite_channel .webhook_id .zip(opposite_channel.webhook_token) { @@ -641,7 +639,6 @@ impl OnMessageDeleteHandler for CallMessageDeleteHandler { message.insert(&rt.state.db).await?; } - } Ok(()) } diff --git a/src/groups/calls/lists/hate-speech-and-slurs.txt b/src/groups/calls/lists/hate-speech-and-slurs.txt index 5817aa4..d453649 100644 --- a/src/groups/calls/lists/hate-speech-and-slurs.txt +++ b/src/groups/calls/lists/hate-speech-and-slurs.txt @@ -14,8 +14,12 @@ qiga qigas niqa niqas +niqer +niqers nqa nqas +nqer +nlga nlger nlgers ngiga diff --git a/src/groups/calls/moderation.rs b/src/groups/calls/moderation.rs index db25e84..436fd9c 100644 --- a/src/groups/calls/moderation.rs +++ b/src/groups/calls/moderation.rs @@ -399,6 +399,16 @@ pub trait Rule: Send + Sync + Debug { /// [`(Vec, Vec)`] - The list of detected rule violations in the original /// (first vector) and normalized (second vector) message. fn detect(&self, original: &str, normalized: &str) -> (Vec, Vec); + + /// Whether this rule should be ignored for moderators. This is used to avoid censoring + /// messages from moderators, while still allowing them to be detected and logged from regular + /// users. + /// + /// Returns: + /// [`bool`] - Whether this rule should be ignored for moderators. + fn ignore_for_moderators(&self) -> bool { + false + } } /// Parses a file containing phrases to be detected by rules. @@ -624,6 +634,10 @@ impl Rule for AdvertisingRule { (detections, vec![]) } + + fn ignore_for_moderators(&self) -> bool { + true + } } #[derive(Debug)] @@ -760,6 +774,10 @@ impl Rule for LinksRule { (detections, vec![]) } + + fn ignore_for_moderators(&self) -> bool { + true + } } /// The list of moderation rules. diff --git a/src/groups/calls/strings/tips.json b/src/groups/calls/strings/tips.json index 465340f..71fcfb7 100644 --- a/src/groups/calls/strings/tips.json +++ b/src/groups/calls/strings/tips.json @@ -1,3 +1,3 @@ [ - "Hey! I'm Payphone's dev, Nyeki. I know you've seen this message quite some times already, it's annoying to you and it sucks for me, yet I really want to keep Payphone going. We need to pay for hosting in two days, and we're nowhere near the amount we need. I'll have to shut down Payphone forever if we can't make it to $60 by the end of the month, $45 in two days. We're volunteering to develop and moderate Payphone daily for free, but we can't do it without your help. If you can spare a few dollars, please consider [donating to Payphone](). Every little bit helps, and it would really mean the world to me. Thank you so much in advance for your support, I really appreciate every single cent I get of help to keep Payphone up and running." + "Thank you X and Anonymous Person for your support! We have covered next month's hosting costs." ] \ No newline at end of file diff --git a/src/groups/moderation/arguments.rs b/src/groups/moderation/arguments.rs index c1a9745..9c3f01b 100644 --- a/src/groups/moderation/arguments.rs +++ b/src/groups/moderation/arguments.rs @@ -26,10 +26,12 @@ use crate::{ /// [`Result`] - The user from the DB, or an [`ArgumentParsingError`] ready /// to be returned by the parser. async fn get_user_by_id(id: u64, db: &Session) -> Result { - let Some(user) = User::get_by_id(id, db).await? else { - return Err(ArgumentParsingError::InvalidArgument); - }; - Ok(user) + let user = User::get_by_id(id, db).await?; + + match user { + Some(user) => Ok(user), + None => Err(ArgumentParsingError::InvalidArgument), + } } /// Fetches the user from the message's reply and falls back to the command executor. @@ -45,20 +47,19 @@ async fn get_user_by_id(id: u64, db: &Session) -> Result, ) -> Result { - if let Some(referenced) = &ctx.event.referenced_message { - if !referenced.author.system.unwrap_or(false) { - if referenced.author.bot { - if let Some(message) = - Message::get_by_propagation_message_id(referenced.id.get(), &ctx.rt.state.db) - .await? - { - return get_user_by_id(message.author_id, &ctx.rt.state.db).await; - } - } - - // If the replied message's author is not a bot or system user, use them. - return get_user_by_id(referenced.author.id.get(), &ctx.rt.state.db).await; + if let Some(referenced) = &ctx.event.referenced_message + && !referenced.author.system.unwrap_or(false) + { + if referenced.author.bot + && let Some(message) = + Message::get_by_propagation_message_id(referenced.id.get(), &ctx.rt.state.db) + .await? + { + return get_user_by_id(message.author_id, &ctx.rt.state.db).await; } + + // If the replied message's author is not a bot or system user, use them. + return get_user_by_id(referenced.author.id.get(), &ctx.rt.state.db).await; } // fallback to command executor