cont reorg botinstance
This commit is contained in:
parent
bf88022eac
commit
39791b20a7
4 changed files with 48 additions and 18 deletions
|
@ -1 +1,2 @@
|
|||
pub mod botinstance;
|
||||
pub mod ratelimiter;
|
|
@ -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<TCPTransport<TLS>,StaticLoginCredentials>,
|
||||
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)))]>,
|
||||
twitch_oauth : String,
|
||||
pub bot_channels : Vec<String>,
|
||||
|
@ -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<Channel(String),Ratelimiter>, // 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<botmodule(String),Vec[Enabled(Channel(String)))]>,
|
||||
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<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
|
||||
}
|
||||
|
||||
}
|
27
src/main.rs
27
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<String,RateLimiter> = HashMap::new();
|
||||
// // ratelimiters are a hashmap of channel and a corresponding rate limiter
|
||||
// let mut ratelimiters:HashMap<String,RateLimiter> = 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
|
||||
|
|
Loading…
Reference in a new issue