botinstance move FINAL

This commit is contained in:
ModulatingForce 2023-12-19 21:43:03 -05:00
parent 39791b20a7
commit 79415210e8
2 changed files with 123 additions and 51 deletions

View file

@ -12,10 +12,13 @@ use dotenv::dotenv;
use std::collections::HashMap; use std::collections::HashMap;
use rand::Rng;
//mod sub::ratelimiter; //mod sub::ratelimiter;
use crate::core::ratelimiter::RateLimiter; use crate::core::ratelimiter::RateLimiter;
use crate::core::ratelimiter;
// use crate::core::ratelimiter; // use crate::core::ratelimiter;
@ -104,4 +107,71 @@ impl BotInstance {
b b
} }
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);
match message {
ServerMessage::Notice(msg) => {
if let Some(chnl) = msg.channel_login {
println!("NOTICE : (#{}) {}", chnl, msg.message_text);
}
}
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..=5);
//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
}
}
},
ServerMessage::Whisper(msg) => {
println!("(w) {}: {}", msg.sender.name, msg.message_text);
},
ServerMessage::Join(msg) => {
println!("JOINED: {}", msg.channel_login);
},
ServerMessage::Part(msg) => {
println!("PARTED: {}", msg.channel_login);
},
_ => {}
}
}
});
join_handle.await.unwrap();
}
} }

View file

@ -21,7 +21,7 @@ use std::collections::HashMap;
pub mod core; pub mod core;
use crate::core::botinstance::BotInstance; use crate::core::botinstance::BotInstance;
// use crate::core::ratelimiter::RateLimiter; // use crate::core::ratelimiter::RateLimiter;
use crate::core::ratelimiter; //use crate::core::ratelimiter;
#[tokio::main] #[tokio::main]
@ -60,7 +60,7 @@ pub async fn main() {
// client.join("modulatingforcebot".to_owned()).unwrap(); // client.join("modulatingforcebot".to_owned()).unwrap();
// client.say("modulatingforcebot".to_owned(), "Connected!".to_owned()).await.unwrap(); // client.say("modulatingforcebot".to_owned(), "Connected!".to_owned()).await.unwrap();
let mut bot = BotInstance::init(); let bot = BotInstance::init();
@ -83,64 +83,66 @@ pub async fn main() {
// println!("{:?}",ratelimiters); // println!("{:?}",ratelimiters);
let join_handle = tokio::spawn(async move { bot.run().await;
// while let Some(message) = incoming_messages.recv().await {
while let Some(message) = bot.incoming_messages.recv().await {
// Below can be used to debug if I want to capture all messages
// println!("Received message: {:?}", message);
match message { // let join_handle = tokio::spawn(async move {
ServerMessage::Notice(msg) => { // // while let Some(message) = incoming_messages.recv().await {
if let Some(chnl) = msg.channel_login { // while let Some(message) = bot.incoming_messages.recv().await {
println!("NOTICE : (#{}) {}", chnl, msg.message_text); // // Below can be used to debug if I want to capture all messages
} // // println!("Received message: {:?}", message);
}
ServerMessage::Privmsg(msg) => { // match message {
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); // ServerMessage::Notice(msg) => {
// if let Some(chnl) = msg.channel_login {
// println!("NOTICE : (#{}) {}", chnl, 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"); // }
// ServerMessage::Privmsg(msg) => {
match contextratelimiter.check_limiter() { // println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
ratelimiter::LimiterResp::Allow => {
let maxblanks = rand::thread_rng().gen_range(1..=5);
//let mut outmsg = "GotTrolled ".to_owned(); // // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
let mut outmsg = "annytfLurk ".to_owned(); // let contextratelimiter = bot.ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
for _i in 1..maxblanks { // match contextratelimiter.check_limiter() {
let blankspace: &str = "󠀀"; // ratelimiter::LimiterResp::Allow => {
outmsg.push_str(blankspace); // let maxblanks = rand::thread_rng().gen_range(1..=5);
} // //let mut outmsg = "GotTrolled ".to_owned();
// let mut outmsg = "annytfLurk ".to_owned();
// client.say_in_reply_to(&msg,outmsg).await.unwrap();
bot.client.say_in_reply_to(&msg,outmsg).await.unwrap(); // for _i in 1..maxblanks {
println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); // let blankspace: &str = "󠀀";
contextratelimiter.increment_counter(); // outmsg.push_str(blankspace);
println!("{:?}",bot.ratelimiters); // }
},
ratelimiter::LimiterResp::Skip => { // // client.say_in_reply_to(&msg,outmsg).await.unwrap();
(); // do nothing otherwise // bot.client.say_in_reply_to(&msg,outmsg).await.unwrap();
} // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase");
} // contextratelimiter.increment_counter();
}, // println!("{:?}",bot.ratelimiters);
ServerMessage::Whisper(msg) => { // },
println!("(w) {}: {}", msg.sender.name, msg.message_text); // ratelimiter::LimiterResp::Skip => {
}, // (); // do nothing otherwise
ServerMessage::Join(msg) => { // }
println!("JOINED: {}", msg.channel_login); // }
}, // },
ServerMessage::Part(msg) => { // ServerMessage::Whisper(msg) => {
println!("PARTED: {}", msg.channel_login); // println!("(w) {}: {}", msg.sender.name, msg.message_text);
}, // },
_ => {} // ServerMessage::Join(msg) => {
} // println!("JOINED: {}", msg.channel_login);
} // },
}); // ServerMessage::Part(msg) => {
// println!("PARTED: {}", msg.channel_login);
// },
// _ => {}
// }
// }
join_handle.await.unwrap(); // });
// join_handle.await.unwrap();
} }