diff options
author | Ashelyn Rose <git@ashen.earth> | 2024-10-05 20:07:18 -0600 |
---|---|---|
committer | Ashelyn Rose <git@ashen.earth> | 2024-10-05 20:07:18 -0600 |
commit | 9e15bb9103ba054fa88501cbff2d5e121ffa8544 (patch) | |
tree | 698f77f882a54155315eede6c5a4e86692600dcc /src/system | |
parent | f2cd49662f74deb894518d5169fae533454bd6a6 (diff) |
Parses edit, nick, and reproxy commands
Diffstat (limited to 'src/system')
-rw-r--r-- | src/system/message_parser.rs | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/system/message_parser.rs b/src/system/message_parser.rs index 01bfaee..31747a8 100644 --- a/src/system/message_parser.rs +++ b/src/system/message_parser.rs @@ -1,8 +1,10 @@ use std::sync::LazyLock; use regex::Regex; -use crate::config::{self, System}; +use crate::config::System; +use twilight_mention::ParseMention; +use twilight_model::id::{marker::UserMarker, Id}; use super::{FullMessage, MemberId, MessageId, Timestamp}; pub enum ParsedMessage { @@ -22,7 +24,7 @@ pub enum ParsedMessage { pub enum Command { Edit(MessageId, String), - Reproxy(MessageId, MemberId), + Reproxy(MemberId, MessageId), Nick(MemberId, String), ReloadSystemConfig, ExitSéance, @@ -74,11 +76,47 @@ impl MessageParser { } fn parse_command(message: &FullMessage, secondary_message: Option<FullMessage>, system_config: &System, latch_state: Option<(MemberId, Timestamp)>) -> Command { + let mut words = message.content.strip_prefix("!").unwrap().split_whitespace(); + let first_word = words.next(); + + match first_word { + Some(command_name) => match command_name { + "edit" => return Command::Edit(secondary_message.unwrap().id, words.remainder().unwrap().to_string()), + "nick" => { + if let Some(member) = MessageParser::match_member(words.next(), system_config) { + return Command::Nick(member, words.remainder().unwrap().to_string()); + } + }, + "reproxy" => { + if let Some(member) = MessageParser::match_member(words.next(), system_config) { + return Command::Reproxy(member, secondary_message.unwrap().id); + } + }, + _ => (), + }, + None => return Command::UnknownCommand, + } + // If unable to parse Command::UnknownCommand } + fn match_member(maybe_mention: Option<&str>, system_config: &System) -> Option<MemberId> { + if let Some(maybe_mention) = maybe_mention { + if let Ok(mention) = Id::<UserMarker>::parse(maybe_mention) { + system_config.members.iter().enumerate() + .filter(|(_id, m)| m.user_id.is_some()) + .find(|(_id, m)| m.user_id.unwrap() == mention) + .map(|(id, _m)| id) + } else { + None + } + } else { + None + } + } + fn check_correction(message: &FullMessage, secondary_message: Option<FullMessage>) -> Option<ParsedMessage> { None } |