reorg common say logic

This commit is contained in:
ModulatingForce 2024-03-23 12:14:51 -04:00
parent 45e3f02297
commit 2957f9462b

View file

@ -27,6 +27,14 @@ pub struct Chat {
pub client: TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>,
}
#[derive(Clone)]
enum BotMsgType<'a> {
SayInReplyTo(&'a PrivmsgMessage,String),
_Say(String,String)
}
impl Chat {
pub fn init(
ratelimiters: HashMap<ChType, RateLimiter>,
@ -43,7 +51,10 @@ impl Chat {
self.ratelimiters.lock().await.insert(chnl, n);
}
pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, mut outmsg: String) {
async fn send_botmsg(&self, msginput: BotMsgType<'_>) {
/*
formats message before sending to TwitchIRC
@ -53,12 +64,21 @@ impl Chat {
*/
let (channel_login,mut outmsg) = match msginput.clone() {
BotMsgType::SayInReplyTo(msg, outmsg) => {
(msg.channel_login.clone(),outmsg)
},
_ => {
panic!("ISSUE : NOT IMPLEMENTED")
},
};
let rl = Arc::clone(&self.ratelimiters);
let mut rllock = rl.lock().await;
let contextratelimiter = rllock
// .get_mut()
.get_mut(&Channel(String::from(&msg.channel_login)))
.get_mut(&Channel(String::from(channel_login.clone())))
.expect("ERROR: Issue with Rate limiters");
// Continue to check the limiter and sleep if required if the minimum is not reached
@ -75,20 +95,38 @@ impl Chat {
outmsg.push_str(blankspace);
}
match msginput.clone() {
BotMsgType::SayInReplyTo(msg, _) => {
self.client.say_in_reply_to(msg, outmsg).await.unwrap();
},
_ => {
panic!("ISSUE : NOT IMPLEMENTED")
},
}
contextratelimiter.increment_counter();
let logstr = format!(
"(#{}) > {} ; contextratelimiter : {:?}",
msg.channel_login, "rate limit counter increase", contextratelimiter
channel_login.clone(), "rate limit counter increase", contextratelimiter
);
if let BotMsgType::SayInReplyTo(msg,_ ) = msginput {
botlog::trace(
logstr.as_str(),
Some("Chat > say_in_reply_to".to_string()),
Some(msg),
Some(&msg),
);
} else {
botlog::trace(
logstr.as_str(),
Some("Chat > say_in_reply_to".to_string()),
None,
);
}
}
ratelimiter::LimiterResp::Skip => {
// (); // do nothing otherwise
@ -98,9 +136,18 @@ impl Chat {
}
}
Log::flush();
}
pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, outmsg: String) {
self.send_botmsg(BotMsgType::SayInReplyTo(msg, outmsg)).await;
}
async fn _say(&self, _: String, _: String) {
// more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say