Reorg PRIVATEMSG loop

This commit is contained in:
ModulatingForce 2023-12-20 18:49:28 -05:00
parent d8c187d53a
commit 606663731c
2 changed files with 77 additions and 135 deletions

View file

@ -113,10 +113,8 @@ impl BotInstance {
pub async fn run(mut self) -> () { pub async fn run(mut self) -> () {
//let b = self;
let join_handle = tokio::spawn(async move { let join_handle = tokio::spawn(async move {
// while let Some(message) = incoming_messages.recv().await {
while let Some(message) = self.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 // Below can be used to debug if I want to capture all messages
// println!("Received message: {:?}", message); // println!("Received message: {:?}", message);
@ -130,40 +128,10 @@ impl BotInstance {
ServerMessage::Privmsg(msg) => { ServerMessage::Privmsg(msg) => {
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
println!("Privmsg section");
// // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); // b.listener_main_prvmsg(&msg);
// let contextratelimiter = self.ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters"); self.listener_main_prvmsg(&msg).await;
// 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!("Something");
self.listener_main_prvmsg();
}, },
ServerMessage::Whisper(msg) => { ServerMessage::Whisper(msg) => {
@ -191,105 +159,41 @@ impl BotInstance {
// ----------------- // -----------------
// PRIVATE FUNCTIONS // PRIVATE FUNCTIONS
// async fn listener_main_prvmsg(bot:BotInstance,msg:PrivmsgMessage) -> () {
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); 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 = 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"); 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() { match contextratelimiter.check_limiter() {
// ratelimiter::LimiterResp::Allow => { ratelimiter::LimiterResp::Allow => {
// let maxblanks = rand::thread_rng().gen_range(1..=20); let maxblanks = rand::thread_rng().gen_range(1..=20);
// //let mut outmsg = "GotTrolled ".to_owned(); //let mut outmsg = "GotTrolled ".to_owned();
// let mut outmsg = "annytfLurk ".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) -> () {
// 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);
// // 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
// }
// }
println!("Something");
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
}
}
println!("End of Separate Listener Main prvmsg");
}
} }

View file

@ -25,6 +25,40 @@ impl RateLimiter {
} }
} }
// 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 {
// // # [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 { 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 {
@ -42,6 +76,10 @@ impl RateLimiter {
} }
} }
// pub fn increment_counter(&mut self) -> () {
// self.msgcounter += 1;
// }
pub fn increment_counter(&mut self) -> () { pub fn increment_counter(&mut self) -> () {
self.msgcounter += 1; self.msgcounter += 1;
} }