From 39791b20a7eff8a4c47f75312d3604300d4dee09 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Tue, 19 Dec 2023 21:08:48 -0500 Subject: [PATCH] cont reorg botinstance --- src/core.rs | 3 ++- src/core/botinstance.rs | 36 ++++++++++++++++++++++++++++++----- src/{ => core}/ratelimiter.rs | 0 src/main.rs | 27 ++++++++++++++------------ 4 files changed, 48 insertions(+), 18 deletions(-) rename src/{ => core}/ratelimiter.rs (100%) diff --git a/src/core.rs b/src/core.rs index 7100cfb..5faac0c 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1 +1,2 @@ -pub mod botinstance; \ No newline at end of file +pub mod botinstance; +pub mod ratelimiter; \ No newline at end of file diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index 62af747..ce81a10 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -10,13 +10,24 @@ use twitch_irc::transport::tcp::TLS; use std::env; use dotenv::dotenv; +use std::collections::HashMap; + + +//mod sub::ratelimiter; + +use crate::core::ratelimiter::RateLimiter; +// use crate::core::ratelimiter; + + +struct Channel(String); + pub struct BotInstance { prefix : char, bot_channel : String, pub client : TwitchIRCClient,StaticLoginCredentials>, pub incoming_messages : UnboundedReceiver, - // ratelimiters : HashMap, // used to limit messages sent per channel + pub ratelimiters : HashMap, // used to limit messages sent per channel // botmodules : Hashmap, twitch_oauth : String, pub bot_channels : Vec, @@ -60,13 +71,15 @@ impl BotInstance { //client.say(chnl.to_owned(), "annytfLurk".to_owned()).await.unwrap(); } - BotInstance { + + + let mut b = BotInstance { prefix : '>', bot_channel : login_name , // tclient : TwitchClient { incoming_messages , client }, - incoming_messages, - client, - // ratelimiters : HashMap, // used to limit messages sent per channel + 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, @@ -75,7 +88,20 @@ impl BotInstance { 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 } } \ No newline at end of file diff --git a/src/ratelimiter.rs b/src/core/ratelimiter.rs similarity index 100% rename from src/ratelimiter.rs rename to src/core/ratelimiter.rs diff --git a/src/main.rs b/src/main.rs index 53a9008..56a4aaf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,12 +14,14 @@ use dotenv::dotenv; use std::collections::HashMap; -mod ratelimiter; -use ratelimiter::RateLimiter; +// mod ratelimiter; +// use ratelimiter::RateLimiter; + pub mod core; use crate::core::botinstance::BotInstance; - +// use crate::core::ratelimiter::RateLimiter; +use crate::core::ratelimiter; #[tokio::main] @@ -71,15 +73,15 @@ pub async fn main() { // Adding rate limit functionality to be under : https://dev.twitch.tv/docs/irc/#rate-limits - // ratelimiters are a hashmap of channel and a corresponding rate limiter - let mut ratelimiters:HashMap = HashMap::new(); + // // ratelimiters are a hashmap of channel and a corresponding rate limiter + // let mut ratelimiters:HashMap = HashMap::new(); - for chnl in bot.bot_channels { - let n = RateLimiter::new(); - ratelimiters.insert(chnl.to_owned(),n); - } + // for chnl in bot.bot_channels { + // let n = RateLimiter::new(); + // ratelimiters.insert(chnl.to_owned(),n); + // } - println!("{:?}",ratelimiters); + // println!("{:?}",ratelimiters); let join_handle = tokio::spawn(async move { // while let Some(message) = incoming_messages.recv().await { @@ -97,7 +99,8 @@ pub async fn main() { 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 = 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 => { @@ -114,7 +117,7 @@ pub async fn main() { bot.client.say_in_reply_to(&msg,outmsg).await.unwrap(); println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); contextratelimiter.increment_counter(); - println!("{:?}",ratelimiters); + println!("{:?}",bot.ratelimiters); }, ratelimiter::LimiterResp::Skip => { (); // do nothing otherwise