arcuru / chaz

Chaz is an AI bot for Matrix
https://chaz.is
MIT License
45 stars 3 forks source link

Respond to direct mentions #9

Closed hiinaspace closed 2 months ago

hiinaspace commented 2 months ago

feels more natural to me than !chaz. The client will autocomplete the mention, and you can do "@chaz respond please" without stepping on the whole command system ('!chaz respond' would be parsed as a command instead).

This depends on a headjack change to pass the full event back (to get at the mentions):

diff --git a/src/lib.rs b/src/lib.rs
index 73ad967..4cf4e4b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -368,7 +368,7 @@ impl Bot {
     /// Useful for bots that want to act more like chatbots, having some response to every message
     pub fn register_text_handler<F, Fut>(&self, callback: F)
     where
-        F: FnOnce(OwnedUserId, String, Room) -> Fut + Send + 'static + Clone + Sync,
+        F: FnOnce(OwnedUserId, String, Room, OriginalSyncRoomMessageEvent) -> Fut + Send + 'static + Clone + Sync,
         Fut: std::future::Future<Output = Result<(), ()>> + Send + 'static,
     {
         let client = self.client.as_ref().expect("client not initialized");
@@ -381,7 +381,7 @@ impl Bot {
                 if room.state() != RoomState::Joined {
                     return;
                 }
-                let MessageType::Text(text_content) = &event.content.msgtype else {
+                let MessageType::Text(text_content) = &event.content.msgtype.clone() else {
                     return;
                 };
                 if !is_allowed(allow_list, event.sender.as_str(), &username) {
@@ -393,7 +393,7 @@ impl Bot {
                 if is_command(&command_prefix, body) {
                     return;
                 }
-                if let Err(e) = callback(event.sender.clone(), body.to_string(), room).await {
+                if let Err(e) = callback(event.sender.clone(), body.to_string(), room, event.clone()).await {
                     error!("Error responding to: {}\nError: {:?}", body, e);
                 }
             },
arcuru commented 2 months ago

Yea this is much better. I realized this too after using it for a little bit but I hadn't gotten the chance to update it.

This does cause some issues when generating the context, and it doesn't really work like replacing the !chaz invocation yet, but I will fix those later. I started doing that yesterday but realized that I should just merge this in and add on the other features as needed.

Thanks so much :)