diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index 5de3650..5d8e11f 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -113,10 +113,8 @@ impl BotInstance { pub async fn run(mut self) -> () { - //let b = self; - let join_handle = tokio::spawn(async move { - // while let Some(message) = incoming_messages.recv().await { + 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); @@ -129,41 +127,11 @@ impl BotInstance { } ServerMessage::Privmsg(msg) => { println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); - - - // // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); - // let contextratelimiter = self.ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); - - // match contextratelimiter.check_limiter() { - // ratelimiter::LimiterResp::Allow => { - // let maxblanks = rand::thread_rng().gen_range(1..=20); - // //let mut outmsg = "GotTrolled ".to_owned(); - // let mut outmsg = "annytfLurk ".to_owned(); - - // for _i in 1..maxblanks { - // let blankspace: &str = "󠀀"; - // outmsg.push_str(blankspace); - // } - - // // client.say_in_reply_to(&msg,outmsg).await.unwrap(); - // self.client.say_in_reply_to(&msg,outmsg).await.unwrap(); - // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); - // contextratelimiter.increment_counter(); - // println!("{:?}",self.ratelimiters); - // }, - // ratelimiter::LimiterResp::Skip => { - // (); // do nothing otherwise - // } - // } - // BotInstance::listener_main_prvmsg(msg); - //let botref = *self; - // BotInstance::listener_main_prvmsg(botref,msg).await(); - //self.listener_main_prvmsg(&msg); + println!("Privmsg section"); - println!("Something"); - - self.listener_main_prvmsg(); + // b.listener_main_prvmsg(&msg); + self.listener_main_prvmsg(&msg).await; }, ServerMessage::Whisper(msg) => { @@ -191,106 +159,42 @@ impl BotInstance { // ----------------- // PRIVATE FUNCTIONS - // async fn listener_main_prvmsg(bot:BotInstance,msg:PrivmsgMessage) -> () { - // println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); - - - // // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); - // let contextratelimiter = bot.ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); - - // match contextratelimiter.check_limiter() { - // ratelimiter::LimiterResp::Allow => { - // let maxblanks = rand::thread_rng().gen_range(1..=20); - // //let mut outmsg = "GotTrolled ".to_owned(); - // let mut outmsg = "annytfLurk ".to_owned(); - - // for _i in 1..maxblanks { - // let blankspace: &str = "󠀀"; - // outmsg.push_str(blankspace); - // } - - // // client.say_in_reply_to(&msg,outmsg).await.unwrap(); - // bot.client.say_in_reply_to(&msg,outmsg).await.unwrap(); - // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); - // contextratelimiter.increment_counter(); - // println!("{:?}",bot.ratelimiters); - // }, - // ratelimiter::LimiterResp::Skip => { - // (); // do nothing otherwise - // } - // } - - // } + 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); - - - // // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); - // let contextratelimiter = self.ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); - - // match contextratelimiter.check_limiter() { - // ratelimiter::LimiterResp::Allow => { - // let maxblanks = rand::thread_rng().gen_range(1..=20); - // //let mut outmsg = "GotTrolled ".to_owned(); - // let mut outmsg = "annytfLurk ".to_owned(); - - // for _i in 1..maxblanks { - // let blankspace: &str = "󠀀"; - // outmsg.push_str(blankspace); - // } - - // // client.say_in_reply_to(&msg,outmsg).await.unwrap(); - // self.client.say_in_reply_to(msg,outmsg).await.unwrap(); - // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); - // contextratelimiter.increment_counter(); - // println!("{:?}",self.ratelimiters); - // }, - // ratelimiter::LimiterResp::Skip => { - // (); // do nothing otherwise - // } - // } - - // } - - - async fn listener_main_prvmsg(self) -> () { - - // println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); + println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); // // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); - // let contextratelimiter = self.ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); + let contextratelimiter = self.ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); + // let contextratelimiter = self.ratelimiters.get(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); - // match contextratelimiter.check_limiter() { - // ratelimiter::LimiterResp::Allow => { - // let maxblanks = rand::thread_rng().gen_range(1..=20); - // //let mut outmsg = "GotTrolled ".to_owned(); - // let mut outmsg = "annytfLurk ".to_owned(); + match contextratelimiter.check_limiter() { + ratelimiter::LimiterResp::Allow => { + let maxblanks = rand::thread_rng().gen_range(1..=20); + //let mut outmsg = "GotTrolled ".to_owned(); + let mut outmsg = "annytfLurk ".to_owned(); - // for _i in 1..maxblanks { - // let blankspace: &str = "󠀀"; - // outmsg.push_str(blankspace); - // } + for _i in 1..maxblanks { + let blankspace: &str = "󠀀"; + outmsg.push_str(blankspace); + } - // // client.say_in_reply_to(&msg,outmsg).await.unwrap(); - // self.client.say_in_reply_to(msg,outmsg).await.unwrap(); - // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); - // contextratelimiter.increment_counter(); - // println!("{:?}",self.ratelimiters); - // }, - // ratelimiter::LimiterResp::Skip => { - // (); // do nothing otherwise - // } - // } + // client.say_in_reply_to(&msg,outmsg).await.unwrap(); + self.client.say_in_reply_to(msg,outmsg).await.unwrap(); + println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); + contextratelimiter.increment_counter(); + println!("{:?}",self.ratelimiters); + }, + ratelimiter::LimiterResp::Skip => { + (); // do nothing otherwise + } + } - println!("Something"); + println!("End of Separate Listener Main prvmsg"); } - } diff --git a/src/core/ratelimiter.rs b/src/core/ratelimiter.rs index bc64850..e8f73a7 100644 --- a/src/core/ratelimiter.rs +++ b/src/core/ratelimiter.rs @@ -25,22 +25,60 @@ impl RateLimiter { } } - pub fn check_limiter(&mut self) -> LimiterResp { +// pub fn check_limiter(&mut self) -> LimiterResp { - if self.timer.elapsed().as_secs() >= TIME_THRESHOLD_S { +// if self.timer.elapsed().as_secs() >= TIME_THRESHOLD_S { +// // # [x] elapsed >= TIME_THRESHOLD_S +// self.timer = Instant::now(); +// self.msgcounter = 0; +// LimiterResp::Allow +// } else if self.msgcounter < MSG_THRESHOLD { +// // # [x] elapsed < TIME_THRESHOLD_S && msgcounter < MSG_THRESHOLD +// LimiterResp::Allow +// // } else if self.msgcounter >= MSG_THRESHOLD { +// } else { +// // # [x] elapsed < TIME_THRESHOLD_S && msgcounter >= MSG_THRESHOLD +// LimiterResp::Skip +// } +// } + +// pub fn check_limiter(&self) -> LimiterResp { + +// if self.timer.elapsed().as_secs() >= TIME_THRESHOLD_S { +// // # [x] elapsed >= TIME_THRESHOLD_S +// self.timer = Instant::now(); +// self.msgcounter = 0; +// LimiterResp::Allow +// } else if self.msgcounter < MSG_THRESHOLD { +// // # [x] elapsed < TIME_THRESHOLD_S && msgcounter < MSG_THRESHOLD +// LimiterResp::Allow +// // } else if self.msgcounter >= MSG_THRESHOLD { +// } else { +// // # [x] elapsed < TIME_THRESHOLD_S && msgcounter >= MSG_THRESHOLD +// LimiterResp::Skip +// } +// } + +pub fn check_limiter(&mut self) -> LimiterResp { + + if self.timer.elapsed().as_secs() >= TIME_THRESHOLD_S { // # [x] elapsed >= TIME_THRESHOLD_S - self.timer = Instant::now(); - self.msgcounter = 0; - LimiterResp::Allow - } else if self.msgcounter < MSG_THRESHOLD { + self.timer = Instant::now(); + self.msgcounter = 0; + LimiterResp::Allow + } else if self.msgcounter < MSG_THRESHOLD { // # [x] elapsed < TIME_THRESHOLD_S && msgcounter < MSG_THRESHOLD - LimiterResp::Allow - // } else if self.msgcounter >= MSG_THRESHOLD { - } else { + LimiterResp::Allow + // } else if self.msgcounter >= MSG_THRESHOLD { + } else { // # [x] elapsed < TIME_THRESHOLD_S && msgcounter >= MSG_THRESHOLD - LimiterResp::Skip - } + LimiterResp::Skip } +} + + // pub fn increment_counter(&mut self) -> () { + // self.msgcounter += 1; + // } pub fn increment_counter(&mut self) -> () { self.msgcounter += 1;