From 88293377e86c9659e33fa0e91213bd5de499a2d5 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Mon, 29 Jan 2024 02:10:29 -0500 Subject: [PATCH] Listeners with exec_body can be added to ModulesManager --- src/core/botinstance.rs | 28 +++++++++++++++++++++------- src/core/botmodules.rs | 10 ++++++---- src/core/ratelimiter.rs | 2 +- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index fe66a2e..0f1ce9a 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -27,11 +27,13 @@ use crate::core::ratelimiter; // println!("I was here"); // } +// use crate::core::botmodules::BotAction + use crate::core::botmodules; use crate::core::botmodules::ModulesManager; -#[derive(Debug, PartialEq, Eq, Hash)] +#[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum ChType { Channel(String), } @@ -59,6 +61,7 @@ pub use ModType::BotModule; // } +#[derive(Clone)] pub struct Chat { pub ratelimiters : HashMap, // used to limit messages sent per channel pub client : TwitchIRCClient,StaticLoginCredentials>, @@ -268,12 +271,16 @@ impl BotInstance while let Some(message) = self.incoming_messages.recv().await { // Below can be used to debug if I want to capture all messages - // println!("Received message: {:?}", message); + //println!("Received message: {:?}", message); match message { ServerMessage::Notice(msg) => { - if let Some(chnl) = msg.channel_login { - println!("NOTICE : (#{}) {}", chnl, msg.message_text); + // if let Some(chnl) = msg.channel_login { + // println!("NOTICE : (#{}) {}", chnl, msg.message_text); + // } + match msg.channel_login { + Some(chnl) => println!("NOTICE : (#{}) {}", chnl, msg.message_text), + None => println!("NOTICE : {}", msg.message_text), } } ServerMessage::Privmsg(msg) => { @@ -282,7 +289,7 @@ impl BotInstance println!("Privmsg section"); // b.listener_main_prvmsg(&msg); - self.listener_main_prvmsg(&msg).await; + self.listener_main_prvmsg(msg).await; // - BotCommand listener should likely need to be called within the above @@ -361,7 +368,7 @@ impl BotInstance // PRIVATE FUNCTIONS - async fn listener_main_prvmsg(&mut self,msg:& PrivmsgMessage) -> () { + async fn listener_main_prvmsg(&mut self,msg:PrivmsgMessage) -> () { println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); @@ -394,10 +401,17 @@ impl BotInstance // } - self.chat.say_in_reply_to(msg,String::from("annytfLurk")).await; + // self.chat.say_in_reply_to(msg,String::from("annytfLurk")).await; // // [ ] Need to run through all Listener Bodies for Enabled Modules for the context of the message (e.g., ModStatus is Enabled in the context for the channel) + for (_m,acts) in &self.botmodules.botactions { + for a in acts { + if let crate::core::botmodules::BotAction::L(lsnr) = a { + lsnr.execute(self.chat.clone(),msg.clone()).await; + } + } + }; // // [ ] There should be a BotCommand Listener to check for prefixes ran diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index a72ba5c..e2f0647 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -201,7 +201,8 @@ pub mod bot_actions { // pub type ExecBody = Box Pin>>>; // pub type ExecBody = Box Pin + Send>> + Send + Sync>; - pub type ExecBody = Box Pin + Send>> + Send + Sync>; + pub type ExecBody = Box Pin + Send>> + Send + Sync>; + //pub type ExecBody = Box Pin + Send>> + Send + Sync>; // pub fn asyncbox(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody // where @@ -213,6 +214,7 @@ pub mod bot_actions { // } pub fn asyncbox(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody + //pub fn asyncbox(f: fn(Chat,&PrivmsgMessage) -> T) -> ExecBody where //T: Future + 'static, //T: Future , @@ -268,11 +270,11 @@ impl Listener // async fn execute(&self,m:&mut Box>,n:&PrivmsgMessage){ // async fn execute(&self,m:&mut botinstance::Chat,n:&PrivmsgMessage){ - async fn execute(&self,m:botinstance::Chat,n:PrivmsgMessage){ + pub async fn execute(&self,m:botinstance::Chat,n:PrivmsgMessage){ // => 12.23 - [ ] #todo requires field // (&self.exec)(String::from("Hello from BotAction Trait")) //self.exec_body(m,n) - //(self.exec_body)(m,n).await; + (self.exec_body)(m,n).await; // (self.exec_body)(m,n); } } @@ -362,7 +364,7 @@ struct Routine {} pub struct ModulesManager { statusdb: HashMap>, - botactions: HashMap>, + pub botactions: HashMap>, } // impl ModulesManager diff --git a/src/core/ratelimiter.rs b/src/core/ratelimiter.rs index c2526c1..a25871c 100644 --- a/src/core/ratelimiter.rs +++ b/src/core/ratelimiter.rs @@ -5,7 +5,7 @@ const TIME_THRESHOLD_S: u64 = 30; const MSG_THRESHOLD: u32 = 20; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct RateLimiter { timer: Instant, msgcounter: u32,