summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs2
-rw-r--r--src/system/message_parser.rs42
2 files changed, 42 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 12e2f3c..4ed4fed 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,5 @@
+#![feature(str_split_whitespace_remainder)]
+
 mod config;
 mod system;
 use system::Manager;
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
     }