summary refs log tree commit diff
path: root/src/system/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/system/mod.rs')
-rw-r--r--src/system/mod.rs30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/system/mod.rs b/src/system/mod.rs
index 4eea408..cb3f040 100644
--- a/src/system/mod.rs
+++ b/src/system/mod.rs
@@ -10,12 +10,13 @@ use twilight_gateway::{Intents, Shard, ShardId};
 use twilight_http::Client;
 pub use types::SystemThreadCommand;
 use crate::SystemUiEvent;
-use std::{num::NonZeroU64, sync::Arc};
+use std::{collections::HashMap, num::NonZeroU64, sync::Arc};
 use tokio::sync::Mutex;
 use plugin::get_plugins;
 
 use std::sync::mpsc::Sender as ThreadSender;
 use types::{Member, Response, System};
+use std::iter::once;
 
 pub struct Manager;
 impl Manager {
@@ -36,6 +37,7 @@ impl Manager {
                 ))),
                 client: Arc::new(Mutex::new(Client::new(member.discord_token.clone())))
             }).collect(),
+            message_cache: Arc::new(Mutex::new(HashMap::new())),
         };
 
         let mut message_receiver = aggregator::MessageAggregator::start(&system);
@@ -63,16 +65,22 @@ impl Manager {
                 Some(MemberEvent::Message(message, seen_by)) => {
                     if let Some(command_string) = message.content.strip_prefix(&system.command_prefix) {
                         let mut words = command_string.split_whitespace();
-                        if let Some(command) = words.next() {
-                            if let Some(plugin) = by_command.get(command) {
-                                logger.log_line(None, format!("Handling command: {command}")).await;
-                                let args : Vec<_> = words.collect();
+                        if let Some(first_word) = words.next() {
+                            if let Some((command, plugin)) = by_command
+                                .get(first_word)
+                                .map(|command| Some(command))
+                                .unwrap_or_else(|| by_command.get(first_word.get(0..1).unwrap())) {
+                                    logger.log_line(None, format!("Handling command: {command:?}")).await;
+                                    let args : Vec<_> = match command {
+                                        plugin::PluginCommand::Long(_) => words.collect(),
+                                        plugin::PluginCommand::Short(_) => once(first_word).chain(words).collect(),
+                                    };
 
-                                plugin.handle_command(&logger, &system, &message, args).await;
-                                continue 'member_event;
-                            } else {
-                                logger.log_line(None, format!("Unknown command: {command}")).await;
-                            }
+                                    plugin.handle_command(&logger, &system, &message, *command, args).await;
+                                    continue 'member_event;
+                                } else {
+                                    logger.log_line(None, format!("Unknown command: {first_word}")).await;
+                                }
                         }
                     }
 
@@ -97,6 +105,8 @@ impl Manager {
                                 if let Err(err) = {member.client.lock().await.delete_message(message.channel_id, message.id).await.map(|_| ()).map_err(|err| err.to_string()).clone() } {
                                     logger.log_err(Some(member.discord_token), format!("Could not proxy message: {err}")).await;
                                     {let _ = member.client.lock().await.delete_message(new_message.channel_id, new_message.id).await;}
+                                } else {
+                                    system.cache_most_recent_message(new_message.channel_id, new_message.clone(), member.clone()).await;
                                 }
 
                                 for plugin in &all_plugins {