[init] introducing botinstance module

This commit is contained in:
ModulatingForce 2023-12-19 20:38:20 -05:00
parent 142b620720
commit bf88022eac
3 changed files with 122 additions and 29 deletions

1
src/core.rs Normal file
View file

@ -0,0 +1 @@
pub mod botinstance;

81
src/core/botinstance.rs Normal file
View file

@ -0,0 +1,81 @@
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;
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
// 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();
}
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
// 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,
}
}
}

View file

@ -17,47 +17,56 @@ use std::collections::HashMap;
mod ratelimiter;
use ratelimiter::RateLimiter;
pub mod core;
use crate::core::botinstance::BotInstance;
#[tokio::main]
pub async fn main() {
dotenv().ok();
// dotenv().ok();
let login_name = "modulatingforcebot".to_owned();
let oauth_token = env::var("access_token").unwrap().to_owned();
// let login_name = "modulatingforcebot".to_owned();
// let oauth_token = env::var("access_token").unwrap().to_owned();
/*
Vector of channels to join
*/
// /*
// Vector of channels to join
// */
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"));
*/
// 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"));
// */
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));
// }
let config = ClientConfig::new_simple(
StaticLoginCredentials::new(login_name, Some(oauth_token))
);
let (mut incoming_messages, client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(config);
// let config = ClientConfig::new_simple(
// StaticLoginCredentials::new(login_name, Some(oauth_token))
// );
// let (mut incoming_messages, client) =
// TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(config);
// client.join("modulatingforcebot".to_owned()).unwrap();
// client.say("modulatingforcebot".to_owned(), "Connected!".to_owned()).await.unwrap();
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();
}
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
@ -65,7 +74,7 @@ pub async fn main() {
// ratelimiters are a hashmap of channel and a corresponding rate limiter
let mut ratelimiters:HashMap<String,RateLimiter> = HashMap::new();
for chnl in &botchannels {
for chnl in bot.bot_channels {
let n = RateLimiter::new();
ratelimiters.insert(chnl.to_owned(),n);
}
@ -73,7 +82,8 @@ pub async fn main() {
println!("{:?}",ratelimiters);
let join_handle = tokio::spawn(async move {
while let Some(message) = incoming_messages.recv().await {
// 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);
@ -100,7 +110,8 @@ pub async fn main() {
outmsg.push_str(blankspace);
}
client.say_in_reply_to(&msg,outmsg).await.unwrap();
// client.say_in_reply_to(&msg,outmsg).await.unwrap();
bot.client.say_in_reply_to(&msg,outmsg).await.unwrap();
println!("(#{}) > {}", msg.channel_login, "rate limit counter increase");
contextratelimiter.increment_counter();
println!("{:?}",ratelimiters);