forcebot_rs/src/main.rs

146 lines
4.9 KiB
Rust
Raw Normal View History

2023-10-22 08:35:09 -04:00
use twitch_irc::login::StaticLoginCredentials;
use twitch_irc::ClientConfig;
use twitch_irc::SecureTCPTransport;
use twitch_irc::TwitchIRCClient;
use twitch_irc::message::ServerMessage;
use std::env;
2023-12-19 00:34:21 -05:00
// use std::time::Instant;
use rand::Rng;
use dotenv::dotenv;
// mod helpers;
2023-12-19 00:34:21 -05:00
use std::collections::HashMap;
2023-12-19 21:08:48 -05:00
// mod ratelimiter;
// use ratelimiter::RateLimiter;
2023-12-19 00:34:21 -05:00
2023-12-19 20:38:20 -05:00
pub mod core;
use crate::core::botinstance::BotInstance;
2023-12-19 21:08:48 -05:00
// use crate::core::ratelimiter::RateLimiter;
use crate::core::ratelimiter;
2023-12-19 20:38:20 -05:00
2023-10-22 08:35:09 -04:00
#[tokio::main]
pub async fn main() {
2023-12-19 20:38:20 -05:00
// dotenv().ok();
2023-12-19 20:38:20 -05:00
// let login_name = "modulatingforcebot".to_owned();
// let oauth_token = env::var("access_token").unwrap().to_owned();
2023-10-22 08:35:09 -04:00
2023-12-19 20:38:20 -05:00
// /*
// Vector of channels to join
// */
2023-12-19 20:38:20 -05:00
// let mut botchannels = Vec::new();
// /* botchannels.push(String::from("modulatingforcebot"));
// botchannels.push(String::from("notohh"));
// botchannels.push(String::from("modulatingforce"));
// botchannels.push(String::from("secondsocksan"));
// */
2023-12-19 20:38:20 -05:00
// for chnl in env::var("bot_channels").unwrap().split(',') {
// // println!("(Env Var # {})",chnl);
// botchannels.push(String::from(chnl));
// }
2023-12-19 20:38:20 -05:00
// let config = ClientConfig::new_simple(
// StaticLoginCredentials::new(login_name, Some(oauth_token))
// );
// let (mut incoming_messages, client) =
// TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(config);
2023-10-22 08:35:09 -04:00
// client.join("modulatingforcebot".to_owned()).unwrap();
// client.say("modulatingforcebot".to_owned(), "Connected!".to_owned()).await.unwrap();
2023-12-19 20:38:20 -05:00
let mut bot = BotInstance::init();
// 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();
// }
// Adding rate limit functionality to be under : https://dev.twitch.tv/docs/irc/#rate-limits
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();
2023-12-19 00:34:21 -05:00
2023-12-19 21:08:48 -05:00
// for chnl in bot.bot_channels {
// let n = RateLimiter::new();
// ratelimiters.insert(chnl.to_owned(),n);
// }
2023-12-19 21:08:48 -05:00
// println!("{:?}",ratelimiters);
2023-10-22 08:35:09 -04:00
let join_handle = tokio::spawn(async move {
2023-12-19 20:38:20 -05:00
// while let Some(message) = incoming_messages.recv().await {
while let Some(message) = bot.incoming_messages.recv().await {
// Below can be used to debug if I want to capture all messages
// println!("Received message: {:?}", message);
match message {
ServerMessage::Notice(msg) => {
if let Some(chnl) = msg.channel_login {
println!("NOTICE : (#{}) {}", chnl, msg.message_text);
}
}
ServerMessage::Privmsg(msg) => {
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
2023-12-19 00:34:21 -05:00
2023-12-19 21:08:48 -05:00
// 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");
2023-12-19 00:34:21 -05:00
match contextratelimiter.check_limiter() {
ratelimiter::LimiterResp::Allow => {
let maxblanks = rand::thread_rng().gen_range(1..=5);
2023-12-19 19:30:08 -05:00
//let mut outmsg = "GotTrolled ".to_owned();
let mut outmsg = "annytfLurk ".to_owned();
2023-12-19 00:34:21 -05:00
for _i in 1..maxblanks {
let blankspace: &str = "󠀀";
outmsg.push_str(blankspace);
}
2023-12-19 20:38:20 -05:00
// client.say_in_reply_to(&msg,outmsg).await.unwrap();
bot.client.say_in_reply_to(&msg,outmsg).await.unwrap();
2023-12-19 00:34:21 -05:00
println!("(#{}) > {}", msg.channel_login, "rate limit counter increase");
contextratelimiter.increment_counter();
2023-12-19 21:08:48 -05:00
println!("{:?}",bot.ratelimiters);
2023-12-19 00:34:21 -05:00
},
ratelimiter::LimiterResp::Skip => {
(); // do nothing otherwise
}
}
},
ServerMessage::Whisper(msg) => {
println!("(w) {}: {}", msg.sender.name, msg.message_text);
},
ServerMessage::Join(msg) => {
println!("JOINED: {}", msg.channel_login);
},
ServerMessage::Part(msg) => {
println!("PARTED: {}", msg.channel_login);
},
_ => {}
}
2023-10-22 08:35:09 -04:00
}
});
2023-10-22 08:35:09 -04:00
join_handle.await.unwrap();
}