diff --git a/src/core.rs b/src/core.rs index 51e40d0..5e6c370 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,5 +1,6 @@ pub mod botinstance; pub mod botlog; pub mod botmodules; +pub mod chat; pub mod identity; pub mod ratelimiter; diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index d9c396b..f6e21b4 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -14,9 +14,9 @@ use dotenv::dotenv; use casual_logger::Log; -use rand::Rng; +// use rand::Rng; -use crate::core::ratelimiter; +// use crate::core::ratelimiter; use crate::core::ratelimiter::RateLimiter; use crate::core::botmodules::bot_actions::actions_util::BotAR; @@ -24,6 +24,7 @@ use crate::core::botmodules::ModulesManager; use crate::core::identity::{ChangeResult, IdentityManager, Permissible}; use crate::core::botlog; +use crate::core::chat::Chat; #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum ChType { @@ -32,102 +33,101 @@ pub enum ChType { pub use ChType::Channel; -#[derive(Clone)] -pub struct Chat { - pub ratelimiters: Arc>>, // used to limit messages sent per channel - pub client: TwitchIRCClient, StaticLoginCredentials>, -} +// #[derive(Clone)] +// pub struct Chat { +// pub ratelimiters: Arc>>, // used to limit messages sent per channel +// pub client: TwitchIRCClient, StaticLoginCredentials>, +// } -impl Chat { - pub fn init( - ratelimiters: HashMap, - client: TwitchIRCClient, StaticLoginCredentials>, - ) -> Chat { - Chat { - ratelimiters: Arc::new(Mutex::new(ratelimiters)), - // client: client, - client, - } - } +// impl Chat { +// pub fn init( +// ratelimiters: HashMap, +// client: TwitchIRCClient, StaticLoginCredentials>, +// ) -> Chat { +// Chat { +// ratelimiters: Arc::new(Mutex::new(ratelimiters)), +// // client: client, +// client, +// } +// } - pub async fn init_channel(&mut self, chnl: ChType) { - let n = RateLimiter::new(); - self.ratelimiters.lock().await.insert(chnl, n); - } +// pub async fn init_channel(&mut self, chnl: ChType) { +// let n = RateLimiter::new(); +// self.ratelimiters.lock().await.insert(chnl, n); +// } - // pub async fn say_in_reply_to(&mut self, msg:& PrivmsgMessage , mut outmsg:String) -> () { - pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, mut outmsg: String) { - /* - formats message before sending to TwitchIRC +// // pub async fn say_in_reply_to(&mut self, msg:& PrivmsgMessage , mut outmsg:String) -> () { +// pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, mut outmsg: String) { +// /* +// formats message before sending to TwitchIRC - - [x] Custom String Formatting (e.g., adding random black spaces) - - [x] Ratelimiter Handling - - [ ] Checkf if BotActions is Enabled & Caller is Allowed to Run +// - [x] Custom String Formatting (e.g., adding random black spaces) +// - [x] Ratelimiter Handling +// - [ ] Checkf if BotActions is Enabled & Caller is Allowed to Run - */ +// */ +// let a = Arc::clone(&self.ratelimiters); +// let mut a = a.lock().await; - let a = Arc::clone(&self.ratelimiters); - let mut a = a.lock().await; +// let contextratelimiter = a +// // .get_mut() +// .get_mut(&Channel(String::from(&msg.channel_login))) +// .expect("ERROR: Issue with Rate limiters"); - let contextratelimiter = a - // .get_mut() - .get_mut(&Channel(String::from(&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); - match contextratelimiter.check_limiter() { - ratelimiter::LimiterResp::Allow => { - let maxblanks = rand::thread_rng().gen_range(1..=20); +// for _i in 1..maxblanks { +// let blankspace: &str = "󠀀"; +// outmsg.push_str(blankspace); +// } - for _i in 1..maxblanks { - let blankspace: &str = "󠀀"; - outmsg.push_str(blankspace); - } +// self.client.say_in_reply_to(msg, outmsg).await.unwrap(); +// // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); +// // Log::trace(&format!("(#{}) > {}", msg.channel_login, "rate limit counter increase")); +// botlog::trace( +// &format!( +// "(#{}) > {}", +// msg.channel_login, "rate limit counter increase" +// ), +// Some("Chat > say_in_reply_to".to_string()), +// Some(msg), +// ); +// contextratelimiter.increment_counter(); +// // println!("{:?}",self.ratelimiters); +// // Log::trace(&format!("{:?}",self.ratelimiters)); +// botlog::trace( +// &format!("{:?}", self.ratelimiters), +// Some("Chat > say_in_reply_to".to_string()), +// Some(msg), +// ); +// } +// ratelimiter::LimiterResp::Skip => { +// // (); // do nothing otherwise +// } +// } +// Log::flush(); +// } - self.client.say_in_reply_to(msg, outmsg).await.unwrap(); - // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); - // Log::trace(&format!("(#{}) > {}", msg.channel_login, "rate limit counter increase")); - botlog::trace( - &format!( - "(#{}) > {}", - msg.channel_login, "rate limit counter increase" - ), - Some("Chat > say_in_reply_to".to_string()), - Some(msg), - ); - contextratelimiter.increment_counter(); - // println!("{:?}",self.ratelimiters); - // Log::trace(&format!("{:?}",self.ratelimiters)); - botlog::trace( - &format!("{:?}", self.ratelimiters), - Some("Chat > say_in_reply_to".to_string()), - Some(msg), - ); - } - ratelimiter::LimiterResp::Skip => { - // (); // do nothing otherwise - } - } - Log::flush(); - } +// async fn _say(&self, _: String, _: String) { +// // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say - async fn _say(&self, _: String, _: String) { - // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say +// // self.client.say(msg,outmsg).await.unwrap(); +// } - // self.client.say(msg,outmsg).await.unwrap(); - } +// async fn _me(&self, _: String, _: String) { +// // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say - async fn _me(&self, _: String, _: String) { - // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say +// // self.client.me(msg,outmsg).await.unwrap(); +// } - // self.client.me(msg,outmsg).await.unwrap(); - } +// async fn _me_in_reply_to(&self, _: String, _: String) { +// // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say - async fn _me_in_reply_to(&self, _: String, _: String) { - // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say - - // self.client.me(msg,outmsg).await.unwrap(); - } -} +// // self.client.me(msg,outmsg).await.unwrap(); +// } +// } #[derive(Clone)] pub struct BotManagers { diff --git a/src/core/chat.rs b/src/core/chat.rs new file mode 100644 index 0000000..0390647 --- /dev/null +++ b/src/core/chat.rs @@ -0,0 +1,117 @@ +use std::collections::HashMap; +use std::sync::Arc; + +use tokio::sync::Mutex; + +use twitch_irc::login::StaticLoginCredentials; +use twitch_irc::message::PrivmsgMessage; +use twitch_irc::transport::tcp::{TCPTransport, TLS}; +use twitch_irc::TwitchIRCClient; + +use casual_logger::Log; + +use rand::Rng; + +use crate::core::ratelimiter; +use crate::core::ratelimiter::RateLimiter; + +use crate::core::botinstance::ChType; +use crate::core::botlog; +pub use ChType::Channel; + +#[derive(Clone)] +pub struct Chat { + pub ratelimiters: Arc>>, // used to limit messages sent per channel + pub client: TwitchIRCClient, StaticLoginCredentials>, +} + +impl Chat { + pub fn init( + ratelimiters: HashMap, + client: TwitchIRCClient, StaticLoginCredentials>, + ) -> Chat { + Chat { + ratelimiters: Arc::new(Mutex::new(ratelimiters)), + // client: client, + client, + } + } + + pub async fn init_channel(&mut self, chnl: ChType) { + let n = RateLimiter::new(); + self.ratelimiters.lock().await.insert(chnl, n); + } + + // pub async fn say_in_reply_to(&mut self, msg:& PrivmsgMessage , mut outmsg:String) -> () { + pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, mut outmsg: String) { + /* + formats message before sending to TwitchIRC + + - [x] Custom String Formatting (e.g., adding random black spaces) + - [x] Ratelimiter Handling + - [ ] Checkf if BotActions is Enabled & Caller is Allowed to Run + + */ + + let a = Arc::clone(&self.ratelimiters); + let mut a = a.lock().await; + + let contextratelimiter = a + // .get_mut() + .get_mut(&Channel(String::from(&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); + + for _i in 1..maxblanks { + let blankspace: &str = "󠀀"; + outmsg.push_str(blankspace); + } + + self.client.say_in_reply_to(msg, outmsg).await.unwrap(); + // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); + // Log::trace(&format!("(#{}) > {}", msg.channel_login, "rate limit counter increase")); + botlog::trace( + &format!( + "(#{}) > {}", + msg.channel_login, "rate limit counter increase" + ), + Some("Chat > say_in_reply_to".to_string()), + Some(msg), + ); + contextratelimiter.increment_counter(); + // println!("{:?}",self.ratelimiters); + // Log::trace(&format!("{:?}",self.ratelimiters)); + botlog::trace( + &format!("{:?}", self.ratelimiters), + Some("Chat > say_in_reply_to".to_string()), + Some(msg), + ); + } + ratelimiter::LimiterResp::Skip => { + // (); // do nothing otherwise + } + } + Log::flush(); + } + + async fn _say(&self, _: String, _: String) { + // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say + + // self.client.say(msg,outmsg).await.unwrap(); + } + + async fn _me(&self, _: String, _: String) { + // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say + + // self.client.me(msg,outmsg).await.unwrap(); + } + + async fn _me_in_reply_to(&self, _: String, _: String) { + // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say + + // self.client.me(msg,outmsg).await.unwrap(); + } +}