Custom Exec Bodies for BotCommand and Listeners #9
3 changed files with 174 additions and 17 deletions
|
@ -151,7 +151,7 @@ pub struct BotInstance {
|
|||
//pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>,
|
||||
pub incoming_messages : UnboundedReceiver<ServerMessage>,
|
||||
// pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
|
||||
chat : Chat,
|
||||
pub chat : Chat,
|
||||
// botmodules : HashMap<ModType,Vec<EnType>>,
|
||||
pub botmodules : ModulesManager,
|
||||
twitch_oauth : String,
|
||||
|
|
|
@ -39,6 +39,7 @@ pub enum ChType {
|
|||
|
||||
|
||||
pub use ChType::Channel;
|
||||
use twitch_irc::message::PrivmsgMessage;
|
||||
|
||||
use crate::core::botinstance::{self, BotInstance};
|
||||
|
||||
|
@ -63,18 +64,61 @@ pub enum BotAction {
|
|||
L(Listener),
|
||||
R(Routine),
|
||||
}
|
||||
|
||||
|
||||
pub trait BotActionTrait {
|
||||
// async fn execute(&self,m:&mut BotInstance,n:&PrivmsgMessage);
|
||||
// fn add_to_bot(self, bot:BotInstance) -> Result<String,Box<dyn Error>>;
|
||||
fn add_to_bot(self, bot:BotInstance);
|
||||
fn add_to_modmgr(self, modmgr:&mut ModulesManager);
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BotCommand {
|
||||
pub module : ModType,
|
||||
pub command : String, // command call name
|
||||
pub alias : Vec<String>, // String of alternative names
|
||||
// bot_prefix : char, // although should be global?
|
||||
// exec_body : fn,
|
||||
exec_body : fn(&BotInstance,&PrivmsgMessage),
|
||||
pub help : String,
|
||||
}
|
||||
|
||||
impl BotCommand {
|
||||
pub fn add_to_bot(self, bot:BotInstance) {
|
||||
// pub 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);
|
||||
// }
|
||||
|
||||
|
||||
// pub fn add_to_modmgr(self, modmgr:&mut ModulesManager) {
|
||||
// // // 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));
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
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));
|
||||
|
@ -85,8 +129,7 @@ impl BotCommand {
|
|||
self.add_to_modmgr(&mut mgr);
|
||||
}
|
||||
|
||||
|
||||
pub fn add_to_modmgr(self, modmgr:&mut ModulesManager) {
|
||||
fn add_to_modmgr(self, modmgr:&mut ModulesManager) {
|
||||
// // let mut mgr = bot.botmodules;
|
||||
// // let nmod = self.module.clone();
|
||||
// // mgr.add_botaction(nmod, BotAction::C(self));
|
||||
|
@ -98,17 +141,97 @@ impl BotCommand {
|
|||
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)
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Listener {
|
||||
module : ModType,
|
||||
name : String,
|
||||
// exec_body : fn,
|
||||
help : String
|
||||
pub struct Listener {
|
||||
pub module : ModType,
|
||||
pub name : String,
|
||||
pub exec_body : fn(&mut BotInstance,&PrivmsgMessage) ,
|
||||
pub help : String
|
||||
}
|
||||
|
||||
impl Listener {
|
||||
|
||||
async 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)
|
||||
}
|
||||
}
|
||||
|
||||
impl BotActionTrait for Listener {
|
||||
// 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(self, modmgr:&mut ModulesManager) {
|
||||
// // 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::L(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)
|
||||
// }
|
||||
|
||||
|
||||
// // => 12.23 - MF : There are issues trying to define async at trait level
|
||||
// async fn execute(&self,m:&mut BotInstance,n:&PrivmsgMessage) -> Result<>{
|
||||
// // => 12.23 - [ ] #todo requires field
|
||||
// // (&self.exec)(String::from("Hello from BotAction Trait"))
|
||||
// //self.exec_body(m,n)
|
||||
// (self.exec_body)(m,n)
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Routine {}
|
||||
|
||||
|
|
|
@ -12,22 +12,56 @@
|
|||
// mod crate::modules;
|
||||
//use crate::modules;
|
||||
|
||||
use crate::core::botmodules::{ModulesManager,BotCommand,BotModule};
|
||||
use crate::core::botmodules::{ModulesManager,BotCommand,Listener,BotModule,BotActionTrait};
|
||||
|
||||
use crate::core::botinstance::BotInstance;
|
||||
use twitch_irc::message::PrivmsgMessage;
|
||||
|
||||
pub fn init(mgr:&mut ModulesManager) -> () {
|
||||
|
||||
|
||||
BotCommand {
|
||||
// BotCommand {
|
||||
// module : BotModule(String::from("experiments 004")),
|
||||
// command : String::from("DUPCMD4"), // command call name
|
||||
// alias : vec![String::from("DUPALIAS4A"),String::from("DUPALIAS4B")], // String of alternative names
|
||||
// // bot_prefix : char, // although should be global?
|
||||
// // exec_body : fn,
|
||||
// help : String::from("DUPCMD4 tester"),
|
||||
// }.add_to_modmgr(mgr);
|
||||
|
||||
|
||||
Listener {
|
||||
module : BotModule(String::from("experiments 004")),
|
||||
command : String::from("DUPCMD4"), // command call name
|
||||
alias : vec![String::from("DUPALIAS4A"),String::from("DUPALIAS4B")], // String of alternative names
|
||||
// bot_prefix : char, // although should be global?
|
||||
// exec_body : fn,
|
||||
help : String::from("DUPCMD4 tester"),
|
||||
name : String::from("GoodGirl Listener"),
|
||||
exec_body : good_girl,
|
||||
help : String::from("")
|
||||
}.add_to_modmgr(mgr);
|
||||
|
||||
println!("At Experiments module");
|
||||
|
||||
|
||||
();
|
||||
}
|
||||
|
||||
|
||||
// async fn good_girl(bot:&mut BotInstance,msg:&PrivmsgMessage) {
|
||||
// println!("In GoodGirl()");
|
||||
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
|
||||
|
||||
// if msg.sender.name == "ModulatingForce" && msg.message_text.contains("GoodGirl") {
|
||||
// bot.chat.say_in_reply_to(msg,String::from("GoodGirl"));
|
||||
// }
|
||||
|
||||
// ()
|
||||
// }
|
||||
|
||||
async fn good_girl(bot:&mut BotInstance,msg:&PrivmsgMessage) {
|
||||
println!("In GoodGirl()");
|
||||
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
|
||||
|
||||
if msg.sender.name == "ModulatingForce" && msg.message_text.contains("GoodGirl") {
|
||||
bot.chat.say_in_reply_to(msg,String::from("GoodGirl")).await;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue