From ba9362908d2defa784a45c16bc142c66c3fab9a3 Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Thu, 7 Nov 2024 17:33:13 -0700 Subject: don't crash on regex failing to parse --- src/system/message_parser.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/system') diff --git a/src/system/message_parser.rs b/src/system/message_parser.rs index 5c848e9..349ce0e 100644 --- a/src/system/message_parser.rs +++ b/src/system/message_parser.rs @@ -134,13 +134,20 @@ impl MessageParser { _ => {return None;}, }}; - let regex = regex.build().unwrap(); - + let valid_regex = regex.build(); let original_content = &secondary_message.as_ref().unwrap().content; - let new_content = if global { - regex.replace_all(original_content.as_str(), *replacement) + + // If the regex parses, replace with that + let new_content = if let Ok(regex) = valid_regex { + if global { + regex.replace_all(original_content.as_str(), *replacement).to_string() + } else { + regex.replace(original_content.as_str(), *replacement).to_string() + } + + // Else attempt replace as string } else { - regex.replace(original_content.as_str(), *replacement) + original_content.replace(pattern, replacement) }; let editing_member = Self::get_member_id_from_user_id(secondary_message.as_ref().unwrap().author.id, system_config).unwrap(); -- cgit 1.4.1