enforcing channel type usage

This commit is contained in:
ModulatingForce 2023-12-20 20:25:20 -05:00
parent 08cd434e84
commit 0df3a0e648

View file

@ -22,21 +22,34 @@ use crate::core::ratelimiter::RateLimiter;
use crate::core::ratelimiter;
// use crate::core::ratelimiter;
enum Ch {
channel(String),
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ChType {
Channel(String),
}
// impl PartialEq for ChType {
// fn eq(&self, other:&Self) -> bool {
// let Channel(chnlstr1) = self;
// let Channel(chnlstr2) = other;
// chnlstr1 == chnlstr2
// }
// }
// impl Eq for ChType {}
pub use ChType::Channel;
pub struct BotInstance {
prefix : char,
bot_channel : String,
bot_channel : ChType,
pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>,
pub incoming_messages : UnboundedReceiver<ServerMessage>,
pub ratelimiters : HashMap<String,RateLimiter>, // used to limit messages sent per channel
pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
// botmodules : Hashmap<botmodule(String),Vec[Enabled(Channel(String)))]>,
twitch_oauth : String,
pub bot_channels : Vec<String>,
pub bot_channels : Vec<ChType>,
/*bot_commands : Vec[BotCommand],
bot_listeners : Vec[Listener],
bot_routines : Vec[Routine],*/
@ -59,11 +72,18 @@ impl BotInstance {
Vector of channels to join
*/
// let chnl = Channel(String::from("modulatingforcebot"));
let mut botchannels = Vec::new();
// for chnl in env::var("bot_channels").unwrap().split(',') {
// // println!("(Env Var # {})",chnl);
// botchannels.push(String::from(chnl));
// }
for chnl in env::var("bot_channels").unwrap().split(',') {
// println!("(Env Var # {})",chnl);
botchannels.push(String::from(chnl));
botchannels.push(Channel(String::from(chnl)));
}
let config = ClientConfig::new_simple(
@ -73,22 +93,28 @@ impl BotInstance {
let (incoming_messages, client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(config);
for chnl in &botchannels {
// hashmap for channels and their associated ratelimiters
let mut ratelimiters = HashMap::new();
for Channel(chnl) in &botchannels {
// For each channel in botchannels
client.join(chnl.to_owned()).unwrap();
// client.say(chnl.to_owned(), "Connected!".to_owned()).await.unwrap();
//client.say(chnl.to_owned(), "annytfLurk".to_owned()).await.unwrap();
let n = RateLimiter::new();
ratelimiters.insert(Channel(String::from(chnl)),n);
}
let mut b = BotInstance {
let b = BotInstance {
//prefix : '>',
prefix : prefix,
bot_channel : login_name ,
bot_channel : Channel(login_name) ,
// tclient : TwitchClient { incoming_messages , client },
incoming_messages : incoming_messages,
client : client,
ratelimiters : HashMap::new(), // used to limit messages sent per channel
ratelimiters : ratelimiters, // used to limit messages sent per channel
// botmodules : Hashmap<botmodule(String),Vec[Enabled(Channel(String)))]>,
twitch_oauth : oauth_token,
bot_channels : botchannels,
@ -102,10 +128,11 @@ impl BotInstance {
// 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);
}
// for chnl in &b.bot_channels {
// let n = RateLimiter::new();
// // b.ratelimiters.insert(chnl.to_owned(),n);
// b.ratelimiters.insert(chnl,n);
// }
println!("{:?}",b.ratelimiters);
@ -169,7 +196,9 @@ impl BotInstance {
// // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
let contextratelimiter = self.ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
let contextratelimiter = self.ratelimiters
.get_mut(&Channel(String::from(&msg.channel_login)))
.expect("ERROR: Issue with Rate limiters");
// let contextratelimiter = self.ratelimiters.get(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
match contextratelimiter.check_limiter() {