chat module

This commit is contained in:
ModulatingForce 2024-03-02 10:14:14 -05:00
parent 96f1f53be7
commit 6e36c7524a
3 changed files with 201 additions and 83 deletions

View file

@ -1,5 +1,6 @@
pub mod botinstance;
pub mod botlog;
pub mod botmodules;
pub mod chat;
pub mod identity;
pub mod ratelimiter;

View file

@ -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<Mutex<HashMap<ChType, RateLimiter>>>, // used to limit messages sent per channel
pub client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
}
// #[derive(Clone)]
// pub struct Chat {
// pub ratelimiters: Arc<Mutex<HashMap<ChType, RateLimiter>>>, // used to limit messages sent per channel
// pub client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
// }
impl Chat {
pub fn init(
ratelimiters: HashMap<ChType, RateLimiter>,
client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
) -> Chat {
Chat {
ratelimiters: Arc::new(Mutex::new(ratelimiters)),
// client: client,
client,
}
}
// impl Chat {
// pub fn init(
// ratelimiters: HashMap<ChType, RateLimiter>,
// client: TwitchIRCClient<TCPTransport<TLS>, 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 {

117
src/core/chat.rs Normal file
View file

@ -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<Mutex<HashMap<ChType, RateLimiter>>>, // used to limit messages sent per channel
pub client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
}
impl Chat {
pub fn init(
ratelimiters: HashMap<ChType, RateLimiter>,
client: TwitchIRCClient<TCPTransport<TLS>, 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();
}
}