From ee0b9ee19661a4821819aedee628c9ad1eae3bab Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Sun, 24 Mar 2024 16:59:50 -0400 Subject: [PATCH] (init) say require parent module --- src/core/botinstance.rs | 11 +- src/core/botmodules.rs | 127 ++++++++++++++++++----- src/core/chat.rs | 15 ++- src/core/identity.rs | 98 +++++++++++++++-- src/custom/experimental/experiment001.rs | 100 +++++++++++++----- src/custom/experimental/experiment002.rs | 74 ++++++++++--- 6 files changed, 344 insertions(+), 81 deletions(-) diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index 5cdc60a..c09050a 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -412,7 +412,10 @@ impl BotInstance { let botlock = bot.read().await; let outstr = format!("sadg Module is disabled : {:?}",a); - botlock.botmgrs.chat.say_in_reply_to(msg, outstr).await; + botlock.botmgrs.chat.say_in_reply_to( + msg, + outstr, + c.module.clone()).await; } return; @@ -451,7 +454,11 @@ impl BotInstance { let botlock = bot.read().await; let outstr = "o7 a Mod. I kneel to serve! pepeKneel ".to_string(); - botlock.botmgrs.chat.say_in_reply_to(msg, outstr).await; + botlock.botmgrs.chat.say_in_reply_to( + msg, + outstr, + c.module.clone(), + ).await; } } diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index 3796d82..87dd243 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -104,6 +104,22 @@ pub async fn init(mgr: Arc) { */ + /* + + Get parent module + + */ + + let params_clone = Arc::clone(¶ms.parent_act); + let actlock = params_clone.read().await; + let act = &(*actlock); + let parent_module = match act { + BotAction::C(c) => Some(&(*c).module), + BotAction::L(l) => Some(&(*l).module), + _ => None, + }; + + // [x] Unwraps arguments from message @@ -172,12 +188,17 @@ pub async fn init(mgr: Arc) { Some("botmodules.rs > cmd_enable()".to_string()), Some(¶ms.msg), ); - - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, outmsg.to_string()) - .await; + + // Only call Say if there is a parent module passed + if parent_module.is_some() { + + + botlock + .botmgrs + .chat + .say_in_reply_to(¶ms.msg, outmsg.to_string() , parent_module.unwrap().clone()) + .await; + } return; @@ -216,11 +237,19 @@ pub async fn init(mgr: Arc) { ChangeResult::Success(a) => format!("YAAY Success : {}",a), }; - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, outmsg.to_string()) - .await; + + // Only call Say if there is a parent module passed + if parent_module.is_some() { + + + botlock + .botmgrs + .chat + .say_in_reply_to(¶ms.msg, outmsg.to_string(),parent_module.unwrap().clone()) + .await; + + } + } @@ -282,6 +311,20 @@ pub async fn init(mgr: Arc) { 3c. , and is -f (forced) , return a Success */ + /* + [x] Get the parent module + */ + + let params_clone = Arc::clone(¶ms.parent_act); + let actlock = params_clone.read().await; + let act = &(*actlock); + let parent_module = match act { + BotAction::C(c) => Some(&(*c).module), + BotAction::L(l) => Some(&(*l).module), + _ => None, + }; + + // [x] Unwraps arguments from message @@ -350,13 +393,30 @@ pub async fn init(mgr: Arc) { Some("botmodules.rs > cmd_disable()".to_string()), Some(¶ms.msg), ); - - botlock + + + // let params_clone = Arc::clone(¶ms.parent_act); + // let actlock = params_clone.read().await; + // let act = &(*actlock); + // let parent_module = match act { + // BotAction::C(c) => Some(&(*c).module), + // BotAction::L(l) => Some(&(*l).module), + // _ => None, + // }; + + + + // Only call Say if there is a parent module passed + if parent_module.is_some() { + + botlock .botmgrs .chat - .say_in_reply_to(¶ms.msg, outmsg.to_string()) + .say_in_reply_to(¶ms.msg, outmsg.to_string(),parent_module.unwrap().clone()) .await; + } + return; } @@ -397,11 +457,35 @@ pub async fn init(mgr: Arc) { ChangeResult::Success(a) => format!("YAAY Success : {}",a), }; - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, outmsg.to_string()) - .await; + + // let actlock = params.parent_act.read().await; + // let act = *actlock; + // let parent_module = match act { + // BotAction::C(c) => Some(c.module), + // BotAction::L(l) => Some(l.module), + // _ => None, + // }; + + + // let params_clone = Arc::clone(¶ms.parent_act); + // let actlock = params_clone.read().await; + // let act = &(*actlock); + // let parent_module = match act { + // BotAction::C(c) => Some(&(*c).module), + // BotAction::L(l) => Some(&(*l).module), + // _ => None, + // }; + + // Only call Say if there is a parent module passed + if parent_module.is_some() { + + botlock + .botmgrs + .chat + .say_in_reply_to(¶ms.msg, outmsg.to_string(),parent_module.unwrap().clone()) + .await; + + } @@ -413,13 +497,8 @@ pub async fn init(mgr: Arc) { #[derive(Debug, Clone)] -// pub enum BotModule { -// BotModule(String), -// } - pub struct BotModule(pub String); - impl PartialEq for BotModule { fn eq(&self, other: &Self) -> bool { let BotModule(name1) = self.clone(); diff --git a/src/core/chat.rs b/src/core/chat.rs index 1ba85e7..d379472 100644 --- a/src/core/chat.rs +++ b/src/core/chat.rs @@ -21,6 +21,8 @@ use crate::core::botlog; use tokio::time::{sleep, Duration}; +use super::botmodules::BotModule; + #[derive(Clone)] pub struct Chat { pub ratelimiters: Arc>>, // used to limit messages sent per channel @@ -54,7 +56,8 @@ impl Chat { - async fn send_botmsg(&self, msginput: BotMsgType<'_>) { + // async fn send_botmsg(&self, msginput: BotMsgType<'_>) { + async fn send_botmsg(&self, msginput: BotMsgType<'_>, _ : BotModule) { /* formats message before sending to TwitchIRC @@ -159,16 +162,18 @@ impl Chat { - pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, outmsg: String) { + // pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, outmsg: String) { + pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, outmsg: String , parent_module : BotModule) { - self.send_botmsg(BotMsgType::SayInReplyTo(msg, outmsg)).await; + self.send_botmsg(BotMsgType::SayInReplyTo(msg, outmsg) , parent_module).await; } - pub async fn say(&self, channel_login: String, message: String) { + // pub async fn say(&self, channel_login: String, message: String) { + pub async fn say(&self, channel_login: String, message: String , parent_module : BotModule) { // more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say - self.send_botmsg(BotMsgType::Say(channel_login.to_lowercase(), message)).await; + self.send_botmsg(BotMsgType::Say(channel_login.to_lowercase(), message), parent_module).await; } async fn _me(&self, _: String, _: String) { diff --git a/src/core/identity.rs b/src/core/identity.rs index 973ebe4..5c2ede2 100644 --- a/src/core/identity.rs +++ b/src/core/identity.rs @@ -12,6 +12,7 @@ use crate::core::bot_actions::BotAR; use crate::core::bot_actions::ExecBodyParams; use crate::core::botinstance::{Channel,ChangeResult}; use crate::core::botlog; +use crate::core::botmodules::BotAction; use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, ModulesManager}; use dotenv::dotenv; @@ -106,6 +107,21 @@ pub async fn init(mgr: Arc) { */ + + /* + [x] Get the parent module + */ + + let params_clone = Arc::clone(¶ms.parent_act); + let actlock = params_clone.read().await; + let act = &(*actlock); + let parent_module = match act { + BotAction::C(c) => Some(&(*c).module), + BotAction::L(l) => Some(&(*l).module), + _ => None, + }; + + // println!("{}",params.msg.message_text); botlog::trace( format!("Twich Message > {}", params.msg.message_text).as_str(), @@ -214,11 +230,20 @@ pub async fn init(mgr: Arc) { Some(¶ms.msg), ); - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, outmsg.to_string()) - .await; + // Only call Say if there is a parent module passed + if parent_module.is_some() { + + + botlock + .botmgrs + .chat + .say_in_reply_to( + ¶ms.msg, + outmsg.to_string(), + parent_module.unwrap().clone()) + .await; + + } botlog::trace( // &format!("End of cmd_promote()"), @@ -281,6 +306,22 @@ pub async fn init(mgr: Arc) { */ + + /* + [x] Get the parent module + */ + + let params_clone = Arc::clone(¶ms.parent_act); + let actlock = params_clone.read().await; + let act = &(*actlock); + let parent_module = match act { + BotAction::C(c) => Some(&(*c).module), + BotAction::L(l) => Some(&(*l).module), + _ => None, + }; + + + // [x] Unwraps arguments from message let (arg1, _arg2) = { @@ -414,11 +455,23 @@ pub async fn init(mgr: Arc) { Some(¶ms.msg), ); - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, outmsg.to_string()) - .await; + + // Only call Say if there is a parent module passed + if parent_module.is_some() { + + botlock + .botmgrs + .chat + .say_in_reply_to( + ¶ms.msg, + outmsg.to_string(), + parent_module.unwrap().clone()) + .await; + + } + + + } let tempcomm = BotCommand { @@ -455,6 +508,20 @@ pub async fn init(mgr: Arc) { */ + /* + [x] Get the parent module + */ + + let params_clone = Arc::clone(¶ms.parent_act); + let actlock = params_clone.read().await; + let act = &(*actlock); + let parent_module = match act { + BotAction::C(c) => Some(&(*c).module), + BotAction::L(l) => Some(&(*l).module), + _ => None, + }; + + let mut argv = params.msg.message_text.split(' '); argv.next(); // Skip the command name @@ -566,7 +633,16 @@ pub async fn init(mgr: Arc) { Some(¶ms.msg), ); - botlock.botmgrs.chat.say_in_reply_to(¶ms.msg, outmsg).await; + if parent_module.is_some() { + + botlock.botmgrs.chat.say_in_reply_to( + ¶ms.msg, + outmsg, + parent_module.unwrap().clone() + ).await; + + } + // [ ] NOTE : After the above, I should receive only the roles in the context of the current channel I received this ideally and maybe BotAdmin ; not outside } diff --git a/src/custom/experimental/experiment001.rs b/src/custom/experimental/experiment001.rs index bcdb9e5..dcd45a2 100644 --- a/src/custom/experimental/experiment001.rs +++ b/src/custom/experimental/experiment001.rs @@ -22,6 +22,7 @@ use crate::core::botlog; use crate::core::bot_actions::actions_util; use crate::core::bot_actions::BotAR; +use crate::core::botmodules::BotAction; use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, Listener, ModulesManager}; use crate::core::identity::UserRole::*; @@ -113,6 +114,19 @@ pub async fn init(mgr: Arc) { // async fn good_girl(bot: BotAR, msg: PrivmsgMessage) { async fn good_girl(params : ExecBodyParams) { + + + let params_clone = Arc::clone(¶ms.parent_act); + let actlock = params_clone.read().await; + let act = &(*actlock); + let parent_module = match act { + BotAction::C(c) => Some(&(*c).module), + BotAction::L(l) => Some(&(*l).module), + _ => None, + }; + + + // [ ] 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) // - More Info : https://rust-random.github.io/rand/rand/trait.Rng.html#method.gen_ratio @@ -140,12 +154,21 @@ pub async fn init(mgr: Arc) { let botlock = bot.read().await; - // uses chat.say_in_reply_to() for the bot controls for messages - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, String::from("GoodGirl xdd ")) - .await; + + if parent_module.is_some() { + + // uses chat.say_in_reply_to() for the bot controls for messages + botlock + .botmgrs + .chat + .say_in_reply_to( + ¶ms.msg, + String::from("GoodGirl xdd "), + parent_module.unwrap().clone() + ).await; + + } + } } } @@ -163,6 +186,22 @@ async fn testy(params : ExecBodyParams) { // async fn babygirl(bot: BotAR, msg: PrivmsgMessage) { async fn babygirl(params : ExecBodyParams) { + + /* + [x] Get the parent module + */ + + let params_clone = Arc::clone(¶ms.parent_act); + let actlock = params_clone.read().await; + let act = &(*actlock); + let parent_module = match act { + BotAction::C(c) => Some(&(*c).module), + BotAction::L(l) => Some(&(*l).module), + _ => None, + }; + + + println!("babygirl triggered!"); // NOTE : This test function intends to print (e.g., to stdout) at fn call botlog::debug( "babygirl triggered!", @@ -175,30 +214,43 @@ async fn babygirl(params : ExecBodyParams) { let botlock = bot.read().await; - // uses chat.say_in_reply_to() for the bot controls for messages - botlock + if parent_module.is_some() { + + // uses chat.say_in_reply_to() for the bot controls for messages + botlock + .botmgrs + .chat + .say_in_reply_to( + ¶ms.msg, + String::from("16:13 notohh: cafdk"), + parent_module.unwrap().clone() + ).await; + + + sleep(Duration::from_secs_f64(0.5)).await; + + botlock .botmgrs .chat - .say_in_reply_to(¶ms.msg, String::from("16:13 notohh: cafdk")) - .await; + .say_in_reply_to( + ¶ms.msg, + String::from("16:13 notohh: have fun eating princess"), + parent_module.unwrap().clone() + ).await; - sleep(Duration::from_secs_f64(0.5)).await; + sleep(Duration::from_secs_f64(2.0)).await; - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, String::from("16:13 notohh: have fun eating princess")) - .await; + botlock + .botmgrs + .chat + .say_in_reply_to( + ¶ms.msg, + String::from("16:13 notohh: baby girl"), + parent_module.unwrap().clone() + ).await; - - sleep(Duration::from_secs_f64(2.0)).await; - - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, String::from("16:13 notohh: baby girl")) - .await; + } } diff --git a/src/custom/experimental/experiment002.rs b/src/custom/experimental/experiment002.rs index 8799f5a..4709394 100644 --- a/src/custom/experimental/experiment002.rs +++ b/src/custom/experimental/experiment002.rs @@ -22,6 +22,7 @@ use crate::core::bot_actions::ExecBodyParams; use crate::core::botinstance::Channel; // use ChType::Channel; use crate::core::botlog; +use crate::core::botmodules::BotAction; use casual_logger::Log; @@ -71,6 +72,24 @@ async fn sayout(params : ExecBodyParams) { */ + + + + /* + [x] Get the parent module + */ + + let params_clone = Arc::clone(¶ms.parent_act); + let actlock = params_clone.read().await; + let act = &(*actlock); + let parent_module = match act { + BotAction::C(c) => Some(&(*c).module), + BotAction::L(l) => Some(&(*l).module), + _ => None, + }; + + + let reply_parent = if let Some(Some(reply)) = params.msg.source.tags.0.get("reply-parent-msg-body") { Some(reply) } else { None } @@ -141,11 +160,19 @@ async fn sayout(params : ExecBodyParams) { ); // return ; - botlock - .botmgrs - .chat - .say_in_reply_to(¶ms.msg, format!("Not a Joined Channel : {}",trgchnl)) - .await; + if parent_module.is_some() { + + + botlock + .botmgrs + .chat + .say_in_reply_to( + ¶ms.msg, + format!("Not a Joined Channel : {}",trgchnl), + parent_module.unwrap().clone() + ).await; + + } } @@ -195,12 +222,21 @@ async fn sayout(params : ExecBodyParams) { outmsg) }; - // uses chat.say_in_reply_to() for the bot controls for messages - botlock - .botmgrs - .chat - .say(trgchnl.to_string(), newoutmsg.to_string()) - .await; + + if parent_module.is_some() { + + + // uses chat.say_in_reply_to() for the bot controls for messages + botlock + .botmgrs + .chat + .say( + trgchnl.to_string(), + newoutmsg.to_string(), + parent_module.unwrap().clone()) + .await; + + } @@ -216,12 +252,20 @@ async fn sayout(params : ExecBodyParams) { let botlock = bot.read().await; - // uses chat.say_in_reply_to() for the bot controls for messages - botlock + + if parent_module.is_some() { + + // uses chat.say_in_reply_to() for the bot controls for messages + botlock .botmgrs .chat - .say_in_reply_to(¶ms.msg, String::from("Invalid arguments")) - .await; + .say_in_reply_to( + ¶ms.msg, + String::from("Invalid arguments"), + parent_module.unwrap().clone() + ).await; + + } },