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 | |
parent | f2cd49662f74deb894518d5169fae533454bd6a6 (diff) |
Parses edit, nick, and reproxy commands
-rw-r--r-- | Cargo.lock | 12 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | rust-toolchain.toml | 4 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/system/message_parser.rs | 42 |
5 files changed, 58 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock index c59002b..f460887 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1216,6 +1216,7 @@ dependencies = [ "toml", "twilight-gateway", "twilight-http", + "twilight-mention", "twilight-model", "twilight-validate", ] @@ -1759,6 +1760,15 @@ dependencies = [ ] [[package]] +name = "twilight-mention" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "903136f763a70ed4716c2e6e4f5436c29d9326b748e3a2e2657010d7584f28d1" +dependencies = [ + "twilight-model", +] + +[[package]] name = "twilight-model" version = "0.15.4" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index c540b25..6137233 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,6 @@ tokio = { version = "1.38.0", features = [ "rt", "macros", "time", "rt-multi-thr toml = "0.8.8" twilight-gateway = "0.15.4" twilight-http = "0.15.4" +twilight-mention = "0.15.3" twilight-model = "0.15.4" twilight-validate = "0.15.3" diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..6c7c6ac --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +# Only needed for str_split_whitespace_remainder, once +# that stabilizes we can and should go back to stable +[toolchain] +channel = "nightly-2024-10-04" 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 } |