Working BotCommand ExecBodies

This commit is contained in:
ModulatingForce 2024-01-29 04:09:53 -05:00
parent 3585c1ec9b
commit b53047e7cf
3 changed files with 72 additions and 85 deletions

View file

@ -269,16 +269,40 @@ impl BotInstance
async fn listener_main_prvmsg(&mut self,msg:PrivmsgMessage) -> () { async fn listener_main_prvmsg(&mut self,msg:PrivmsgMessage) -> () {
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); // println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
// // [ ] Need to run through all Listener Bodies for Enabled Modules for the context of the message (e.g., ModStatus is Enabled in the context for the channel) // // [ ] Need to run through all Listener Bodies for Enabled Modules for the context of the message (e.g., ModStatus is Enabled in the context for the channel)
for (_m,acts) in &self.botmodules.botactions { for (_m,acts) in &self.botmodules.botactions {
for a in acts { for a in acts {
// if let crate::core::botmodules::BotAction::L(lsnr) = a {
// lsnr.execute(self.chat.clone(),msg.clone()).await; match a {
// }
a.execute(self.chat.clone(), msg.clone()).await; crate::core::botmodules::BotAction::C(c) => {
/*
BotCommand handling -
- Checks if the input message is a prefix with command name or alias
*/
let inpt = msg.message_text.split("\n").next().expect("ERROR during BotCommand");
// [x] Check if a bot command based on ...
// [x] prefix + command
if inpt == self.prefix.to_string() + c.command.as_str() {
c.execute(self.chat.clone(), msg.clone()).await;
}
// [x] prefix + alias
for alias in &c.alias {
if inpt == self.prefix.to_string() + alias.as_str() {
c.execute(self.chat.clone(), msg.clone()).await;
}
}
},
crate::core::botmodules::BotAction::L(l) => l.execute(self.chat.clone(), msg.clone()).await,
_ => (),
}
} }
}; };

View file

