WIP: Child fn should be able to access BotAction Header #46

Closed
modulatingforce wants to merge 2 commits from botaction-header-accessible-by-child-fn into master
5 changed files with 207 additions and 34 deletions

View file

@ -16,6 +16,7 @@ pub struct ExecBodyParams {
pub bot : BotAR,
pub msg : PrivmsgMessage,
pub parent_act : ActAR ,
pub current_act : ActAR ,
}
@ -28,11 +29,11 @@ impl ExecBodyParams {
let act = &(*parent_act_lock);
match act {
BotAction::C(c) => {
let temp = c.module.clone();
let temp = c.read().await.module.clone();
Some(temp)
},
BotAction::L(l) => {
let temp = l.module.clone();
let temp = l.read().await.module.clone();
Some(temp)
},
_ => None

View file

@ -335,12 +335,12 @@ impl BotInstance {
let mut confirmed_bot_command = false;
let instr = bot.read().await.get_prefix();
if inpt == String::from(instr) + c.command.as_str() {
if inpt == String::from(instr) + c.read().await.command.as_str() {
confirmed_bot_command = true;
}
// [x] prefix + alias
for alias in &c.alias {
for alias in &c.read().await.alias {
let instr = bot.read().await.get_prefix();
if inpt == String::from(instr) + alias.as_str() {
confirmed_bot_command = true;
@ -362,7 +362,7 @@ impl BotInstance {
// [x] Check first if the Module for that Given Command is Enabled or Disabled on the given Channel
let modmgr = Arc::clone(&botlock.botmodules);
let modstatus = modmgr.modstatus(
c.module.clone(),
c.read().await.module.clone(),
Channel(msg.channel_login.to_string())).await;
@ -405,6 +405,7 @@ impl BotInstance {
bot : Arc::clone(&bot),
msg : (*msg).clone(),
parent_act : Arc::clone(&act_clone),
current_act : Arc::clone(&act_clone) ,
};
// When sending a BotMsgTypeNotif, send_botmsg does Roles related validation as required
@ -428,7 +429,7 @@ impl BotInstance {
let eval = {
let mut idlock = id.write().await;
let (permissability, chngrslt) = idlock
.can_user_run_prvmsg(msg, c.required_roles.clone())
.can_user_run_prvmsg(msg, c.read().await.required_roles.clone())
.await;
(permissability, chngrslt)
@ -461,8 +462,8 @@ impl BotInstance {
let params = ExecBodyParams {
bot : Arc::clone(&bot),
msg : (*msg).clone(),
parent_act : Arc::clone(&act_clone),
parent_act : Arc::clone(&act_clone),
current_act : Arc::clone(&act_clone) ,
};
botlock.botmgrs.chat.send_botmsg(super::chat::BotMsgType::Notif(
@ -492,7 +493,7 @@ impl BotInstance {
bot : Arc::clone(&bot),
msg : (*msg).clone(),
parent_act : Arc::clone(&act_clone),
current_act : Arc::clone(&act_clone) ,
};
botlock.botmgrs.chat.send_botmsg(super::chat::BotMsgType::Notif(
@ -513,10 +514,11 @@ impl BotInstance {
);
let a = Arc::clone(&bot);
c.execute(ExecBodyParams {
c.read().await.execute(ExecBodyParams {
bot : a,
msg : msg.clone() ,
parent_act : Arc::clone(&act_clone),
current_act : Arc::clone(&act_clone) ,
}).await;
botlog::trace(
@ -544,7 +546,7 @@ impl BotInstance {
// [x] Check first if the Module for that Given Command is Enabled or Disabled on the given Channel
let modmgr = Arc::clone(&botlock.botmodules);
let modstatus = modmgr.modstatus(
l.module.clone(),
l.read().await.module.clone(),
Channel(msg.channel_login.to_string())).await;
@ -561,10 +563,11 @@ impl BotInstance {
} else {
let a = Arc::clone(&bot);
l.execute(ExecBodyParams {
l.read().await.execute(ExecBodyParams {
bot : a,
msg : msg.clone() ,
parent_act : Arc::clone(&act_clone),
current_act : Arc::clone(&act_clone) ,
} ).await;
}

View file

@ -25,6 +25,7 @@ const OF_CMD_CHANNEL:Channel = Channel(String::new());
use core::panic;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::sync::Arc;
@ -459,19 +460,64 @@ pub enum StatusType {
Disabled(StatusLvl),
}
#[derive(Clone)]
pub enum BotAction {
C(BotCommand),
L(Listener),
R(Routine),
// C(BotCommand),
C(Arc<RwLock<BotCommand>>),
L(Arc<RwLock<Listener>>),
R(Arc<RwLock<Routine>>),
}
impl BotAction {
pub async fn execute(&self, params : ExecBodyParams) {
match self {
BotAction::L(a) => a.execute(params).await,
BotAction::C(a) => a.execute(params).await,
_ => (),
// pub async fn execute(&self, params : ExecBodyParams) {
pub async fn execute(self, params : ExecBodyParams) {
let act_ar = Arc::new(RwLock::new(self));
// let act_lock = act_ar.read().await;
let mut params_adjusted = params.clone();
params_adjusted.current_act = Arc::clone(&act_ar);
if let BotAction::L(a) = &*Arc::clone(&act_ar).read().await {
a.read().await.execute(params_adjusted.clone()).await;
}
if let BotAction::C(a) = &*Arc::clone(&act_ar).read().await {
let cmd_ar = Arc::clone(a);
cmd_ar.read().await.execute(params_adjusted).await;
// cmd_ar.read().await.execute(params.clone()).await;
// (&*Arc::clone(&cmd_ar).blocking_read()).execute(params.clone()).await;
}
if let BotAction::R(_a) = &*Arc::clone(&act_ar).read().await {
//a.read().await.execute(params.clone()).await;
}
// match &*act_lock {
// BotAction::L(a) => {
// // let inner = Arc::clone(a);
// // inner.read().await.execute(params).await
// },
// BotAction::C(b) => {
// // a.read().await.execute(params).await
// // a.read().await.command.clone();
// b.read().await.execute(params).await;
// },
// _ => (),
// }
// match self {
// BotAction::L(a) => a.execute(params).await,
// BotAction::C(a) => {
// a.read().await.execute(params).await
// },
// _ => (),
// }
}
}
@ -493,8 +539,71 @@ pub struct BotCommand {
}
impl BotCommand {
// pub async fn execute(&self, params : ExecBodyParams) {
// pub async fn execute(self, mut params : ExecBodyParams) {
// let self_ar = Arc::new(RwLock::new(self));
// let self_ar1 = Arc::clone(&self_ar);
// let self_lock = self_ar1.read().await;
// let current_act = BotAction::C(self_lock);
// params.current_act = Arc::new(RwLock::new(current_act));
// // (*self.exec_body)(params).await;
// // (*self.exec_body)(params).await;
// (*self_lock.exec_body)(params).await;
// }
// async fn botaction(self) -> BotAction {
// let a =
// BotAction::C(self)
// }
// pub async fn execute(self, params : ExecBodyParams) {
// let a = Arc::new(RwLock::new(self));
// let current_act = BotAction::C(Arc::clone(&a));
// let mut params_clone = params.clone();
// params_clone.current_act = Arc::new(RwLock::new(current_act));
// let alock = a.read().await;
// (*alock.exec_body)(params).await;
// }
// pub async fn execute(&self, params : ExecBodyParams) {
// let a = Arc::new(RwLock::new(*self));
// let current_act = BotAction::C(Arc::clone(&a));
// let mut params_clone = params.clone();
// params_clone.current_act = Arc::new(RwLock::new(current_act));
// let alock = a.read().await;
// (*alock.exec_body)(params).await;
// }
pub async fn execute(&self, params : ExecBodyParams) {
(*self.exec_body)(params).await;
// let current_act = BotAction::C(Arc::new(RwLock::new(self)));
// let mut params_clone = params.clone();
// params_clone.current_act = Arc::new(RwLock::new(current_act));
(self.exec_body)(params).await
// let a = Arc::new(RwLock::new(*self));
// let current_act = BotAction::C(Arc::clone(&a));
// let mut params_clone = params.clone();
// params_clone.current_act = Arc::new(RwLock::new(current_act));
// let alock = a.read().await;
// (*alock.exec_body)(params).await;
}
}
@ -506,7 +615,7 @@ impl BotActionTrait for BotCommand {
async fn add_to_modmgr(self, modmgr: Arc<ModulesManager>) {
modmgr
.add_botaction(self.module.clone(), BotAction::C(self))
.add_botaction(self.module.clone(), BotAction::C(Arc::new(RwLock::new(self))))
.await
}
@ -516,7 +625,7 @@ impl BotActionTrait for BotCommand {
async fn add_core_to_modmgr(self, modmgr: Arc<ModulesManager>) {
modmgr
.add_core_act(self.module.clone(), BotAction::C(self))
.add_core_act(self.module.clone(), BotAction::C(Arc::new(RwLock::new(self))))
.await
}
}
@ -553,7 +662,7 @@ impl BotActionTrait for Listener {
);
modmgr
.add_botaction(self.module.clone(), BotAction::L(self))
.add_botaction(self.module.clone(), BotAction::L(Arc::new(RwLock::new(self))))
.await;
}
@ -563,7 +672,7 @@ impl BotActionTrait for Listener {
async fn add_core_to_modmgr(self, modmgr: Arc<ModulesManager>) {
modmgr
.add_core_act(self.module.clone(), BotAction::L(self))
.add_core_act(self.module.clone(), BotAction::L(Arc::new(RwLock::new(self))))
.await
}
}
@ -1354,13 +1463,14 @@ impl ModulesManager {
// [x] check if given botcommand c.command:String conflicts with any in botactions
if incmd.command.to_lowercase() == dbcmd.command.to_lowercase() {
// if incmd.command.to_lowercase() == dbcmd.command.to_lowercase() {
if incmd.read().await.command.to_lowercase() == dbcmd.read().await.command.to_lowercase() {
// Returning State - with the identified module
return Some(module.clone()); // works
}
for a in &dbcmd.alias {
if incmd.command.to_lowercase() == a.to_lowercase() {
for a in &dbcmd.read().await.alias {
if incmd.read().await.command.to_lowercase() == a.to_lowercase() {
// Returning State - with the identified module
return Some(module.clone()); // works
@ -1369,14 +1479,14 @@ impl ModulesManager {
// [x] Then do the same check except for each c.alias
for inalias in &incmd.alias {
if inalias.to_lowercase() == dbcmd.command.to_lowercase() {
for inalias in &incmd.read().await.alias {
if inalias.to_lowercase() == dbcmd.read().await.command.to_lowercase() {
// Returning State - with the identified module
return Some(module.clone()); // works
}
for a in &dbcmd.alias {
for a in &dbcmd.read().await.alias {
if inalias.to_lowercase() == a.to_lowercase() {
// Returning State - with the identified module

View file

@ -26,7 +26,7 @@ use crate::core::botlog;
use casual_logger::Log;
use crate::core::bot_actions::actions_util;
use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, ModulesManager};
use crate::core::botmodules::{BotAction, BotActionTrait, BotCommand, BotModule, ModulesManager};
use crate::core::identity::UserRole::*;
@ -55,7 +55,66 @@ pub async fn init(mgr: Arc<ModulesManager>) {
// If enabling by defauling at instance level , uncomment the following
// mgr.set_instance_enabled(BotModule(String::from("experiments002"))).await;
// 1. Define the BotAction
let botc1 = BotCommand {
module: BotModule(String::from("experiments002")),
command: String::from("testhelp"), // command call name
alias: vec![
], // String of alternative names
exec_body: actions_util::asyncbox(testhelp),
help: String::from("Help body - Success"),
required_roles: vec![
BotAdmin,
// Mod(OF_CMD_CHANNEL),
// VIP(OF_CMD_CHANNEL),
],
};
// 2. Add the BotAction to ModulesManager
botc1.add_to_modmgr(Arc::clone(&mgr)).await;
}
async fn testhelp(params : ExecBodyParams) {
if params.msg.sender.name.to_lowercase() == "ModulatingForce".to_lowercase()
|| params.msg.sender.name.to_lowercase() == "mzNToRi".to_lowercase()
{
botlog::debug(
"Few Chosen triggered Testhelp!",
Some("experiments02 > testhelp()".to_string()),
Some(&params.msg),
);
let bot = Arc::clone(&params.bot);
let botlock = bot.read().await;
let curract = params.current_act.read().await;
let helptext = match &*curract {
BotAction::C(a) => a.read().await.help.clone(),
BotAction::L(a) => a.read().await.help.clone(),
_ => "No Text Found".to_string()
};
botlock
.botmgrs
.chat
.say(
params.clone().msg.channel_login,
helptext.to_string(),
params.clone(),
).await;
}
Log::flush();
}

View file

@ -39,10 +39,10 @@ pub async fn main() {
let outstr = match &(*act) {
botmodules::BotAction::C(b) => {
format!("bot actions > Command : {}", b.command)
format!("bot actions > Command : {}", b.read().await.command)
}
botmodules::BotAction::L(l) => {
format!("bot actions > Listener : {}", l.name)
format!("bot actions > Listener : {}", l.read().await.name)
}
_ => "Not a valid match??".to_string(),
};