summary refs log tree commit diff
diff options
context:
space:
mode:
authorashelyn ghost <git@ashen.earth>2024-07-08 21:30:34 -0600
committerAshelyn Rose <git@ashen.earth>2024-07-08 21:30:34 -0600
commitd22f2b74d8931a32da0ed970bedcb4e49f4934f7 (patch)
tree45a595ba1ef413a6719458a3a7aed2bd33cea791
parent2bacf887c0f3aff01d60dd1255b2d0e673f550ea (diff)
Basic proxying
-rw-r--r--src/listener.rs7
-rw-r--r--src/system.rs25
2 files changed, 23 insertions, 9 deletions
diff --git a/src/listener.rs b/src/listener.rs
index 1db480f..9d0e062 100644
--- a/src/listener.rs
+++ b/src/listener.rs
@@ -1,5 +1,5 @@
 use tokio::sync::mpsc::Sender;
-use twilight_model::{id::{Id, marker::{MessageMarker, UserMarker}}, user::User};
+use twilight_model::{channel::ChannelMention, id::{Id, marker::{ChannelMarker, MessageMarker, UserMarker}}, user::User};
 use twilight_gateway::{error::ReceiveMessageError, Intents, Shard, ShardId};
 use twilight_model::util::Timestamp;
 
@@ -17,8 +17,6 @@ impl Listener {
     }
 
     pub async fn start_listening(&mut self, channel : Sender<ClientEvent>) {
-        println!("Starting client with token: {}", self.config.discord_token);
-
         let intents = Intents::GUILD_MEMBERS | Intents::GUILD_PRESENCES | Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT;
 
         let mut shard = Shard::new(ShardId::ONE, self.config.discord_token.clone(), intents);
@@ -39,6 +37,7 @@ impl Listener {
                             if let Err(_) = channel.send(ClientEvent::Message {
                                 event_time: message.timestamp,
                                 message_id: message.id,
+                                channel_id: message.channel_id,
                                 author: message.author.clone(),
                                 content: message.content.clone()
                             }).await {
@@ -61,6 +60,7 @@ impl Listener {
                             if let Err(_) = channel.send(ClientEvent::Message {
                                 event_time: message.edited_timestamp.unwrap(),
                                 message_id: message.id,
+                                channel_id: message.channel_id,
                                 author: message.author.unwrap(),
                                 content: message.content.unwrap()
                             }).await {
@@ -92,6 +92,7 @@ pub enum ClientEvent {
     Message {
         event_time: Timestamp,
         message_id: Id<MessageMarker>,
+        channel_id: Id<ChannelMarker>,
         author: User,
         content: String,
     },
diff --git a/src/system.rs b/src/system.rs
index 9a8b327..332e901 100644
--- a/src/system.rs
+++ b/src/system.rs
@@ -2,7 +2,7 @@ use std::{collections::HashMap, num::NonZeroUsize, str::FromStr};
 
 use tokio::sync::mpsc::channel;
 use twilight_http::Client;
-use twilight_model::id::{Id, marker::{MessageMarker, UserMarker}};
+use twilight_model::id::{Id, marker::{ChannelMarker, MessageMarker, UserMarker}};
 use twilight_model::util::Timestamp;
 
 use crate::listener::{Listener, ClientEvent};
@@ -47,11 +47,11 @@ impl System {
         loop {
             match rx.recv().await {
                 Some(event) => match event {
-                    ClientEvent::Message { event_time, message_id, content, author: _ } => {
+                    ClientEvent::Message { event_time, message_id, channel_id, content, author: _ } => {
                         if self.is_new_message(message_id, event_time) {
-                            self.handle_message(content).await;
+                            self.handle_message(message_id, channel_id, content).await;
                         }
-                    }
+                    },
                     ClientEvent::Error(_err) => {
                         println!("Client ran into an error for system {}", self.name);
                         return
@@ -76,7 +76,20 @@ impl System {
         }
     }
 
-    async fn handle_message(&mut self, content: String) {
-        println!("Message: {}", content);
+    async fn handle_message(&mut self, message_id: Id<MessageMarker>, channel_id: Id<ChannelMarker>, content: String) {
+        // Check for command
+        
+        // Check for prefix
+        for member in self.config.members.iter() {
+            if let Some(captures) = member.message_pattern.captures(content.as_str()) {
+                let client = self.clients.get(&member.name).expect("No client for member");
+                let content = captures.name("content").expect("No capture group").as_str();
+
+                if let Ok(_) = client.create_message(channel_id)
+                    .content(content).expect("Cannot set content").await {
+                        client.delete_message(channel_id, message_id).await.expect("Could not delete message");
+                    }
+            }
+        }
     }
 }