(init) experiment test module
All checks were successful
ci/woodpecker/pr/cargo-checks Pipeline was successful

This commit is contained in:
ModulatingForce 2024-03-26 16:56:30 -04:00
parent 2ba92388a2
commit 4e9316ad49
5 changed files with 413 additions and 14 deletions

View file

@ -5,7 +5,7 @@ use tokio::sync::RwLock;
use crate::core::botinstance::BotInstance; use crate::core::botinstance::BotInstance;
use super::{botmodules::{BotAction, BotModule}, identity::ChatBadge}; use super::{botinstance::Channel, botmodules::{BotAction, BotModule}, identity::ChatBadge};
pub type BotAR = Arc<RwLock<BotInstance>>; pub type BotAR = Arc<RwLock<BotInstance>>;
@ -21,24 +21,63 @@ pub struct ExecBodyParams {
impl ExecBodyParams { impl ExecBodyParams {
pub async fn get_parent_module(&self) -> Option<BotModule> { // pub async fn get_parent_module(&self) -> Option<BotModule> {
pub async fn get_parent_module(&self) -> BotModule {
let parent_act = Arc::clone(&self.parent_act); let parent_act = Arc::clone(&self.parent_act);
let parent_act_lock = parent_act.read().await; let parent_act_lock = parent_act.read().await;
let act = &(*parent_act_lock); let act = &(*parent_act_lock);
match act { match act {
BotAction::C(c) => { BotAction::C(c) => {
let temp = c.module.clone(); // let temp = c.module.clone();
Some(temp) // Some(temp)
c.module.clone()
}, },
BotAction::L(l) => { BotAction::L(l) => {
let temp = l.module.clone(); // let temp = l.module.clone();
Some(temp) // Some(temp)
l.module.clone()
}, },
_ => None BotAction::R(r) => {
// let temp = r.module.clone();
// Some(temp)
r.module.clone()
}
// _ => None
} }
} }
pub async fn get_routine_channel(&self) -> Option<Channel> {
// THIS IS INCORRECT - BELOW MAY BE PULLING THE PARENT BOTACTION
// NOT THE CURRENT BOT ACTION
let parent_act = Arc::clone(&self.parent_act);
let parent_act_lock = parent_act.read().await;
let act = &(*parent_act_lock);
match act {
BotAction::C(_) => {
// let temp = c.module.clone();
// Some(temp)
None
},
BotAction::L(_) => {
// let temp = l.module.clone();
// Some(temp)
// l.module.clone()
None
},
BotAction::R(r) => {
// let temp = r.module.clone();
// Some(temp)
Some(r.channel.clone())
}
// _ => None
}
}
pub fn get_sender(&self) -> String { pub fn get_sender(&self) -> String {
self.msg.sender.name.clone() self.msg.sender.name.clone()
} }

View file

@ -615,9 +615,9 @@ pub enum RoutineAttr {
// #[derive(Debug)] // #[derive(Debug)]
pub struct Routine { pub struct Routine {
pub name : String , pub name : String ,
module : BotModule , // from() can determine this if passed parents_params pub module : BotModule , // from() can determine this if passed parents_params
// pub channel : Option<Channel> , // Routines generally run by Channel ; but can be left None // pub channel : Option<Channel> , // Routines generally run by Channel ; but can be left None
channel : Channel , // Requiring some channel context pub channel : Channel , // Requiring some channel context
exec_body: bot_actions::actions_util::ExecBody, exec_body: bot_actions::actions_util::ExecBody,
// parent_params : Option<ExecBodyParams> , // parent_params : Option<ExecBodyParams> ,
parent_params : ExecBodyParams , parent_params : ExecBodyParams ,
@ -664,6 +664,7 @@ impl Routine {
}) ; }) ;
} }
Err("NOT IMPLEMENTED".to_string()) Err("NOT IMPLEMENTED".to_string())
} }
@ -685,6 +686,7 @@ impl Routine {
// [ ] & Assigns self.join_handle // [ ] & Assigns self.join_handle
let self_rw = Arc::new(RwLock::new(self)); let self_rw = Arc::new(RwLock::new(self));
let mut self_wr_lock = self_rw.write().await; let mut self_wr_lock = self_rw.write().await;
@ -746,15 +748,31 @@ impl Routine {
); );
Log::flush();
})) }))
} }
self_wr_lock.join_handle = innerhelper(self_rw.clone()) ; self_wr_lock.join_handle = innerhelper(self_rw.clone()) ;
return Ok("Successfully Started Routine".to_string()); return Ok("Successfully Started Routine".to_string());
} }
botlog::trace(
format!(
"[ERROR][Routine NOT IMPLEMENTED] {} in {}",
self_wr_lock.name,self_wr_lock.channel.0
)
.as_str(),
Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}",
self_wr_lock.module
)),
Some(&self_wr_lock.parent_params.msg),
);
Log::flush();
Err("NOT IMPLEMENTED".to_string()) Err("NOT IMPLEMENTED".to_string())
} }
@ -767,27 +785,88 @@ impl Routine {
).await; ).await;
} }
pub fn stop(&self) -> Result<String,String> pub async fn stop(&self) -> Result<String,String>
{ {
let self_rw = Arc::new(RwLock::new(self));
let self_lock = self_rw.read().await;
botlog::trace(
format!(
"[ERROR][Routine NOT IMPLEMENTED] {} in {}",
self_lock.name,self_lock.channel.0
)
.as_str(),
Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}",
self_lock.module
)),
Some(&self_lock.parent_params.msg),
);
Log::flush();
Err("NOT IMPLEMENTED".to_string()) Err("NOT IMPLEMENTED".to_string())
} }
pub fn restart( pub async fn restart(
&self, &self,
_force : bool _force : bool
) -> Result<String,String> ) -> Result<String,String>
{ {
// force flag aborts the routine immediately (like cancel()) // force flag aborts the routine immediately (like cancel())
let self_rw = Arc::new(RwLock::new(self));
let self_lock = self_rw.read().await;
botlog::trace(
format!(
"[ERROR][Routine NOT IMPLEMENTED] {} in {}",
self_lock.name,self_lock.channel.0
)
.as_str(),
Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}",
self_lock.module
)),
Some(&self_lock.parent_params.msg),
);
Log::flush();
Err("NOT IMPLEMENTED".to_string()) Err("NOT IMPLEMENTED".to_string())
} }
pub fn cancel(&self) -> Result<String,String> pub async fn cancel(&self) -> Result<String,String>
{ {
// [ ] Likely calls abort() // [ ] Likely calls abort()
// Related : // Related :
// https://docs.rs/tokio/latest/tokio/task/struct.JoinHandle.html#method.abort // https://docs.rs/tokio/latest/tokio/task/struct.JoinHandle.html#method.abort
let self_rw = Arc::new(RwLock::new(self));
let self_lock = self_rw.read().await;
botlog::trace(
format!(
"[ERROR][Routine NOT IMPLEMENTED] {} in {}",
self_lock.name,self_lock.channel.0
)
.as_str(),
Some(format!(
"Routine > start() > (In Tokio Spawn) > {:?}",
self_lock.module
)),
Some(&self_lock.parent_params.msg),
);
Log::flush();
Err("NOT IMPLEMENTED".to_string()) Err("NOT IMPLEMENTED".to_string())
} }

