From a2747b1ad51f843fab9dad85d0d8acc8d2265ad3 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Sat, 2 Mar 2024 09:44:09 -0500 Subject: [PATCH] (cont) comments & cleanup (cont) cont --- src/core.rs | 5 --- src/core/botinstance.rs | 52 +++++++--------------- src/core/ratelimiter.rs | 10 ++--- src/main.rs | 89 ++++++++++++------------------------- src/modules.rs | 11 ++--- src/modules/experiments.rs | 91 ++++++++++++++++---------------------- 6 files changed, 89 insertions(+), 169 deletions(-) diff --git a/src/core.rs b/src/core.rs index 7027176..ef71070 100644 --- a/src/core.rs +++ b/src/core.rs @@ -2,8 +2,3 @@ pub mod botinstance; pub mod botmodules; pub mod identity; pub mod ratelimiter; - -// pub fn init() -> () -// { -// println!("I was here"); -// } diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index 3593861..a00f6e1 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -1,51 +1,29 @@ -// use futures::lock::Mutex; -use tokio::sync::mpsc::UnboundedReceiver; -use tokio::sync::RwLock; -use twitch_irc::login::StaticLoginCredentials; -use twitch_irc::message::PrivmsgMessage; -use twitch_irc::message::ServerMessage; -use twitch_irc::transport::tcp::TCPTransport; -use twitch_irc::transport::tcp::TLS; -use twitch_irc::ClientConfig; -use twitch_irc::SecureTCPTransport; -use twitch_irc::TwitchIRCClient; -// use std::borrow::Borrow; -use dotenv::dotenv; -// use std::borrow::BorrowMut; -// use std::boxed; -// use std::cell::Ref; -use std::env; +use std::sync::Arc; +use std::env; use std::collections::HashMap; +use tokio::sync::mpsc::UnboundedReceiver; +use tokio::sync::{RwLock,Mutex}; + +use twitch_irc::login::StaticLoginCredentials; +use twitch_irc::message::{PrivmsgMessage,ServerMessage}; +use twitch_irc::transport::tcp::{TCPTransport,TLS}; +use twitch_irc::{ClientConfig,SecureTCPTransport,TwitchIRCClient}; + +use dotenv::dotenv; + +use casual_logger::Log; + use rand::Rng; -// Important to use tokios Mutex here since std Mutex doesn't work with async functions -use tokio::sync::Mutex; - -// use crate::core::botmodules::BotAction; use crate::core::ratelimiter; use crate::core::ratelimiter::RateLimiter; -// use crate::core::botmodules; use crate::core::botmodules::ModulesManager; +use crate::core::botmodules::bot_actions::actions_util::BotAR; use crate::core::identity::{ChangeResult, IdentityManager, Permissible}; -// use std::cell::RefCell; -// use std::rc::Rc; -use std::sync::Arc; -// use futures::lock::Mutex; - -// use std::pin::Pin; - -//use std::borrow::Borrow; -// use core::borrow::Borrow; - -// pub type BotAR = Arc>; -use super::botmodules::bot_actions::actions_util::BotAR; - -// use casual_logger::{Level, Log}; -use casual_logger::Log; pub mod botlog { diff --git a/src/core/ratelimiter.rs b/src/core/ratelimiter.rs index c8fdc12..5e99a5a 100644 --- a/src/core/ratelimiter.rs +++ b/src/core/ratelimiter.rs @@ -1,8 +1,8 @@ -use std::time::Instant; - const TIME_THRESHOLD_S: u64 = 30; const MSG_THRESHOLD: u32 = 20; +use std::time::Instant; + #[derive(Debug, Clone)] pub struct RateLimiter { timer: Instant, @@ -12,6 +12,7 @@ pub struct RateLimiter { pub enum LimiterResp { Allow, // when it's evaluated to be within limits Skip, // as outside of rate limits + // Enqueue, // [FUTURE] } impl Default for RateLimiter { @@ -30,16 +31,13 @@ impl RateLimiter { pub fn check_limiter(&mut self) -> LimiterResp { if self.timer.elapsed().as_secs() >= TIME_THRESHOLD_S { - // # [x] elapsed >= TIME_THRESHOLD_S self.timer = Instant::now(); self.msgcounter = 0; LimiterResp::Allow } else if self.msgcounter < MSG_THRESHOLD { - // # [x] elapsed < TIME_THRESHOLD_S && msgcounter < MSG_THRESHOLD LimiterResp::Allow - // } else if self.msgcounter >= MSG_THRESHOLD { } else { - // # [x] elapsed < TIME_THRESHOLD_S && msgcounter >= MSG_THRESHOLD + // when elapsed() < TIME_THRESHOLD_S && msgcounter >= MSG_THRESHOLD LimiterResp::Skip } } diff --git a/src/main.rs b/src/main.rs index 0069c1e..1e53b43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,14 @@ -// pub mod core; -// pub mod modules; -//use myLib; -//pub mod lib; -// use std::process::Output; - -// use crate::core::botinstance::ArcBox; -// use bot_lib::core::botinstance::ArcBox; - -use bot_lib::core::botinstance::{self, BotInstance}; -// use core::botinstance::{self,BotInstance}; - -use casual_logger::Extension; use std::sync::Arc; use tokio::sync::RwLock; + +use casual_logger::{Extension, Level, Log}; + +use bot_lib::core::botinstance::{BotInstance,botlog}; +use bot_lib::core::botmodules; + pub type BotAR = Arc>; -use casual_logger::{Level, Log}; - -// Cweamcat can cweam on me - Forcen +// God I love anime girls #[tokio::main] pub async fn main() { @@ -27,59 +18,37 @@ pub async fn main() { let bot = BotInstance::init().await; - // Log::debug("Checking bot actions"); - botinstance::botlog::debug("Checking bot actions", Some("main()".to_string()), None); - let a = Arc::clone(&bot.botmodules.botactions); - let a = a.read().await; - // let a = *a; + { + botlog::debug("Reading bot actions", Some("main()".to_string()), None); - // for (_, acts) in &*a { - for acts in (*a).values() { - for act in acts { - match act { - bot_lib::core::botmodules::BotAction::C(b) => { - // println!("bot actiions: {}",b.command) - // Log::info(&format!("bot actions: {}",b.command)); - botinstance::botlog::info( - &format!("bot actions: {}", b.command), - Some("main()".to_string()), - None, - ); - } - bot_lib::core::botmodules::BotAction::L(l) => { - // println!("bot actiions: {}",l.name) - // Log::info(&format!("bot actions: {}",l.name)); - botinstance::botlog::info( - &format!("bot actions: {}", l.name), - Some("main()".to_string()), - None, - ); - } - _ => { - // println!("Not a valid match??") - // Log::info("Not a valid match??"); - botinstance::botlog::info( - "Not a valid match??", - Some("main()".to_string()), - None, - ); - } + let actsdb = Arc::clone(&bot.botmodules.botactions); + let actsdb_lock = actsdb.read().await; + + for acts in (*actsdb_lock).values() { + for act in acts { + let outstr = match act { + botmodules::BotAction::C(b) => { + format!("bot actions: {}", b.command) + } + botmodules::BotAction::L(l) => { + format!("bot actions: {}", l.name) + } + _ => "Not a valid match??".to_string(), + }; + + botlog::info(outstr.as_str(), Some("main()".to_string()), None); } } } - // println!("Starting runner.."); - // Log::notice("Starting Bot Runner"); - botinstance::botlog::notice("Starting Bot Runner", Some("main()".to_string()), None); + botlog::notice("Starting Bot Runner", Some("main()".to_string()), None); println!("Starting Bot Runner"); Log::flush(); bot.runner().await; - // println!("ERROR : EXIT Game loop"); - // let msg = Log::fatal("ERROR : EXIT Game loop"); - // panic!("{}",Log::fatal("ERROR : EXIT Game loop")); - let a = botinstance::botlog::fatal("ERROR : EXIT Game loop", Some("main()".to_string()), None); - panic!("{}", a); + let pstr = + botlog::fatal("ERROR : EXIT Game loop", Some("main()".to_string()), None); + panic!("{}", pstr); } diff --git a/src/modules.rs b/src/modules.rs index 38c52f3..c9eeaa9 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -4,15 +4,11 @@ */ -//mod crate::core::botmodules; -// use crate::core::botmodules; -pub use crate::core::botmodules::ModulesManager; - -// use crate::core::botinstance; -pub use crate::core::botinstance::BotInstance; -// use futures::lock::Mutex; use std::sync::Arc; +pub use crate::core::botinstance::BotInstance; +pub use crate::core::botmodules::ModulesManager; + // [ ] Load submodules mod experiments; @@ -25,5 +21,4 @@ pub async fn init(mgr: Arc) { experiments::init(mgr).await - //(); } diff --git a/src/modules/experiments.rs b/src/modules/experiments.rs index 7c792ab..80b5a7d 100644 --- a/src/modules/experiments.rs +++ b/src/modules/experiments.rs @@ -1,48 +1,32 @@ /* - Submodules - - - - should have definitions of BotAction that will be added to a bit - - therefore, will be defined in modules.rs file - - will define one init(&BotInstance) take within the module that will contain : - - BotAction definitions that each call &BotInstance module manager to add itself + Custom Modules - + Usage : + [ ] within the file's init(), define BotActions & Load them into the ModulesManager + [ ] Define Execution Bodies for these BotActions + [ ] Afterwards, add the following to parent modules.rs file + - mod ; + - within init(), ::init(mgr).await + */ -// mod crate::modules; -//use crate::modules; -// use std::future::Future; +use std::sync::Arc; +use rand::Rng; + +use twitch_irc::message::PrivmsgMessage; + +use crate::core::botinstance::botlog; use crate::core::botmodules::bot_actions::actions_util::{self, BotAR}; use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, Listener, ModulesManager}; -// use crate::core::botinstance::{self, BotInstance, ChType}; -use crate::core::botinstance; -// use futures::lock::Mutex; -use twitch_irc::message::PrivmsgMessage; - use crate::core::identity; -use rand::Rng; - -// use std::rc::Rc; - -use std::sync::Arc; - -// pub fn init(mgr:&mut ModulesManager) pub async fn init(mgr: Arc) { - // BotCommand { - // module : BotModule(String::from("experiments 004")), - // command : String::from("test1"), // command call name - // alias : vec![String::from("tester1"),String::from("testy1")], // String of alternative names - // exec_body : actions_util::asyncbox(testy) , - // help : String::from("DUPCMD4 tester"), - // required_roles : vec![ - // //identity::UserRole::Mod(ChType::Channel(String::new())), - // identity::UserRole::SupMod(ChType::Channel(String::new())) - // ], - // }.add_to_modmgr(mgr); + + // 1. Define the BotAction let botc1 = BotCommand { module: BotModule(String::from("experiments001")), command: String::from("test1"), // command call name @@ -52,8 +36,11 @@ pub async fn init(mgr: Arc) { required_roles: vec![identity::UserRole::BotAdmin], }; + // 2. Add the BotAction to ModulesManager botc1.add_to_modmgr(Arc::clone(&mgr)).await; + + // 1. Define the BotAction let list1 = Listener { module: BotModule(String::from("experiments001")), name: String::from("GoodGirl Listener"), @@ -61,19 +48,12 @@ pub async fn init(mgr: Arc) { help: String::from(""), }; + // 2. Add the BotAction to ModulesManager list1.add_to_modmgr(Arc::clone(&mgr)).await; + } async fn good_girl(bot: BotAR, msg: PrivmsgMessage) { - // println!("In GoodGirl() Listener"); - // Change below from debug to trace if required later - botinstance::botlog::debug( - "In GoodGirl() Listener", - Some("experiments > goodgirl()".to_string()), - Some(&msg), - ); - - //println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); // [ ] Uses gen_ratio() to output bool based on a ratio probability . // - For example gen_ratio(2,3) is 2 out of 3 or 0.67% (numerator,denomitator) @@ -81,38 +61,43 @@ async fn good_girl(bot: BotAR, msg: PrivmsgMessage) { if msg.sender.name.to_lowercase() == "ModulatingForce".to_lowercase() || msg.sender.name.to_lowercase() == "mzNToRi".to_lowercase() - // && msg.message_text.contains("GoodGirl") { - // chat.say_in_reply_to(&msg,String::from("GoodGirl")).await; - //if rng.gen_ratio(1,5) { - // println!("In GoodGirl() > Pausechamp"); - botinstance::botlog::debug( - "In GoodGirl() > Pausechamp", + + botlog::debug( + "Good Girl Detected > Pausechamp", Some("experiments > goodgirl()".to_string()), Some(&msg), ); + + let rollwin = rand::thread_rng().gen_ratio(1, 8); + if rollwin { - // println!("In GoodGirl() > Win"); - botinstance::botlog::debug( - "In GoodGirl() > Win", + + botlog::debug( + "Oh that's a good girl!", Some("experiments > goodgirl()".to_string()), Some(&msg), ); - let a = Arc::clone(&bot); - let botlock = a.read().await; + + let bot = Arc::clone(&bot); + + let botlock = bot.read().await; + + // uses chat.say_in_reply_to() for the bot controls for messages botlock .botmgrs .chat .say_in_reply_to(&msg, String::from("GoodGirl xdd ")) .await; + } } } async fn testy(mut _chat: BotAR, msg: PrivmsgMessage) { println!("testy triggered!"); // NOTE : This test function intends to print (e.g., to stdout) at fn call - botinstance::botlog::debug( + botlog::debug( "testy triggered!", Some("experiments > testy()".to_string()), Some(&msg),