@ -58,7 +58,7 @@ pub enum ModStatusType {
pub enum BotAction pub enum BotAction
{ {
//C(BotCommand), C(BotCommand),
L(Listener), L(Listener),
R(Routine), R(Routine),
} }
@ -68,6 +68,7 @@ impl BotAction {
match self { match self {
BotAction::L(a) => a.execute(m,n).await, BotAction::L(a) => a.execute(m,n).await,
BotAction::C(a) => a.execute(m,n).await,
_ => (), _ => (),
} }
@ -81,88 +82,36 @@ pub trait BotActionTrait
} }
// #[derive(Debug)] pub struct BotCommand {
// pub struct BotCommand { pub module : ModType,
// pub module : ModType, pub command : String, // command call name
// pub command : String, // command call name pub alias : Vec<String>, // String of alternative names
// pub alias : Vec<String>, // String of alternative names // bot_prefix : char, // although should be global?
// // bot_prefix : char, // although should be global? pub exec_body : bot_actions::actions_util::ExecBody,
// exec_body : fn(&BotInstance<F>,&PrivmsgMessage), pub help : String,
// pub help : String, }
// }
// impl BotCommand { impl BotCommand
// // pub fn add_to_bot(self, bot:BotInstance) { {
// // // let mut mgr = bot.botmodules; pub async fn execute(&self,m:botinstance::Chat,n:PrivmsgMessage){
// // // let nmod = self.module.clone(); (self.exec_body)(m,n).await;
// // // mgr.add_botaction(nmod, BotAction::C(self)); }
// // let mut mgr = bot.botmodules; }
// // //let nmod = self.module.clone();
// // // mgr.add_botaction(self.module.clone(), BotAction::C(self));
// // self.add_to_modmgr(&mut mgr);
// // }
// // pub fn add_to_modmgr(self, modmgr:&mut ModulesManager) { impl BotActionTrait for BotCommand
// // // // let mut mgr = bot.botmodules; {
// // // // let nmod = self.module.clone(); fn add_to_bot(self, mut bot:BotInstance) {
// // // // mgr.add_botaction(nmod, BotAction::C(self)); let mgr = &mut bot.botmodules;
// // // let mut mgr = modmgr; self.add_to_modmgr(mgr);
// // // //let nmod = self.module.clone(); }
// // // mgr.add_botaction(self.module.clone(), BotAction::C(self));
// // // let mut mgr = modmgr;
// // // //let nmod = self.module.clone();
// // modmgr.add_botaction(self.module.clone(), BotAction::C(self));
// // }
fn add_to_modmgr(self, modmgr:&mut ModulesManager) {
modmgr.add_botaction(self.module.clone(), BotAction::C(self))
}
// } }
// impl BotActionTrait for BotCommand {
// // fn add_to_bot(&self) -> Result<String,Box<dyn Error>> {
// // println!("Calling Add to Bot");
// // Ok(String::from("Hello"))
// // }
// fn add_to_bot(self, bot:BotInstance) {
// // let mut mgr = bot.botmodules;
// // let nmod = self.module.clone();
// // mgr.add_botaction(nmod, BotAction::C(self));
// let mut mgr = bot.botmodules;
// //let nmod = self.module.clone();
// // mgr.add_botaction(self.module.clone(), BotAction::C(self));
// self.add_to_modmgr(&mut mgr);
// }
// fn add_to_modmgr<F>(self, modmgr:&mut ModulesManager<F>) {
// // // let mut mgr = bot.botmodules;
// // // let nmod = self.module.clone();
// // // mgr.add_botaction(nmod, BotAction::C(self));
// // let mut mgr = modmgr;
// // //let nmod = self.module.clone();
// // mgr.add_botaction(self.module.clone(), BotAction::C(self));
// // let mut mgr = modmgr;
// // //let nmod = self.module.clone();
// modmgr.add_botaction(self.module.clone(), BotAction::C(self));
// }
// // => 12.23 - MF: NOTE : The exec body is defined ad module level , not this core level
// // fn exec_body(&self,m:&BotInstance,n:&PrivmsgMessage){
// // // => 12.23 - [ ] #todo requires field
// // // (&self.exec)(String::from("Hello from BotAction Trait"))
// // //self.exec_body(m,n)
// // }
// // fn execute(&self,m:&mut BotInstance,n:&PrivmsgMessage){
// // // => 12.23 - [ ] #todo requires field
// // // (&self.exec)(String::from("Hello from BotAction Trait"))
// // //self.exec_body(m,n)
// // (self.exec_body)(m,n)
// // }
// }
pub mod bot_actions { pub mod bot_actions {

View file

@ -14,7 +14,7 @@
use std::future::Future; use std::future::Future;
use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait, self}; use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait, BotCommand};
use crate::core::botmodules::bot_actions::actions_util; use crate::core::botmodules::bot_actions::actions_util;
use crate::core::botinstance::{self}; use crate::core::botinstance::{self};
@ -33,6 +33,14 @@ pub fn init(mgr:&mut ModulesManager)
// help : String::from("DUPCMD4 tester"), // help : String::from("DUPCMD4 tester"),
// }.add_to_modmgr(mgr); // }.add_to_modmgr(mgr);
BotCommand {
module : BotModule(String::from("experiments 004")),
command : String::from("test"), // command call name
alias : vec![String::from("tester"),String::from("testy")], // String of alternative names
exec_body : actions_util::asyncbox(testy) ,
help : String::from("DUPCMD4 tester"),
}.add_to_modmgr(mgr);
let list1 = Listener { let list1 = Listener {
module : BotModule(String::from("experiments 004")), module : BotModule(String::from("experiments 004")),
@ -50,7 +58,7 @@ pub fn init(mgr:&mut ModulesManager)
async fn good_girl(mut chat:botinstance::Chat,msg:PrivmsgMessage) async fn good_girl(mut chat:botinstance::Chat,msg:PrivmsgMessage)
{ {
println!("In GoodGirl()"); println!("In GoodGirl()");
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); //println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
if msg.sender.name == "ModulatingForce" && msg.message_text.contains("GoodGirl") { if msg.sender.name == "ModulatingForce" && msg.message_text.contains("GoodGirl") {
chat.say_in_reply_to(&msg,String::from("GoodGirl")).await; chat.say_in_reply_to(&msg,String::from("GoodGirl")).await;
@ -58,3 +66,9 @@ async fn good_girl(mut chat:botinstance::Chat,msg:PrivmsgMessage)
} }
async fn testy(mut _chat:botinstance::Chat,_msg:PrivmsgMessage)
{
println!("testy triggered!")
}