cont reorg botinstance

This commit is contained in:
ModulatingForce 2023-12-19 21:08:48 -05:00
parent bf88022eac
commit 39791b20a7
4 changed files with 48 additions and 18 deletions

View file

@ -1 +1,2 @@
pub mod botinstance; pub mod botinstance;
pub mod ratelimiter;

View file

@ -10,13 +10,24 @@ use twitch_irc::transport::tcp::TLS;
use std::env; use std::env;
use dotenv::dotenv; 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 { pub struct BotInstance {
prefix : char, prefix : char,
bot_channel : String, bot_channel : String,
pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>, pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>,
pub incoming_messages : UnboundedReceiver<ServerMessage>, pub incoming_messages : UnboundedReceiver<ServerMessage>,
// ratelimiters : HashMap<Channel(String),Ratelimiter>, // used to limit messages sent per channel pub ratelimiters : HashMap<String,RateLimiter>, // used to limit messages sent per channel
// botmodules : Hashmap<botmodule(String),Vec[Enabled(Channel(String)))]>, // botmodules : Hashmap<botmodule(String),Vec[Enabled(Channel(String)))]>,
twitch_oauth : String, twitch_oauth : String,
pub bot_channels : Vec<String>, pub bot_channels : Vec<String>,
@ -60,13 +71,15 @@ impl BotInstance {
//client.say(chnl.to_owned(), "annytfLurk".to_owned()).await.unwrap(); //client.say(chnl.to_owned(), "annytfLurk".to_owned()).await.unwrap();
} }
BotInstance {
let mut b = BotInstance {
prefix : '>', prefix : '>',
bot_channel : login_name , bot_channel : login_name ,
// tclient : TwitchClient { incoming_messages , client }, // tclient : TwitchClient { incoming_messages , client },
incoming_messages, incoming_messages : incoming_messages,
client, client : client,
// ratelimiters : HashMap<Channel(String),Ratelimiter>, // used to limit messages sent per channel ratelimiters : HashMap::new(), // used to limit messages sent per channel
// botmodules : Hashmap<botmodule(String),Vec[Enabled(Channel(String)))]>, // botmodules : Hashmap<botmodule(String),Vec[Enabled(Channel(String)))]>,
twitch_oauth : oauth_token, twitch_oauth : oauth_token,
bot_channels : botchannels, bot_channels : botchannels,
@ -75,7 +88,20 @@ impl BotInstance {
bot_routines : Vec[Routine],*/ bot_routines : Vec[Routine],*/
// botactionsdb : botactionsdb:botactions, // botactionsdb : botactionsdb:botactions,
// identity : identitymodule, // identity : identitymodule,
};
// ratelimiters are a hashmap of channel and a corresponding rate limiter
// let mut ratelimiters:HashMap<String,RateLimiter> = HashMap::new();
for chnl in &b.bot_channels {
let n = RateLimiter::new();
b.ratelimiters.insert(chnl.to_owned(),n);
} }
println!("{:?}",b.ratelimiters);
b
} }
} }

View file

@ -14,12 +14,14 @@ use dotenv::dotenv;
use std::collections::HashMap; use std::collections::HashMap;
mod ratelimiter; // mod ratelimiter;
use ratelimiter::RateLimiter; // use ratelimiter::RateLimiter;
pub mod core; pub mod core;
use crate::core::botinstance::BotInstance; use crate::core::botinstance::BotInstance;
// use crate::core::ratelimiter::RateLimiter;
use crate::core::ratelimiter;
#[tokio::main] #[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 // 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 // // ratelimiters are a hashmap of channel and a corresponding rate limiter
let mut ratelimiters:HashMap<String,RateLimiter> = HashMap::new(); // let mut ratelimiters:HashMap<String,RateLimiter> = HashMap::new();
for chnl in bot.bot_channels { // for chnl in bot.bot_channels {
let n = RateLimiter::new(); // let n = RateLimiter::new();
ratelimiters.insert(chnl.to_owned(),n); // ratelimiters.insert(chnl.to_owned(),n);
} // }
println!("{:?}",ratelimiters); // println!("{:?}",ratelimiters);
let join_handle = tokio::spawn(async move { let join_handle = tokio::spawn(async move {
// while let Some(message) = incoming_messages.recv().await { // 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); 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() { match contextratelimiter.check_limiter() {
ratelimiter::LimiterResp::Allow => { ratelimiter::LimiterResp::Allow => {
@ -114,7 +117,7 @@ pub async fn main() {
bot.client.say_in_reply_to(&msg,outmsg).await.unwrap(); bot.client.say_in_reply_to(&msg,outmsg).await.unwrap();
println!("(#{}) > {}", msg.channel_login, "rate limit counter increase"); println!("(#{}) > {}", msg.channel_login, "rate limit counter increase");
contextratelimiter.increment_counter(); contextratelimiter.increment_counter();
println!("{:?}",ratelimiters); println!("{:?}",bot.ratelimiters);
}, },
ratelimiter::LimiterResp::Skip => { ratelimiter::LimiterResp::Skip => {
(); // do nothing otherwise (); // do nothing otherwise