Custom Exec Bodies for BotCommand and Listeners #9
5 changed files with 175 additions and 74 deletions
|
@ -145,9 +145,9 @@ impl Chat {
|
|||
|
||||
|
||||
|
||||
pub struct BotInstance<F>
|
||||
where
|
||||
F: std::future::Future + ?Sized,
|
||||
pub struct BotInstance
|
||||
// where
|
||||
// F: std::future::Future + ?Sized,
|
||||
{
|
||||
prefix : char,
|
||||
bot_channel : ChType,
|
||||
|
@ -156,7 +156,7 @@ where
|
|||
// pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
|
||||
pub chat : Chat,
|
||||
// botmodules : HashMap<ModType,Vec<EnType>>,
|
||||
pub botmodules : ModulesManager<F>,
|
||||
pub botmodules : ModulesManager,
|
||||
twitch_oauth : String,
|
||||
pub bot_channels : Vec<ChType>,
|
||||
/*bot_commands : Vec[BotCommand],
|
||||
|
@ -168,15 +168,16 @@ where
|
|||
|
||||
|
||||
|
||||
impl<F> BotInstance<F>
|
||||
where
|
||||
F: std::future::Future + 'static,
|
||||
impl BotInstance
|
||||
// where
|
||||
// F: std::future::Future + 'static,
|
||||
// //F: 'static,
|
||||
{
|
||||
|
||||
|
||||
pub fn init() -> BotInstance<F>
|
||||
where
|
||||
F: std::future::Future + 'static,
|
||||
pub fn init() -> BotInstance
|
||||
// where
|
||||
// F: std::future::Future + 'static,
|
||||
{
|
||||
dotenv().ok();
|
||||
|
||||
|
|
|
@ -59,24 +59,24 @@ pub enum ModStatusType {
|
|||
// pub use EnType::Enabled;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BotAction<F>
|
||||
where
|
||||
F: std::future::Future + ?Sized,
|
||||
pub enum BotAction
|
||||
// where
|
||||
// F: std::future::Future + ?Sized,
|
||||
{
|
||||
//C(BotCommand),
|
||||
L(Listener<F>),
|
||||
L(Listener),
|
||||
R(Routine),
|
||||
}
|
||||
|
||||
|
||||
pub trait BotActionTrait<F>
|
||||
where
|
||||
F: std::future::Future,
|
||||
pub trait BotActionTrait
|
||||
// where
|
||||
// F: std::future::Future,
|
||||
{
|
||||
// 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<F>);
|
||||
fn add_to_modmgr(self, modmgr:&mut ModulesManager<F>);
|
||||
fn add_to_bot(self, bot:BotInstance);
|
||||
fn add_to_modmgr(self, modmgr:ModulesManager) -> ModulesManager;
|
||||
}
|
||||
|
||||
|
||||
|
@ -173,51 +173,59 @@ where
|
|||
// }
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Listener<F: ?Sized>
|
||||
where
|
||||
F: std::future::Future,
|
||||
pub struct Listener
|
||||
// where
|
||||
// F: std::future::Future + ?Sized,
|
||||
{
|
||||
pub module : ModType,
|
||||
pub name : String,
|
||||
//pub exec_body : fn(&mut BotInstance,&PrivmsgMessage) ,
|
||||
pub exec_body : fn(&mut Box<BotInstance<F>>,&PrivmsgMessage) -> F ,
|
||||
// pub exec_body : fn(&mut Box<BotInstance<F>>,&PrivmsgMessage) -> F ,
|
||||
//pub exec_body : fn(&mut botinstance::Chat,&PrivmsgMessage) -> F ,
|
||||
// pub exec_body : fn(botinstance::Chat,PrivmsgMessage) -> F ,
|
||||
//pub exec_body : fn(String,&PrivmsgMessage) -> F ,
|
||||
pub help : String
|
||||
}
|
||||
|
||||
impl<F> Listener<F>
|
||||
where
|
||||
F: std::future::Future,
|
||||
impl Listener
|
||||
// where
|
||||
// F: std::future::Future,
|
||||
{
|
||||
|
||||
async fn execute(&self,m:&mut Box<BotInstance<F>>,n:&PrivmsgMessage){
|
||||
// async fn execute(&self,m:&mut Box<BotInstance<F>>,n:&PrivmsgMessage){
|
||||
// async fn execute(&self,m:&mut botinstance::Chat,n:&PrivmsgMessage){
|
||||
async fn execute(&self,m:botinstance::Chat,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).await;
|
||||
//(self.exec_body)(m,n).await;
|
||||
// (self.exec_body)(m,n);
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> BotActionTrait<F> for Listener<F>
|
||||
where
|
||||
F: std::future::Future,
|
||||
impl BotActionTrait for Listener
|
||||
// where
|
||||
// F: std::future::Future,
|
||||
{
|
||||
// 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<F>) {
|
||||
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 mgr = bot.botmodules;
|
||||
//let nmod = self.module.clone();
|
||||
// mgr.add_botaction(self.module.clone(), BotAction::C(self));
|
||||
|
||||
self.add_to_modmgr(&mut mgr);
|
||||
self.add_to_modmgr(mgr);
|
||||
}
|
||||
|
||||
fn add_to_modmgr(self, modmgr:&mut ModulesManager<F>) {
|
||||
// fn add_to_modmgr(self, modmgr:&mut ModulesManager<F>) {
|
||||
//fn add_to_modmgr(self, mut modmgr:ModulesManager) {
|
||||
fn add_to_modmgr(self, mut modmgr:ModulesManager) -> ModulesManager {
|
||||
// // let mut mgr = bot.botmodules;
|
||||
// // let nmod = self.module.clone();
|
||||
// // mgr.add_botaction(nmod, BotAction::C(self));
|
||||
|
@ -226,7 +234,8 @@ where
|
|||
// 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));
|
||||
// modmgr.add_botaction(self.module.clone(), BotAction::L(self));
|
||||
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
|
||||
|
@ -261,20 +270,23 @@ struct Routine {}
|
|||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ModulesManager<F: ?Sized>
|
||||
where
|
||||
F: std::future::Future,
|
||||
pub struct ModulesManager
|
||||
// where
|
||||
// F: std::future::Future + ?Sized,
|
||||
{
|
||||
statusdb: HashMap<ModType,Vec<ModStatusType>>,
|
||||
botactions: HashMap<ModType,Vec<BotAction<F>>>,
|
||||
botactions: HashMap<ModType,Vec<BotAction>>,
|
||||
}
|
||||
|
||||
impl<F> ModulesManager<F>
|
||||
where
|
||||
F: std::future::Future,
|
||||
impl ModulesManager
|
||||
// where
|
||||
// F: std::future::Future,
|
||||
{
|
||||
|
||||
pub fn init() -> ModulesManager<F> {
|
||||
pub fn init() -> ModulesManager
|
||||
// where
|
||||
// F: std::future::Future ,
|
||||
{
|
||||
|
||||
// initializes the modulers manager
|
||||
// Ideally, this should have added known modules based on
|
||||
|
@ -308,7 +320,7 @@ where
|
|||
// modactions.push(BotAction::L(newlistener));
|
||||
|
||||
|
||||
let mut mgr = ModulesManager {
|
||||
let mgr = ModulesManager {
|
||||
statusdb : m,
|
||||
botactions : act,
|
||||
};
|
||||
|
@ -366,7 +378,7 @@ where
|
|||
// crate::core::init(); // works
|
||||
//crate::submods::
|
||||
//crate::arbfile;
|
||||
crate::modules::init(&mut mgr);
|
||||
let mgr = crate::modules::init(mgr);
|
||||
|
||||
|
||||
|
||||
|
@ -400,8 +412,8 @@ where
|
|||
Ok("")
|
||||
}
|
||||
|
||||
|
||||
pub fn add_botaction(&mut self, in_module:ModType, in_action:BotAction<F> ) -> () {
|
||||
pub fn add_botaction(mut self, in_module:ModType, in_action:BotAction ) -> ModulesManager {
|
||||
//pub fn add_botaction(&mut self, in_module:ModType, in_action:BotAction ) -> () {
|
||||
/*
|
||||
adds a BotAction to the Modules Manager - This will require a BotModule passed as well
|
||||
This will including the logic of a valid add
|
||||
|
@ -429,9 +441,9 @@ where
|
|||
// - If BotAction to Add is a BotCommand , In Module Manager DB (botactions),
|
||||
// Check All Other BotAction Command Names & Aliases to ensure they don't conflict
|
||||
|
||||
fn find_conflict_module<F>(mgr:& ModulesManager<F>, act:& BotAction<F>) -> Option<ModType>
|
||||
where
|
||||
F: std::future::Future,
|
||||
fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option<ModType>
|
||||
// where
|
||||
// F: std::future::Future,
|
||||
{
|
||||
|
||||
// Some(BotModule(String::from("GambaCore")))
|
||||
|
@ -544,7 +556,12 @@ where
|
|||
println!(">> Modules Manager : Called Add bot Action");
|
||||
//println!(">> Modules Manager : {:?}",&self);
|
||||
|
||||
();
|
||||
//();
|
||||
//let mgr = self;
|
||||
|
||||
//mgr
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -24,9 +24,9 @@ use crate::core::botinstance::BotInstance;
|
|||
#[tokio::main]
|
||||
pub async fn main() {
|
||||
|
||||
let bot:BotInstance<dyn std::future::Future<Output = ()>> = BotInstance::init();
|
||||
// let bot:BotInstance<dyn std::future::Future<Output = ()>> = BotInstance::init();
|
||||
|
||||
//let bot = BotInstance::init();
|
||||
let bot = BotInstance::init();
|
||||
|
||||
bot.runner().await;
|
||||
|
||||
|
|
|
@ -19,15 +19,14 @@ mod experiments;
|
|||
|
||||
// [ ] init() function that accepts bot instance - this is passed to init() on submodules
|
||||
|
||||
pub fn init<F>(mgr:&mut ModulesManager<F>) -> ()
|
||||
where
|
||||
F: std::future::Future,
|
||||
pub fn init(mgr:ModulesManager) -> ModulesManager
|
||||
// where
|
||||
// F: std::future::Future,
|
||||
{
|
||||
// Modules initializer loads modules into the bot
|
||||
// this is achieved by calling submodules that also have fn init() defined
|
||||
|
||||
experiments::init(mgr);
|
||||
|
||||
experiments::init(mgr)
|
||||
|
||||
();
|
||||
//();
|
||||
}
|
|
@ -12,14 +12,14 @@
|
|||
// mod crate::modules;
|
||||
//use crate::modules;
|
||||
|
||||
use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait};
|
||||
use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait, self};
|
||||
|
||||
use crate::core::botinstance::BotInstance;
|
||||
use crate::core::botinstance::{self};
|
||||
use twitch_irc::message::PrivmsgMessage;
|
||||
|
||||
pub fn init<F>(mgr:&mut ModulesManager<F>) -> ()
|
||||
where
|
||||
F: std::future::Future,
|
||||
pub fn init(mgr:ModulesManager) -> ModulesManager
|
||||
// where
|
||||
// F: std::future::Future,
|
||||
{
|
||||
|
||||
|
||||
|
@ -33,20 +33,87 @@ where
|
|||
// }.add_to_modmgr(mgr);
|
||||
|
||||
|
||||
Listener {
|
||||
let list1 = Listener {
|
||||
module : BotModule(String::from("experiments 004")),
|
||||
name : String::from("GoodGirl Listener"),
|
||||
exec_body : good_girl,
|
||||
// exec_body : good_girl::<dyn std::future::Future<Output = ()>> ,
|
||||
// exec_body : good_girl ,
|
||||
help : String::from("")
|
||||
}.add_to_modmgr(mgr);
|
||||
};
|
||||
|
||||
let mgr = list1.add_to_modmgr(mgr);
|
||||
|
||||
// Listener {
|
||||
// module : BotModule(String::from("experiments 004")),
|
||||
// name : String::from("GoodGirl Listener"),
|
||||
// // exec_body : good_girl::<dyn std::future::Future<Output = ()>> ,
|
||||
// exec_body : good_girl ,
|
||||
// help : String::from("")
|
||||
// }.add_to_modmgr(mgr);
|
||||
|
||||
|
||||
// let list1 = Listener2 {
|
||||
// module : BotModule(String::from("experiments 004")),
|
||||
// name : String::from("GoodGirl Listener"),
|
||||
// // exec_body : good_girl::<dyn std::future::Future<Output = ()>> ,
|
||||
// exec_body : good_girl ,
|
||||
// help : String::from("")
|
||||
// };
|
||||
|
||||
// let list1 = Listener3 {
|
||||
// module : BotModule(String::from("experiments 004")),
|
||||
// name : String::from("GoodGirl Listener"),
|
||||
// // exec_body : good_girl::<dyn std::future::Future<Output = ()>> ,
|
||||
// // exec_body : testfn2 ,
|
||||
// help : String::from("")
|
||||
// };
|
||||
|
||||
println!("At Experiments module");
|
||||
|
||||
|
||||
();
|
||||
//();
|
||||
mgr
|
||||
}
|
||||
|
||||
|
||||
pub struct Listener2<F>
|
||||
// where
|
||||
// F: std::future::Future + ?Sized,
|
||||
{
|
||||
pub module : botmodules::ModType,
|
||||
pub name : String,
|
||||
//pub exec_body : fn(&mut BotInstance,&PrivmsgMessage) ,
|
||||
// pub exec_body : fn(&mut Box<BotInstance<F>>,&PrivmsgMessage) -> F ,
|
||||
//pub exec_body : fn(&mut botinstance::Chat,&PrivmsgMessage) -> dyn std::future::Future<Output = ()> ,
|
||||
pub exec_body : fn(&mut botinstance::Chat,&PrivmsgMessage) -> F ,
|
||||
//pub exec_body : fn(String,&PrivmsgMessage) -> F ,
|
||||
pub help : String
|
||||
}
|
||||
|
||||
|
||||
// pub struct Listener3<F>
|
||||
// // where
|
||||
// // F: std::future::Future + ?Sized,
|
||||
// {
|
||||
// pub module : botmodules::ModType,
|
||||
// pub name : String,
|
||||
// //pub exec_body : fn(&mut BotInstance,&PrivmsgMessage) ,
|
||||
// // pub exec_body : fn(&mut Box<BotInstance<F>>,&PrivmsgMessage) -> F ,
|
||||
// //pub exec_body : fn(&mut botinstance::Chat,&PrivmsgMessage) -> dyn std::future::Future<Output = ()> ,
|
||||
// // pub exec_body : fn(botinstance::Chat) -> F , // this appears to work
|
||||
// // pub exec_body : fn(botinstance::Chat,PrivmsgMessage) -> F , // this appears to work
|
||||
// //pub exec_body : fn(String,&PrivmsgMessage) -> F ,
|
||||
// pub help : String
|
||||
// }
|
||||
|
||||
|
||||
async fn testfn2(mut _chat:botinstance::Chat,_msg:PrivmsgMessage)
|
||||
// where
|
||||
// F : std::future::Future + ?Sized,
|
||||
{
|
||||
println!("testfn2");
|
||||
}
|
||||
|
||||
// async fn good_girl(bot:&mut BotInstance,msg:&PrivmsgMessage) {
|
||||
// println!("In GoodGirl()");
|
||||
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
|
||||
|
@ -72,16 +139,33 @@ where
|
|||
|
||||
// }
|
||||
|
||||
async fn good_girl<F>(bot:&mut Box<BotInstance<F>>,msg:&PrivmsgMessage)
|
||||
where
|
||||
F: std::future::Future,
|
||||
//async fn good_girl<F>(bot:&mut Box<BotInstance<F>>,msg:&PrivmsgMessage)
|
||||
//async fn good_girl<F>(chat:&mut botinstance::Chat,msg:&PrivmsgMessage)
|
||||
// async fn good_girl(chat:&mut botinstance::Chat,msg:&PrivmsgMessage)
|
||||
async fn good_girl(mut chat:botinstance::Chat,msg:PrivmsgMessage)
|
||||
// where
|
||||
// F: std::future::Future + ?Sized,
|
||||
{
|
||||
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;
|
||||
chat.say_in_reply_to(&msg,String::from("GoodGirl")).await;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// async fn good_girl(chat:String,msg:&PrivmsgMessage)
|
||||
// // where
|
||||
// // F: std::future::Future + ?Sized,
|
||||
// {
|
||||
// println!("In GoodGirl()");
|
||||
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
|
||||
|
||||
// if msg.sender.name == "ModulatingForce" && msg.message_text.contains("GoodGirl") {
|
||||
// println!("Testing from goodgirl");
|
||||
// }
|
||||
|
||||
|
||||
// }
|
Loading…
Reference in a new issue