use tokio::sync::mpsc::UnboundedReceiver; use twitch_irc::login::StaticLoginCredentials; use twitch_irc::ClientConfig; use twitch_irc::SecureTCPTransport; use twitch_irc::TwitchIRCClient; use twitch_irc::message::PrivmsgMessage; use twitch_irc::message::ServerMessage; use twitch_irc::transport::tcp::TCPTransport; use twitch_irc::transport::tcp::TLS; use std::env; use dotenv::dotenv; use std::collections::HashMap; use rand::Rng; //mod sub::ratelimiter; use crate::core::ratelimiter::RateLimiter; use crate::core::ratelimiter; // use crate::core::ratelimiter; enum Ch { channel(String), } pub struct BotInstance { prefix : char, bot_channel : String, pub client : TwitchIRCClient,StaticLoginCredentials>, pub incoming_messages : UnboundedReceiver, pub ratelimiters : HashMap, // used to limit messages sent per channel // botmodules : Hashmap, twitch_oauth : String, pub bot_channels : Vec, /*bot_commands : Vec[BotCommand], bot_listeners : Vec[Listener], bot_routines : Vec[Routine],*/ // botactionsdb : botactionsdb:botactions, // identity : identitymodule, } impl BotInstance { pub fn init() -> BotInstance { dotenv().ok(); let login_name = "modulatingforcebot".to_owned(); let oauth_token = env::var("access_token").unwrap().to_owned(); /* Vector of channels to join */ let mut botchannels = Vec::new(); for chnl in env::var("bot_channels").unwrap().split(',') { // println!("(Env Var # {})",chnl); botchannels.push(String::from(chnl)); } let config = ClientConfig::new_simple( StaticLoginCredentials::new(login_name.to_owned(), Some(oauth_token.to_owned())) ); let (incoming_messages, client) = TwitchIRCClient::::new(config); for chnl in &botchannels { client.join(chnl.to_owned()).unwrap(); // client.say(chnl.to_owned(), "Connected!".to_owned()).await.unwrap(); //client.say(chnl.to_owned(), "annytfLurk".to_owned()).await.unwrap(); } let mut b = BotInstance { prefix : '>', bot_channel : login_name , // tclient : TwitchClient { incoming_messages , client }, incoming_messages : incoming_messages, client : client, ratelimiters : HashMap::new(), // used to limit messages sent per channel // botmodules : Hashmap, twitch_oauth : oauth_token, bot_channels : botchannels, /*bot_commands : Vec[BotCommand], bot_listeners : Vec[Listener], bot_routines : Vec[Routine],*/ // botactionsdb : botactionsdb:botactions, // identity : identitymodule, }; // ratelimiters are a hashmap of channel and a corresponding rate limiter // let mut ratelimiters:HashMap = HashMap::new(); for chnl in &b.bot_channels { let n = RateLimiter::new(); b.ratelimiters.insert(chnl.to_owned(),n); } println!("{:?}",b.ratelimiters); 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..=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) => { 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(); } // ----------------- // 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) -> () { // 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"); } }