View file

@ -108,7 +108,8 @@ impl Chat {
let botlock = botclone.read().await; let botlock = botclone.read().await;
let modmgr = Arc::clone(&botlock.botmodules); let modmgr = Arc::clone(&botlock.botmodules);
let modstatus = (*modmgr).modstatus( let modstatus = (*modmgr).modstatus(
parent_module.clone().expect("ERROR - Expected a module"), // parent_module.clone().expect("ERROR - Expected a module"),
parent_module.clone(),
Channel(channel_login.clone()) Channel(channel_login.clone())
).await; ).await;
@ -180,7 +181,8 @@ impl Chat {
self.send_botmsg(BotMsgType::Notif( self.send_botmsg(BotMsgType::Notif(
format!("uuh {:?} is disabled on {} : {:?}", format!("uuh {:?} is disabled on {} : {:?}",
parent_module.clone().unwrap(), // parent_module.clone().unwrap(),
parent_module.clone(),
channel_login.clone(), channel_login.clone(),
lvl lvl
), ),

View file

@ -12,6 +12,7 @@ pub use crate::core::botmodules::ModulesManager;
mod experiment001; mod experiment001;
mod experiment002; mod experiment002;
mod experiment003;
// [ ] init() function that accepts bot instance - this is passed to init() on submodules // [ ] init() function that accepts bot instance - this is passed to init() on submodules
@ -21,4 +22,5 @@ pub async fn init(mgr: Arc<ModulesManager>) {
experiment001::init(Arc::clone(&mgr)).await; experiment001::init(Arc::clone(&mgr)).await;
experiment002::init(Arc::clone(&mgr)).await; experiment002::init(Arc::clone(&mgr)).await;
experiment003::init(Arc::clone(&mgr)).await;
} }

View file

@ -0,0 +1,277 @@
/*
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 <modulename>;
- within init(), <modulename>::init(mgr).await
*/
const OF_CMD_CHANNEL:Channel = Channel(String::new());
use casual_logger::Log;
use rand::Rng;
use std::sync::Arc;
use crate::core::bot_actions::ExecBodyParams;
use crate::core::botinstance::Channel;
use crate::core::botlog;
use crate::core::bot_actions::actions_util;
use crate::core::botmodules::{BotAction, BotActionTrait, BotCommand, BotModule, Listener, ModulesManager, Routine, RoutineAttr};
use crate::core::identity::UserRole::*;
use tokio::time::{sleep, Duration};
pub async fn init(mgr: Arc<ModulesManager>) {
// 1. Define the BotAction
let botc1 = BotCommand {
module: BotModule(String::from("experiments003")),
command: String::from("test3"), // command call name
alias: vec![], // String of alternative names
exec_body: actions_util::asyncbox(test3_body),
help: String::from("Test Command tester"),
required_roles: vec![
BotAdmin,
Mod(OF_CMD_CHANNEL),
],
};
// 2. Add the BotAction to ModulesManager
botc1.add_to_modmgr(Arc::clone(&mgr)).await;
}
async fn test3_body(params : ExecBodyParams) {
// println!("testy triggered!"); // NOTE : This test function intends to print (e.g., to stdout) at fn call
botlog::debug(
"testy triggered!",
Some("Experiments003 > test3 command body".to_string()),
Some(&params.msg),
);
/*
Test Routine Start() by :
1. In this single exec body , create a Routine
2. Create a Routine Execution Body
3. Pass the Execution Body & Routine Attributes to create the Routine
4. Start the Routine
5. For RunOnce , we should see it only trigger once, and then complete in the logs
*/
// [x] Get the module from params
let parentmodule = params.get_parent_module().await;
let channel = params.get_routine_channel().await;
let routine_attr = vec![
RoutineAttr::RunOnce
];
let exec_body = actions_util::asyncbox(rtestbody);
let parent_params = params.clone();
async fn rtestbody(params : ExecBodyParams) {
let bot = Arc::clone(&params.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(
&params.msg.clone(),
String::from("Inner Routine Loop Message"),
params.clone()
).await;
}
let a = Routine::from(
"Routine Test".to_string(),
parentmodule,
channel.unwrap(),
routine_attr,
exec_body,
parent_params
);
if let Ok(newr) = a {
let rslt = newr.start().await;
botlog::debug(
format!("TEST3_BODY RESULT : {:?}",
rslt
).as_str(),
Some("experiment003 > test3_body".to_string()),
Some(&params.msg),
);
let bot = Arc::clone(&params.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(
&params.msg,
format!("Routine Result : {:?}",rslt),
params.clone()
).await;
}
Log::flush();
}
async fn good_girl(params : ExecBodyParams) {
// [ ] 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
if params.msg.sender.name.to_lowercase() == "ModulatingForce".to_lowercase()
|| params.msg.sender.name.to_lowercase() == "mzNToRi".to_lowercase()
{
botlog::debug(
"Good Girl Detected > Pausechamp",
Some("experiments > goodgirl()".to_string()),
Some(&params.msg),
);
let rollwin = rand::thread_rng().gen_ratio(1, 10);
if rollwin {
botlog::debug(
"Oh that's a good girl!",
Some("experiments > goodgirl()".to_string()),
Some(&params.msg),
);
let bot = Arc::clone(&params.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(
&params.msg,
String::from("GoodGirl xdd "),
params.clone()
).await;
}
}
}
async fn testy(params : ExecBodyParams) {
println!("testy triggered!"); // NOTE : This test function intends to print (e.g., to stdout) at fn call
botlog::debug(
"testy triggered!",
Some("experiments > testy()".to_string()),
Some(&params.msg),
);
}
async fn babygirl(params : ExecBodyParams) {
println!("babygirl triggered!"); // NOTE : This test function intends to print (e.g., to stdout) at fn call
botlog::debug(
"babygirl triggered!",
Some("experiments > babygirl()".to_string()),
Some(&params.msg),
);
let bot = Arc::clone(&params.bot);
let botlock = bot.read().await;
botlock
.botmgrs
.chat
.say_in_reply_to(
&params.msg,
String::from("16:13 notohh: cafdk"),
params.clone()
).await;
sleep(Duration::from_secs_f64(0.5)).await;
botlock
.botmgrs
.chat
.say_in_reply_to(
&params.msg,
String::from("16:13 notohh: have fun eating princess"),
params.clone()
).await;
sleep(Duration::from_secs_f64(2.0)).await;
botlock
.botmgrs
.chat
.say_in_reply_to(
&params.msg,
String::from("16:13 notohh: baby girl"),
params.clone()
).await;
}
async fn routinelike(params : ExecBodyParams) {
println!("routinelike triggered!"); // NOTE : This test function intends to print (e.g., to stdout) at fn call
botlog::debug(
"routinelike triggered!",
Some("experiments > routinelike()".to_string()),
Some(&params.msg),
);
// spawn an async block that runs independently from others
tokio::spawn( async {
for _ in 0..5 {
println!(">> Innterroutine triggered!");
sleep(Duration::from_secs_f64(5.0)).await;
}
}
);
// lines are executed after in conjunction to the spawn
}