chat module
This commit is contained in:
parent
96f1f53be7
commit
6e36c7524a
3 changed files with 201 additions and 83 deletions
|
@ -1,5 +1,6 @@
|
||||||
pub mod botinstance;
|
pub mod botinstance;
|
||||||
pub mod botlog;
|
pub mod botlog;
|
||||||
pub mod botmodules;
|
pub mod botmodules;
|
||||||
|
pub mod chat;
|
||||||
pub mod identity;
|
pub mod identity;
|
||||||
pub mod ratelimiter;
|
pub mod ratelimiter;
|
||||||
|
|
|
@ -14,9 +14,9 @@ use dotenv::dotenv;
|
||||||
|
|
||||||
use casual_logger::Log;
|
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::ratelimiter::RateLimiter;
|
||||||
|
|
||||||
use crate::core::botmodules::bot_actions::actions_util::BotAR;
|
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::identity::{ChangeResult, IdentityManager, Permissible};
|
||||||
|
|
||||||
use crate::core::botlog;
|
use crate::core::botlog;
|
||||||
|
use crate::core::chat::Chat;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||||
pub enum ChType {
|
pub enum ChType {
|
||||||
|
@ -32,102 +33,101 @@ pub enum ChType {
|
||||||
|
|
||||||
pub use ChType::Channel;
|
pub use ChType::Channel;
|
||||||
|
|
||||||
#[derive(Clone)]
|
// #[derive(Clone)]
|
||||||
pub struct Chat {
|
// pub struct Chat {
|
||||||
pub ratelimiters: Arc<Mutex<HashMap<ChType, RateLimiter>>>, // used to limit messages sent per channel
|
// pub ratelimiters: Arc<Mutex<HashMap<ChType, RateLimiter>>>, // used to limit messages sent per channel
|
||||||
pub client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
|
// pub client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
|
||||||
}
|
// }
|
||||||
|
|
||||||
impl Chat {
|
// impl Chat {
|
||||||
pub fn init(
|
// pub fn init(
|
||||||
ratelimiters: HashMap<ChType, RateLimiter>,
|
// ratelimiters: HashMap<ChType, RateLimiter>,
|
||||||
client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
|
// client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
|
||||||
) -> Chat {
|
// ) -> Chat {
|
||||||
Chat {
|
// Chat {
|
||||||
ratelimiters: Arc::new(Mutex::new(ratelimiters)),
|
// ratelimiters: Arc::new(Mutex::new(ratelimiters)),
|
||||||
// client: client,
|
// // client: client,
|
||||||
client,
|
// client,
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub async fn init_channel(&mut self, chnl: ChType) {
|
// pub async fn init_channel(&mut self, chnl: ChType) {
|
||||||
let n = RateLimiter::new();
|
// let n = RateLimiter::new();
|
||||||
self.ratelimiters.lock().await.insert(chnl, n);
|
// 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(&mut self, msg:& PrivmsgMessage , mut outmsg:String) -> () {
|
||||||
pub async fn say_in_reply_to(&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
|
// formats message before sending to TwitchIRC
|
||||||
|
|
||||||
- [x] Custom String Formatting (e.g., adding random black spaces)
|
// - [x] Custom String Formatting (e.g., adding random black spaces)
|
||||||
- [x] Ratelimiter Handling
|
// - [x] Ratelimiter Handling
|
||||||
- [ ] Checkf if BotActions is Enabled & Caller is Allowed to Run
|
// - [ ] 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 contextratelimiter = a
|
||||||
let mut a = a.lock().await;
|
// // .get_mut()
|
||||||
|
// .get_mut(&Channel(String::from(&msg.channel_login)))
|
||||||
|
// .expect("ERROR: Issue with Rate limiters");
|
||||||
|
|
||||||
let contextratelimiter = a
|
// match contextratelimiter.check_limiter() {
|
||||||
// .get_mut()
|
// ratelimiter::LimiterResp::Allow => {
|
||||||
.get_mut(&Channel(String::from(&msg.channel_login)))
|
// let maxblanks = rand::thread_rng().gen_range(1..=20);
|
||||||
.expect("ERROR: Issue with Rate limiters");
|
|
||||||
|
|
||||||
match contextratelimiter.check_limiter() {
|
// for _i in 1..maxblanks {
|
||||||
ratelimiter::LimiterResp::Allow => {
|
// let blankspace: &str = "";
|
||||||
let maxblanks = rand::thread_rng().gen_range(1..=20);
|
// outmsg.push_str(blankspace);
|
||||||
|
// }
|
||||||
|
|
||||||
for _i in 1..maxblanks {
|
// self.client.say_in_reply_to(msg, outmsg).await.unwrap();
|
||||||
let blankspace: &str = "";
|
// // println!("(#{}) > {}", msg.channel_login, "rate limit counter increase");
|
||||||
outmsg.push_str(blankspace);
|
// // 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();
|
// async fn _say(&self, _: String, _: String) {
|
||||||
// println!("(#{}) > {}", msg.channel_login, "rate limit counter increase");
|
// // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say
|
||||||
// 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) {
|
// // self.client.say(msg,outmsg).await.unwrap();
|
||||||
// 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
|
||||||
|
|
||||||
async fn _me(&self, _: String, _: String) {
|
// // self.client.me(msg,outmsg).await.unwrap();
|
||||||
// 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
|
||||||
|
|
||||||
async fn _me_in_reply_to(&self, _: String, _: String) {
|
// // self.client.me(msg,outmsg).await.unwrap();
|
||||||
// more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say
|
// }
|
||||||
|
// }
|
||||||
// self.client.me(msg,outmsg).await.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct BotManagers {
|
pub struct BotManagers {
|
||||||
|
|
117
src/core/chat.rs
Normal file
117
src/core/chat.rs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue