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;
|
2023-12-20 17:55:46 -05:00
|
|
|
|
use twitch_irc::message::PrivmsgMessage;
|
2023-12-19 20:38:20 -05:00
|
|
|
|
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;
|
|
|
|
|
|
2023-12-19 21:43:03 -05:00
|
|
|
|
use rand::Rng;
|
|
|
|
|
|
2023-12-19 21:08:48 -05:00
|
|
|
|
|
|
|
|
|
//mod sub::ratelimiter;
|
|
|
|
|
|
|
|
|
|
use crate::core::ratelimiter::RateLimiter;
|
2023-12-19 21:43:03 -05:00
|
|
|
|
use crate::core::ratelimiter;
|
2023-12-19 21:08:48 -05:00
|
|
|
|
// use crate::core::ratelimiter;
|
|
|
|
|
|
2023-12-21 00:48:09 -05:00
|
|
|
|
|
|
|
|
|
use crate::core::botmodules;
|
|
|
|
|
use crate::core::botmodules::ModulesManager;
|
|
|
|
|
|
2023-12-20 20:25:20 -05:00
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum ChType {
|
|
|
|
|
Channel(String),
|
2023-12-19 22:21:56 -05:00
|
|
|
|
}
|
2023-12-19 21:08:48 -05:00
|
|
|
|
|
2023-12-20 20:25:20 -05:00
|
|
|
|
|
|
|
|
|
pub use ChType::Channel;
|
|
|
|
|
|
2023-12-20 20:52:20 -05:00
|
|
|
|
pub enum ModType {
|
|
|
|
|
BotModule(String),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub use ModType::BotModule;
|
|
|
|
|
|
|
|
|
|
pub enum EnType {
|
|
|
|
|
Enabled(ChType),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub use EnType::Enabled;
|
|
|
|
|
|
2023-12-21 00:48:09 -05:00
|
|
|
|
// pub enum ModStatusType {
|
|
|
|
|
// Enabled(EnType),
|
|
|
|
|
// Disabled(EnType),
|
|
|
|
|
// Enabled(ModType),
|
|
|
|
|
// Disabled(ModType),
|
|
|
|
|
// }
|
|
|
|
|
|
2023-12-19 20:38:20 -05:00
|
|
|
|
|
|
|
|
|
pub struct BotInstance {
|
|
|
|
|
prefix : char,
|
2023-12-20 20:25:20 -05:00
|
|
|
|
bot_channel : ChType,
|
2023-12-19 20:38:20 -05:00
|
|
|
|
pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>,
|
|
|
|
|
pub incoming_messages : UnboundedReceiver<ServerMessage>,
|
2023-12-20 20:25:20 -05:00
|
|
|
|
pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
|
2023-12-20 20:52:20 -05:00
|
|
|
|
// botmodules : HashMap<ModType,Vec<EnType>>,
|
2023-12-21 00:48:09 -05:00
|
|
|
|
botmodules : ModulesManager,
|
2023-12-19 20:38:20 -05:00
|
|
|
|
twitch_oauth : String,
|
2023-12-20 20:25:20 -05:00
|
|
|
|
pub bot_channels : Vec<ChType>,
|
2023-12-19 20:38:20 -05:00
|
|
|
|
/*bot_commands : Vec[BotCommand],
|
|
|
|
|
bot_listeners : Vec[Listener],
|
|
|
|
|
bot_routines : Vec[Routine],*/
|
|
|
|
|
// botactionsdb : botactionsdb:botactions,
|
|
|
|
|
// identity : identitymodule,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BotInstance {
|
|
|
|
|
|
2023-12-20 17:55:46 -05:00
|
|
|
|
|
2023-12-19 20:38:20 -05:00
|
|
|
|
pub fn init() -> BotInstance {
|
|
|
|
|
dotenv().ok();
|
|
|
|
|
|
2023-12-20 19:12:53 -05:00
|
|
|
|
let login_name = env::var("login_name").unwrap().to_owned();
|
2023-12-19 20:38:20 -05:00
|
|
|
|
let oauth_token = env::var("access_token").unwrap().to_owned();
|
2023-12-20 19:22:45 -05:00
|
|
|
|
let prefix = env::var("prefix").unwrap().to_owned().chars().next().expect("ERROR : when defining prefix");
|
2023-12-19 20:38:20 -05:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Vector of channels to join
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
let mut botchannels = Vec::new();
|
|
|
|
|
|
|
|
|
|
for chnl in env::var("bot_channels").unwrap().split(',') {
|
|
|
|
|
// println!("(Env Var # {})",chnl);
|
2023-12-20 20:25:20 -05:00
|
|
|
|
botchannels.push(Channel(String::from(chnl)));
|
2023-12-19 20:38:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-12-20 20:25:20 -05:00
|
|
|
|
// hashmap for channels and their associated ratelimiters
|
|
|
|
|
let mut ratelimiters = HashMap::new();
|
|
|
|
|
|
|
|
|
|
for Channel(chnl) in &botchannels {
|
|
|
|
|
// For each channel in botchannels
|
|
|
|
|
|
2023-12-19 20:38:20 -05:00
|
|
|
|
client.join(chnl.to_owned()).unwrap();
|
2023-12-20 20:25:20 -05:00
|
|
|
|
|
2023-12-20 20:27:01 -05:00
|
|
|
|
// ratelimiters are a hashmap of channel and a corresponding rate limiter
|
2023-12-20 20:25:20 -05:00
|
|
|
|
let n = RateLimiter::new();
|
|
|
|
|
ratelimiters.insert(Channel(String::from(chnl)),n);
|
2023-12-19 20:38:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 21:08:48 -05:00
|
|
|
|
|
|
|
|
|
|
2023-12-20 20:25:20 -05:00
|
|
|
|
let b = BotInstance {
|
2023-12-20 19:22:45 -05:00
|
|
|
|
prefix : prefix,
|
2023-12-20 20:25:20 -05:00
|
|
|
|
bot_channel : Channel(login_name) ,
|
2023-12-19 21:08:48 -05:00
|
|
|
|
incoming_messages : incoming_messages,
|
|
|
|
|
client : client,
|
2023-12-20 20:25:20 -05:00
|
|
|
|
ratelimiters : ratelimiters, // used to limit messages sent per channel
|
2023-12-20 20:52:20 -05:00
|
|
|
|
// botmodules : HashMap::new(),
|
2023-12-21 00:48:09 -05:00
|
|
|
|
botmodules : ModulesManager::init(),
|
2023-12-19 20:38:20 -05:00
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("{:?}",b.ratelimiters);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b
|
2023-12-19 20:38:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 21:43:03 -05:00
|
|
|
|
|
|
|
|
|
pub async fn run(mut self) -> () {
|
|
|
|
|
|
|
|
|
|
let join_handle = tokio::spawn(async move {
|
2023-12-20 18:49:28 -05:00
|
|
|
|
|
2023-12-19 21:43:03 -05:00
|
|
|
|
while let Some(message) = self.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-20 18:49:28 -05:00
|
|
|
|
|
|
|
|
|
println!("Privmsg section");
|
|
|
|
|
|
|
|
|
|
// b.listener_main_prvmsg(&msg);
|
|
|
|
|
self.listener_main_prvmsg(&msg).await;
|
2023-12-20 17:55:46 -05:00
|
|
|
|
|
2023-12-19 21:43:03 -05:00
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
join_handle.await.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 17:55:46 -05:00
|
|
|
|
|
|
|
|
|
// -----------------
|
|
|
|
|
// PRIVATE FUNCTIONS
|
|
|
|
|
|
|
|
|
|
|
2023-12-20 18:49:28 -05:00
|
|
|
|
async fn listener_main_prvmsg(&mut self,msg:& PrivmsgMessage) -> () {
|
2023-12-20 17:55:46 -05:00
|
|
|
|
|
2023-12-20 18:49:28 -05:00
|
|
|
|
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
|
2023-12-20 17:55:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
|
2023-12-20 20:25:20 -05:00
|
|
|
|
let contextratelimiter = self.ratelimiters
|
|
|
|
|
.get_mut(&Channel(String::from(&msg.channel_login)))
|
|
|
|
|
.expect("ERROR: Issue with Rate limiters");
|
2023-12-20 18:49:28 -05:00
|
|
|
|
// let contextratelimiter = self.ratelimiters.get(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
|
|
|
|
|
|
|
|
|
|
match contextratelimiter.check_limiter() {
|
|
|
|
|
ratelimiter::LimiterResp::Allow => {
|
|
|
|
|
let maxblanks = rand::thread_rng().gen_range(1..=20);
|
|
|
|
|
//let mut outmsg = "GotTrolled ".to_owned();
|
|
|
|
|
let mut outmsg = "annytfLurk ".to_owned();
|
|
|
|
|
|
|
|
|
|
for _i in 1..maxblanks {
|
|
|
|
|
let blankspace: &str = "";
|
|
|
|
|
outmsg.push_str(blankspace);
|
|
|
|
|
}
|
2023-12-20 17:55:46 -05:00
|
|
|
|
|
2023-12-20 18:49:28 -05:00
|
|
|
|
// client.say_in_reply_to(&msg,outmsg).await.unwrap();
|
|
|
|
|
self.client.say_in_reply_to(msg,outmsg).await.unwrap();
|
|
|
|
|
println!("(#{}) > {}", msg.channel_login, "rate limit counter increase");
|
|
|
|
|
contextratelimiter.increment_counter();
|
|
|
|
|
println!("{:?}",self.ratelimiters);
|
|
|
|
|
},
|
|
|
|
|
ratelimiter::LimiterResp::Skip => {
|
|
|
|
|
(); // do nothing otherwise
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("End of Separate Listener Main prvmsg");
|
2023-12-20 17:55:46 -05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|