forcebot_rs/src/core/botinstance.rs

107 lines
3.2 KiB
Rust
Raw Normal View History

2023-12-19 20:38:20 -05:00
use tokio::sync::mpsc::UnboundedReceiver;
use twitch_irc::login::StaticLoginCredentials;
use twitch_irc::ClientConfig;
use twitch_irc::SecureTCPTransport;
use twitch_irc::TwitchIRCClient;
use twitch_irc::message::ServerMessage;
use twitch_irc::transport::tcp::TCPTransport;
use twitch_irc::transport::tcp::TLS;
use std::env;
use dotenv::dotenv;
2023-12-19 21:08:48 -05:00
use std::collections::HashMap;
//mod sub::ratelimiter;
use crate::core::ratelimiter::RateLimiter;
// use crate::core::ratelimiter;
struct Channel(String);
2023-12-19 20:38:20 -05:00
pub struct BotInstance {
prefix : char,
bot_channel : String,
pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>,
pub incoming_messages : UnboundedReceiver<ServerMessage>,
2023-12-19 21:08:48 -05:00
pub ratelimiters : HashMap<String,RateLimiter>, // used to limit messages sent per channel
2023-12-19 20:38:20 -05:00
// botmodules : Hashmap<botmodule(String),Vec[Enabled(Channel(String)))]>,
twitch_oauth : String,
pub bot_channels : Vec<String>,
/*bot_commands : Vec[BotCommand],
bot_listeners : Vec[Listener],
bot_routines : Vec[Routine],*/
// botactionsdb : botactionsdb:botactions,
// identity : identitymodule,
}
impl BotInstance {
pub fn init() -> BotInstance {
dotenv().ok();
let login_name = "modulatingforcebot".to_owned();
let oauth_token = env::var("access_token").unwrap().to_owned();
/*
Vector of channels to join
*/
let mut botchannels = Vec::new();
for chnl in env::var("bot_channels").unwrap().split(',') {
// println!("(Env Var # {})",chnl);
botchannels.push(String::from(chnl));
}
let config = ClientConfig::new_simple(
StaticLoginCredentials::new(login_name.to_owned(), Some(oauth_token.to_owned()))
);
let (incoming_messages, client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(config);
for chnl 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();
}
2023-12-19 21:08:48 -05:00
let mut b = BotInstance {
2023-12-19 20:38:20 -05:00
prefix : '>',
bot_channel : login_name ,
// tclient : TwitchClient { incoming_messages , client },
2023-12-19 21:08:48 -05:00
incoming_messages : incoming_messages,
client : client,
ratelimiters : HashMap::new(), // used to limit messages sent per channel
2023-12-19 20:38:20 -05:00
// botmodules : Hashmap<botmodule(String),Vec[Enabled(Channel(String)))]>,
twitch_oauth : oauth_token,
bot_channels : botchannels,
/*bot_commands : Vec[BotCommand],
bot_listeners : Vec[Listener],
bot_routines : Vec[Routine],*/
// botactionsdb : botactionsdb:botactions,
// identity : identitymodule,
2023-12-19 21:08:48 -05:00
};
// 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);
2023-12-19 20:38:20 -05:00
}
2023-12-19 21:08:48 -05:00
println!("{:?}",b.ratelimiters);
b
2023-12-19 20:38:20 -05:00
}
}