From eedf90907ee420667e9629cfbc881fc23aa0009b Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Mon, 12 Feb 2024 01:25:12 -0500 Subject: [PATCH] 2024.02.12 - WORKS --- src/core/botinstance.rs | 292 +++++++++++++++++++++++++++++-------- src/core/botmodules.rs | 169 +++++++++++++++------ src/core/identity.rs | 67 ++++++--- src/main.rs | 5 +- src/modules.rs | 2 +- src/modules/experiments.rs | 30 ++-- 6 files changed, 422 insertions(+), 143 deletions(-) diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index 5f9561d..090b0d5 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -1,6 +1,7 @@ -use futures::lock::Mutex; +// use futures::lock::Mutex; use tokio::sync::mpsc::UnboundedReceiver; +use tokio::sync::RwLock; use twitch_irc::login::StaticLoginCredentials; use twitch_irc::ClientConfig; use twitch_irc::SecureTCPTransport; @@ -20,6 +21,9 @@ use std::collections::HashMap; use rand::Rng; +// Important to use tokios Mutex here since std Mutex doesn't work with async functions +use tokio::sync::Mutex; + use crate::core::ratelimiter::RateLimiter; @@ -31,7 +35,7 @@ use crate::core::identity::{IdentityManager,Permissible}; use std::rc::Rc; use std::cell::RefCell; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; // use futures::lock::Mutex; use std::pin::Pin; @@ -39,6 +43,9 @@ use std::pin::Pin; //use std::borrow::Borrow; use core::borrow::Borrow; +// pub type BotAR = Arc>; +use super::botmodules::bot_actions::actions_util::BotAR; + #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum ChType { @@ -48,11 +55,14 @@ pub enum ChType { pub use ChType::Channel; -pub enum ModType { - BotModule(String), -} -pub use ModType::BotModule; + +// pub enum ModType { +// BotModule(String), +// } + +// pub use ModType::BotModule; + #[derive(Clone)] pub struct Chat { @@ -163,7 +173,7 @@ impl Chat { #[derive(Clone)] pub struct BotManagers { // pub botmodules : ModulesManager, - pub identity : Arc>, + pub identity : Arc>, pub chat : Chat, } @@ -171,23 +181,27 @@ impl BotManagers { pub fn init(ratelimiters:HashMap, client:TwitchIRCClient, StaticLoginCredentials>) - -> Arc> { - let a = Arc::new(Mutex::new(BotManagers { + -> BotManagers { + // let a = Arc::new(Mutex::new(BotManagers { + // // botmodules : ModulesManager::init(), + // identity : Arc::new(Mutex::new(IdentityManager::init())), + // chat : Chat::init(ratelimiters,client), + // })); + // a + BotManagers { // botmodules : ModulesManager::init(), - identity : Arc::new(Mutex::new(IdentityManager::init())), + identity : Arc::new(RwLock::new(IdentityManager::init())), chat : Chat::init(ratelimiters,client), - })); - a - + } } - pub fn rIdentity(self) -> Arc> { + pub fn rIdentity(self) -> Arc> { self.identity } - pub fn rChat(&self) -> Arc> { - Arc::new(Mutex::new(self.chat)) - } + // pub fn rChat(&self) -> Arc> { + // Arc::new(Mutex::new(self.chat)) + // } } @@ -206,14 +220,16 @@ pub struct BotInstance { pub prefix : char, pub bot_channel : ChType, - pub incoming_messages : UnboundedReceiver, + // pub incoming_messages : UnboundedReceiver, + pub incoming_messages : Arc>>, // pub incoming_messages : RefCell>, // pub chat : Chat, - pub botmodules : Arc>, + pub botmodules : Arc, pub twitch_oauth : String, pub bot_channels : Vec, // pub identity : IdentityManager, - pub botmgrs : Arc>, + // pub botmgrs : Arc>, + pub botmgrs : BotManagers, } @@ -223,7 +239,8 @@ impl BotInstance // pub fn init() -> BotInstance - pub fn init() -> Arc + // pub fn init() -> Arc + pub fn init() -> BotInstance { dotenv().ok(); @@ -272,7 +289,7 @@ impl BotInstance let b = BotInstance { prefix : prefix, bot_channel : Channel(login_name) , - incoming_messages : incoming_messages, + incoming_messages : Arc::new(RwLock::new(incoming_messages)), //client : client, // chat : Chat { // ratelimiters : ratelimiters, @@ -289,7 +306,9 @@ impl BotInstance //println!("{:?}",b.botmgrs.chat.ratelimiters); - Arc::new(b) + // Arc::new(b) + //Arc::new(RwLock::new(b)) + b } // async fn rcv_helper(self) -> Option { @@ -299,10 +318,14 @@ impl BotInstance // } // pub async fn runner(mut self) -> () { - pub async fn runner(mut self) -> () { + // pub async fn runner(&'static mut self) -> () { + pub async fn runner(self) -> () { + // let bot_am = Arc::new(Mutex::new(self)); // let mut boxed_bot = Arc::new(RefCell::new(self)); // <-- [ERROR] Future cannot be handled safely + + let bot = Arc::new(RwLock::new(self)); let join_handle = tokio::spawn(async move { @@ -313,7 +336,7 @@ impl BotInstance // let mut bot = Rc::new(RefCell::new(&self)); //let bot = Arc::new(Mutex::new(&self)); // let mut boxed_bot = Arc::new(RefCell::new(self)); - let mut boxed_bot = Arc::new(Mutex::new(self)); + // let mut boxed_bot = Arc::new(Mutex::new(self)); // while let Some(message) = bot.borrow_mut().incoming_messages.recv().await { @@ -385,8 +408,16 @@ impl BotInstance // while let Some(message) = a.incoming_messages.recv().await { // let a = Arc::clone(&boxed_bot).into_inner(); // while let Some(message) = a.incoming_messages.recv().await { - let tempbot = boxed_bot.clone(); - while let Some(message) = Arc::try_unwrap(tempbot.clone()).ok().unwrap().into_inner().incoming_messages.recv().await { + // let tempbot = boxed_bot.clone(); + // while let Some(message) = Arc::try_unwrap(tempbot.clone()).ok().unwrap().into_inner().incoming_messages.recv().await { + + // let tempbot = Arc::clone(&boxed_bot); + + // while let Some(message) = tempbot.lock().await.incoming_messages.recv().await { + let a = bot.read().await; + let mut a = a.incoming_messages.write().await; + while let Some(message) = a.recv().await { + // while let Some(message) = tempbot.into_inner().incoming_messages.recv().await { //while let Some(message) = boxed_bot.borrow().incoming_messages.recv().await { // // Below can be used to debug if I want to capture all messages @@ -436,8 +467,28 @@ impl BotInstance // (*a).listener_main_prvmsg(&msg).await; // let a:Arc> = Arc::clone(&boxed_bot); // a.into_inner().listener_main_prvmsg(&msg).await; - let tempbot = boxed_bot.clone(); - Arc::try_unwrap(tempbot.clone()).ok().unwrap().into_inner().listener_main_prvmsg(&msg).await ; + // let tempbot = boxed_bot.clone(); + // Arc::try_unwrap(tempbot.clone()).ok().unwrap().into_inner().listener_main_prvmsg(&msg).await ; + + // let tempbot = Arc::clone(&tempbot); + // // let a = tempbot.lock().await; + // tempbot.lock().await.listener_main_prvmsg(&msg).await; + // self.listener_main_prvmsg(&msg).await; + // bot.read().await.listener_main_prvmsg(&msg).await; + + // let a = bot.read().await; + // a.listener_main_prvmsg(&msg).await; + // a.listener_main_prvmsg(&msg).await; + // let a = bot.read().await; + BotInstance::listener_main_prvmsg(Arc::clone(&bot), &msg).await; + // let a = bot.read().await; + // a.lis + + + + + + //self.listener_main_prvmsg(&msg).await; // - BotCommand listener should likely need to be called within the above @@ -468,18 +519,32 @@ impl BotInstance - pub fn get_botmodules(self) -> Arc> { + pub fn get_botmodules(self) -> Arc { // let a = self.botmodules; // Arc::clone(&Arc::new(Mutex::new(self.botmodules))) + // *self.botmodules self.botmodules } - pub fn get_botactions(&self) -> &HashMap> { - self.get_botactions() - } + // pub fn get_botactions(self:&Self) -> (Self,HashMap>) { + // // self.get_botactions() + // // (*self,(*self).botmodules.rbotactions()) + // (Self { bot_channel},(*self).botmodules.rbotactions()) + // } - pub async fn get_botmgrs(self) -> Arc> { + // pub fn get_botactions(&self) -> (Self,HashMap>) { + // pub fn get_botactions(&self) -> HashMap> { + // // self.get_botactions() + // // (*self,(*self).botmodules.rbotactions()) + // // (self,self.botmodules.rbotactions()) + // // (*self).botmodules.rbotactions() + // // let a = (*self).botmodules.rbotactions(); + // let a = + // a + // } + + pub async fn get_botmgrs(self) -> BotManagers { // Arc::new(self.botmgrs) // Arc::clone(&Arc::new(Mutex::new(self.botmgrs))) let a = self.botmgrs; @@ -488,29 +553,75 @@ impl BotInstance a } - pub async fn get_identity(self) -> Arc> { - // let a = self.botmgrs; - // Arc::clone(&Arc::new(Mutex::new(a.rIdentity()))) - // let a = self.botmgrs; - // Arc::clone(&Arc::new(Mutex::new(a.rIdentity()))) - let a = self.get_botmgrs().await; - let a = a.lock().await; - // let a = a.rIdentity(); - let a = a.clone().identity; - a.clone() + // pub fn get_identity(self:&Self) -> (Self,IdentityManager) { + pub fn get_identity(&self) -> Arc> { + // // let a = self.botmgrs; + // // Arc::clone(&Arc::new(Mutex::new(a.rIdentity()))) + // // let a = self.botmgrs; + // // Arc::clone(&Arc::new(Mutex::new(a.rIdentity()))) + // let a = self.get_botmgrs().await; + // let a = a.lock().await; + // // let a = a.rIdentity(); + // let a = a.clone().identity; + // a.clone() + // let id = (*self).botmgrs.identity; + // id + Arc::clone(&self.botmgrs.identity) } - pub fn get_prefix(&self) -> String { - self.prefix.to_string() + + // pub fn get_prefix(self) -> (Self,char) { + pub fn get_prefix(&self) -> char { + // self.prefix.to_string() + // let a = self.prefix; + // a.clone().to_string() + // (self,self.prefix) + + (*self).prefix } + // pub fn get_prefix(self:&Self) -> (Self,String) { + // (*self,(*self).prefix.to_string()) + // } + // pub fn get_prefix(self:Self) -> (Self,String) { + // let str1 = self.prefix.to_string(); + // (self,str1) + // } + + // pub fn get_prefix(&self) -> String { + // // self.prefix.to_string() + // let a = self.prefix; + // a.clone().to_string() + // } + + // pub fn get_prefix(self) -> (Self,String) { + // // self.prefix.to_string() + // // let a = self.prefix; + // // a.clone().to_string() + // (Self { + // prefix : self.prefix , + // bot_channel : self.bot_channel , + // incoming_messages : self.incoming_messages , + // botmodules : self.botmodules, + // twitch_oauth : self.twitch_oauth, + // bot_channels : self.bot_channels , + // // identity : IdentityManager::init(), + // botmgrs: self.botmgrs , + // }, + // self.prefix.to_string()) + // } + // ----------------- // PRIVATE FUNCTIONS // async fn listener_main_prvmsg(&mut self,msg:PrivmsgMessage) -> () { - async fn listener_main_prvmsg(self,msg:&PrivmsgMessage) -> () { + // async fn listener_main_prvmsg(&mut self,msg:&PrivmsgMessage) -> () { + // async fn listener_main_prvmsg(self,msg:&PrivmsgMessage) -> () { + // async fn listener_main_prvmsg(self:Arc,msg:&PrivmsgMessage) -> () { + pub async fn listener_main_prvmsg(bot:BotAR,msg:&PrivmsgMessage) -> () { + // let a = a; // 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) @@ -526,7 +637,8 @@ impl BotInstance // let mut boxed_bot = Arc::new(RwLock::new(self)); // let boxed_bot = Arc::new(RwLock::new(self)); // let boxed_bot = Arc::new(Mutex::new(self)); - let boxed_bot = Arc::new(Mutex::new(self)); + //let boxed_bot = Arc::new(Mutex::new(self)); + // let bot = Arc::new(RwLock::new(self)); // for (_m,acts) in &self.botmodules.botactions { @@ -547,12 +659,27 @@ impl BotInstance // for (_m,acts) in b.rbotactions() { // for (_m,acts) in (*Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().read().unwrap()).rbotactions() { // let a = boxed_bot.clone().into_inner(); - let a = boxed_bot.lock().await; + // let a = boxed_bot.lock().await; //let a = a.lock().in // for (_m,acts) in self.rbotactions() { // for (_m,acts) in a.read().ok().unwrap().rbotactions() { // for (_m,acts) in a.into_inner().ok().unwrap().rbotactions() { - for (_m,acts) in a.get_botactions() { + + // let bot = self; + // let mut instr:char; + + // let hacts = self.get_botactions(); + // let hacts = boxed_bot.clone().lock().await.get_botactions(); + // let hacts = bot.read().await.get_botactions(); + let botlock = bot.read().await; + let hacts = botlock.botmodules.botactions.read().await; + // let hacts = hacts + for (_m,acts) in &(*hacts) { + + + + + // let bot = bot; for a in acts { @@ -600,10 +727,16 @@ impl BotInstance // if inpt == a.into_inner().ok().unwrap().prefix.to_string() + c.command.as_str() { // if inpt == a.into_inner().ok().unwrap().prefix.to_string() + c.command.as_str() - let a = boxed_bot.lock().await; + // let a = boxed_bot.lock().await; // // if inpt == a.into_inner().prefix.to_string() + c.command.as_str() { - if inpt == a.get_prefix() + c.command.as_str() { + // if inpt == a.get_prefix() + c.command.as_str() { + // if inpt == self.get_prefix() + c.command.as_str() { + // let instr = self.get_prefix(); + // let instr = boxed_bot.clone().lock().await.get_prefix(); + // let instr = bot.read().await.get_prefix(); + let instr = bot.read().await.get_prefix(); + if inpt == String::from(instr) + c.command.as_str() { confirmed_bot_command = true; } @@ -618,10 +751,16 @@ impl BotInstance // if inpt == Arc::try_unwrap(boxed_bot).ok().unwrap().read().unwrap().prefix.to_string() + alias.as_str() { // if inpt == self.prefix.to_string() + alias.as_str() { // let a = boxed_bot.clone(); - let a = boxed_bot.lock().await; + // let a = boxed_bot.lock().await; // if inpt == a.into_inner().ok().unwrap().prefix.to_string() + alias.as_str() { // if inpt == a.into_inner().prefix.to_string() + alias.as_str() { - if inpt == a.get_prefix() + alias.as_str() { + // if inpt == a.get_prefix() + alias.as_str() { + // if inpt == self.get_prefix() + alias.as_str() { + // let instr = self.get_prefix(); + // let instr = boxed_bot.clone().lock().await.get_prefix(); + // let instr = bot.read().await.get_prefix(); + let instr = bot.read().await.get_prefix(); + if inpt == String::from(instr) + alias.as_str() { confirmed_bot_command = true; } } @@ -671,14 +810,27 @@ impl BotInstance // match a.lock().await.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await { { - let le = boxed_bot.lock().await; + // let le = boxed_bot.lock().await; + // // let le = le.lock().await; + // let le = le.get_identity().await; + // let le = *le; // let le = le.lock().await; - let le = le.get_identity().await; - let le = *le; - let le = le.lock().await; - let le = le.clone(); - let le = le.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await; - match le { + // let le = le.clone(); + // let le = le.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await; + // let le = self.get_identity().await.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await; + // let le = self.botmgrs; + // let le = le.identity; + // let (bot,id) = self.get_identity(); + // let id = boxed_bot.clone().lock().await.get_identity(); + // let id = bot.read().await.get_identity(); + // let id = Arc::clone(&self.botmgrs.identity); + // let id = id.write().await; + // let id = &(*self.get_identity()); + let botlock = bot.read().await; + let id = botlock.get_identity(); + let id = id.read().await; + let eval= id.can_user_run_PRVMSG(&msg, c.required_roles.clone()); + match eval { // Ok(Permissible::Allow) => (), Permissible::Allow => { println!("Executed as permissible"); @@ -687,7 +839,15 @@ impl BotInstance // c.execute(bot, msg.clone()).await; // } // let boxed_bot = Arc::new(RwLock::new(self)); - c.execute(boxed_bot.clone(), msg.clone()).await; + // c.execute(boxed_bot.clone(), msg.clone()).await; + // c.execute(self, msg.clone()).await; + //let mut a = *self; + // c.execute(self, msg.clone()); + // let a = self; + // let a = Arc::clone(&self); + let a = Arc::clone(&bot); + // let a = Arc::clone(&bot); + c.execute(a, msg.clone()); } Permissible::Block => { @@ -708,7 +868,10 @@ impl BotInstance //let bot = Rc::clone(&bot).into_inner(); // l.execute(boxed_bot.clone(), msg.clone()).await // let boxed_bot = Arc::new(RwLock::new(self)); - l.execute(boxed_bot.clone(), msg.clone()).await; + // l.execute(boxed_bot.clone(), msg.clone()).await; + // let a = Arc::clone(&self); + let a = Arc::clone(&bot); + l.execute(a, msg.clone()); }, _ => (), @@ -720,6 +883,9 @@ impl BotInstance println!("End of Separate Listener Main prvmsg"); + // self + // bot + } diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index a05db7d..38f1fc6 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -6,16 +6,24 @@ use std::collections::HashMap; use crate::core::identity; use std::cell::RefCell; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; +use tokio::sync::RwLock; use std::future::Future; -use futures::lock::Mutex; +// use futures::lock::Mutex; + +// Important to use tokios Mutex here since std Mutex doesn't work with async functions +use tokio::sync::Mutex; use crate::core::botinstance::{self, BotInstance}; use std::rc::Rc; +// use tokio::sync::RwLock; + + + use async_trait::async_trait; /* @@ -55,6 +63,9 @@ pub enum ChType { pub use ChType::Channel; use twitch_irc::message::PrivmsgMessage; +use self::bot_actions::actions_util; +use self::bot_actions::actions_util::BotAR; + #[derive(Debug)] enum StatusLvl { @@ -78,11 +89,12 @@ pub enum BotAction impl BotAction { // pub async fn execute(&self,m:botinstance::BotManagers,n:PrivmsgMessage){ - pub async fn execute(&self,m:Arc>,n:PrivmsgMessage){ + pub fn execute(&self,m:BotAR,n:PrivmsgMessage) -> () + { match self { - BotAction::L(a) => a.execute(m,n).await, - BotAction::C(a) => a.execute(m,n).await, + BotAction::L(a) => a.execute(m,n), + BotAction::C(a) => a.execute(m,n), _ => (), } @@ -92,8 +104,9 @@ impl BotAction { #[async_trait] pub trait BotActionTrait { - async fn add_to_bot(&self, bot:BotInstance); - async fn add_to_modmgr(self,modmgr:Arc>); + async fn add_to_bot(self, bot:BotInstance); + // async fn add_to_modmgr(self,modmgr:&'static ModulesManager); + async fn add_to_modmgr(self,modmgr:Arc); } // #[derive(Clone)] @@ -111,25 +124,38 @@ impl BotCommand { // pub async fn execute(&self,m:botinstance::BotManagers,n:PrivmsgMessage){ // pub async fn execute(&self,m:Rc<&botinstance::BotManagers>,n:PrivmsgMessage){ - pub async fn execute(&self,m:Arc>,n:PrivmsgMessage){ - (self.exec_body)(m,n).await; + // pub async fn execute(&self,m:BotInstance,n:PrivmsgMessage){ + // (self.exec_body)(m,n).await; + // } + // pub fn execute(&self,m:&mut BotInstance,n:PrivmsgMessage) -> () { + // pub fn execute(&self,m:actions_util::BotAR,n:PrivmsgMessage) -> () { + pub fn execute(&self,m:BotAR,n:PrivmsgMessage) -> () { + // ((*self).exec_body)(m,n); + // ((*self).exec_body)(*m,n); + // m + // ((*self).exec_body)( + ((*self).exec_body)(m,n); + // m } } #[async_trait] impl BotActionTrait for BotCommand { - async fn add_to_bot(&self, mut bot:BotInstance) { + async fn add_to_bot(self, bot:BotInstance) { // let mgr = &mut bot.botmodules; // let mut mgr = *mgr.lock().await; // let mut mgr = &mut mgr; + // (*self).add_to_modmgr(bot.botmodules); self.add_to_modmgr(bot.botmodules); } - async fn add_to_modmgr(self, modmgr:Arc>) { + // async fn add_to_modmgr(self, modmgr:Arc>) { + async fn add_to_modmgr(self, modmgr:Arc) { // modmgr.add_botaction(self.module.clone(), BotAction::C(self)) - let modmgr = *modmgr.lock().await; - modmgr.add_botaction(self.module.clone(), BotAction::C(self)) + // let modmgr = *modmgr.lock().await; + // modmgr.add_botaction(self.module.clone(), BotAction::C(self)) + modmgr.add_botaction(self.module.clone(), BotAction::C(self)).await } } @@ -149,8 +175,13 @@ pub mod bot_actions { use crate::core::botinstance::{BotInstance, BotManagers, Chat}; use twitch_irc::message::PrivmsgMessage; use std::cell::RefCell; - use std::sync::{Arc, RwLock}; - use futures::lock::Mutex; + use std::sync::{Arc}; + // use futures::lock::Mutex; + // Important to use tokios Mutex here since std Mutex doesn't work with async functions + use tokio::sync::{Mutex,RwLock}; + + pub type BotAM = Arc>; + pub type BotAR = Arc>; // pub type ExecBody = Box Pin + Send>> + Send + Sync>; @@ -170,8 +201,14 @@ pub mod bot_actions { // pub type ExecBody = Box,PrivmsgMessage) -> Pin + Send>> + Send + Sync>; // pub type ExecBody = Box>,PrivmsgMessage) -> Pin + Send>> + Send + Sync>; // pub type ExecBody = Box>,PrivmsgMessage) -> Pin + Send>> + Send + Sync>; - pub type ExecBody = Box>,PrivmsgMessage) -> Pin + Send>> + Send + Sync>; - + // pub type ExecBody = Box>,PrivmsgMessage) -> Pin + Send>> + Send + Sync>; + // pub type ExecBody = Box Pin + Send>> + Send + Sync>; + // pub type ExecBody = Box Pin + Send>> + Send + Sync>; + // pub type ExecBody = Box Pin + Send>> + Send + Sync>; + // pub type ExecBody = Box Pin + Send>> + Send + Sync>; + pub type ExecBody = Box Pin + Send>> + Send + Sync>; + // pub type ExecBody = Box,PrivmsgMessage) -> Pin + Send>> + Send + Sync>; + //pub fn asyncbox(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody @@ -187,9 +224,14 @@ pub mod bot_actions { // pub fn asyncbox(f: fn(Arc,PrivmsgMessage) -> T) -> ExecBody // pub fn asyncbox(f: fn(Arc>,PrivmsgMessage) -> T) -> ExecBody // pub fn asyncbox(f: fn(Arc>,PrivmsgMessage) -> T) -> ExecBody - pub fn asyncbox(f: fn(Arc>,PrivmsgMessage) -> T) -> ExecBody + // pub fn asyncbox(f: fn(Arc>,PrivmsgMessage) -> T) -> ExecBody + // pub fn asyncbox(f: fn(BotInstance,PrivmsgMessage) -> T) -> ExecBody + // pub fn asyncbox(f: fn(&'static BotInstance,PrivmsgMessage) -> T) -> ExecBody + // pub fn asyncbox(f: fn(&mut BotInstance,PrivmsgMessage) -> T) -> ExecBody + pub fn asyncbox(f: fn(BotAR,PrivmsgMessage) -> T) -> ExecBody where //T: Future + Send + 'static + // T: Future + Send + 'static T: Future + Send + 'static { Box::new(move |a,b| Box::pin(f(a,b))) @@ -211,25 +253,41 @@ pub struct Listener impl Listener { - pub async fn execute(&self,m:Arc>,n:PrivmsgMessage){ - (self.exec_body)(m,n).await; + // pub async fn execute(&self,m:BotInstance,n:PrivmsgMessage){ + // (self.exec_body)(m,n).await; + // } + // pub fn execute(&self,m:BotInstance,n:PrivmsgMessage){ + // (self.exec_body)(m,n); + // } + // pub fn execute(&self,m:&BotInstance,n:PrivmsgMessage) -> &BotInstance { + // pub fn execute(&self,m:actions_util::BotAR,n:PrivmsgMessage) -> () { + pub fn execute(&self,m:BotAR,n:PrivmsgMessage) -> () { + // let mut m = Arc::*m; + ((*self).exec_body)(m,n); + // *self + // &m } } #[async_trait] impl BotActionTrait for Listener { - async fn add_to_bot(&self, mut bot:BotInstance) { + async fn add_to_bot(self, bot:BotInstance) { // let mgr = &mut bot.botmodules; - let mgr = bot.botmodules; - self.add_to_modmgr(mgr); + // let mgr = bot.botmodules; + // self.add_to_modmgr(Arc::new(*mgr)); + self.add_to_modmgr(bot.botmodules); } // fn add_to_modmgr(self, modmgr:&mut ModulesManager) { - async fn add_to_modmgr(self, modmgr:Arc>) { - let modmgr = *modmgr.lock().await; - modmgr.add_botaction(self.module.clone(), BotAction::L(self)) + // async fn add_to_modmgr(self, modmgr:Arc>) { + // async fn add_to_modmgr(self, modmgr:&'static ModulesManager) { + // async fn add_to_modmgr(self, modmgr:&'static ModulesManager) { + async fn add_to_modmgr(self, modmgr:Arc) { + // let modmgr = *modmgr.lock().await; + modmgr.add_botaction(self.module.clone(), BotAction::L(self)).await; + // modmgr.add_botaction(self.module.clone(), BotAction:L(self)) } } @@ -243,14 +301,17 @@ struct Routine {} pub struct ModulesManager { - statusdb: HashMap>, - pub botactions: HashMap>, + // statusdb: HashMap>, + statusdb: Arc>>>, + // pub botactions: HashMap>, + pub botactions: Arc>>>, } impl ModulesManager { - pub fn init() -> Arc> + // pub fn init() -> Arc> + pub fn init() -> Arc { @@ -258,33 +319,40 @@ impl ModulesManager let act = HashMap::new(); let mut mgr = ModulesManager { - statusdb : m, - botactions : act, + statusdb : Arc::new(RwLock::new(m)), + botactions : Arc::new(RwLock::new(act)), }; // :: [x] initialize core modules // crate::core::identity::init(&mut mgr); - let a = Arc::new(Mutex::new(mgr)); - // let a = a.clone(); - crate::core::identity::init(a.clone()); + // let a = Arc::new(Mutex::new(mgr)); + // // let a = a.clone(); + // crate::core::identity::init(a.clone()); + + // crate::core::identity::init(&mgr); + let mgra = Arc::new(mgr); + crate::core::identity::init(Arc::clone(&mgra)); // initialize custom crate modules // crate::modules::init(&mut mgr); // let a = a.clone(); - crate::modules::init(a.clone()); + // crate::modules::init(a.clone()); + crate::modules::init(Arc::clone(&mgra)); println!(">> Modules Manager : End of Init"); // mgr - a + mgra } - pub fn rbotactions(self) -> HashMap> { - self.botactions - } + // pub async fn rbotactions(&self) -> HashMap> { + // // (*self).botactions + // let a = self.botactions.read().await; + // *a + // } pub fn modstatus(&self, _:ModType, _:ChType) -> ModStatusType { @@ -314,7 +382,7 @@ impl ModulesManager //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 ) -> ModulesManager { //pub fn add_botaction(&mut self, in_module:ModType, in_action:BotAction ) -> () { - pub fn add_botaction(&mut self, in_module:ModType, in_action:BotAction ) { + pub async fn add_botaction(&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 @@ -342,7 +410,7 @@ impl ModulesManager // - 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(mgr:& ModulesManager, act:& BotAction) -> Option + async fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option { // Some(BotModule(String::from("GambaCore"))) @@ -360,7 +428,8 @@ impl ModulesManager // let n = & mgr.botactions; - let d = &mgr.botactions; + let d = mgr.botactions.read().await; + let d = &(*d); for (module,moduleactions) in d { @@ -431,20 +500,30 @@ impl ModulesManager // // () // return because there was a conflict? // panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , probmod ); // } - match find_conflict_module(&self, &in_action) { + match find_conflict_module(&self, &in_action).await { // Some(c) => panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , c ), Some(c) => panic!("ERROR: Could not add module; there was a conflict with existing module {:?}", c ), None => (), } - let statusvector = self.statusdb + // { + // let mut lala = self; + // let statusvector = (*lala).statusdb + // // .entry(BotModule(String::from("experiments"))) + // .entry(in_module.clone()) + // .or_insert(Vec::new()); + // } + + let mut dbt = self.statusdb.write().await; + let statusvector = dbt // .entry(BotModule(String::from("experiments"))) .entry(in_module.clone()) .or_insert(Vec::new()); statusvector.push(ModStatusType::Enabled(StatusLvl::Instance)); // Pushes the Module as Enabled at Instance Level - let modactions = self.botactions + let mut a = self.botactions.write().await; + let modactions = a //.entry( BotModule(String::from("experiments"))) .entry( in_module.clone()) .or_insert(Vec::new()); diff --git a/src/core/identity.rs b/src/core/identity.rs index 224aa20..7026835 100644 --- a/src/core/identity.rs +++ b/src/core/identity.rs @@ -20,15 +20,20 @@ use std::cell::RefCell; use std::sync::{Arc, RwLock}; +use super::botmodules::bot_actions::actions_util::BotAR; + + fn adminvector() -> Vec { vec![String::from("ModulatingForce")] } // pub fn init(mgr:&mut ModulesManager) -pub fn init(mgr:Arc>) +pub fn init(mgr:Arc) { + let a = actions_util::asyncbox(cmd_promote) ; + BotCommand { module : BotModule(String::from("identity")), command : String::from("promote"), // command call name @@ -41,9 +46,11 @@ pub fn init(mgr:Arc>) UserRole::Broadcaster, UserRole::BotAdmin, ], - }.add_to_modmgr(mgr); + }.add_to_modmgr(Arc::clone(&mgr)); - async fn cmd_promote(mut bot:Arc>,msg:PrivmsgMessage) + // async fn cmd_promote(mut bot:Arc>,msg:PrivmsgMessage) + // async fn cmd_promote(mut bot:&BotInstance,msg:PrivmsgMessage) -> &BotInstance + async fn cmd_promote(bot:BotAR,msg:PrivmsgMessage) -> () { //println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); println!("Called cmd promote"); @@ -133,9 +140,11 @@ pub fn init(mgr:Arc>) // if let Some(a) = ta { - let p = bot.lock().await.get_identity(); - let mut p = p.lock().await; - let ta = p.getspecialuserroles(String::from("Hello"), Some(ChType::Channel(msg.channel_login.to_lowercase()))).await; + // let p = bot.lock().await.get_identity(); + // let mut p = p.lock().await; + // let ta = p.getspecialuserroles(String::from("Hello"), Some(ChType::Channel(msg.channel_login.to_lowercase()))).await; + let botlock = bot.read().await; + let ta = botlock.get_identity().read().await.getspecialuserroles(String::from("Hello"), Some(ChType::Channel(msg.channel_login.to_lowercase()))).await; if let Some(a) = ta { @@ -183,9 +192,13 @@ pub fn init(mgr:Arc>) // let mut mutex2 = Arc::clone(&mutex).into_inner().get_identity(); // let a = (*bot).lock().await.get_identity(); // let mut a = a.lock().await; - let p = bot.lock().await.get_identity(); - let mut p = p.lock().await; - let ta = p.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)).await; + // let p = bot.lock().await.get_identity(); + // let mut p = p.lock().await; + // let ta = p.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)).await; + let botlock = Arc::clone(&bot.read().await.get_identity()); + let idlock = botlock.write().await; + // let mut idlock = *idlock; + let ta = idlock.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)).await; match ta { // Success(_) => { @@ -227,7 +240,7 @@ pub fn init(mgr:Arc>) - + // bot @@ -245,10 +258,11 @@ pub fn init(mgr:Arc>) UserRole::Broadcaster, UserRole::BotAdmin, ], - }.add_to_modmgr(mgr); + }.add_to_modmgr(Arc::clone(&mgr)); - async fn cmd_demote(mut _chat:Arc>,_msg:PrivmsgMessage) { + // async fn cmd_demote(mut _chat:Arc>,_msg:PrivmsgMessage) { + async fn cmd_demote(mut _chat:BotAR,_msg:PrivmsgMessage) { println!("Called cmd demote"); } @@ -266,10 +280,11 @@ pub fn init(mgr:Arc>) UserRole::Broadcaster, UserRole::BotAdmin, ], - }.add_to_modmgr(mgr); + }.add_to_modmgr(Arc::clone(&mgr)); - async fn getroles(bot:Arc>,msg:PrivmsgMessage) { + // async fn getroles(bot:Arc>,msg:PrivmsgMessage) { + async fn getroles(bot:BotAR,msg:PrivmsgMessage) { println!("Called cmd getroles"); /* @@ -337,10 +352,14 @@ pub fn init(mgr:Arc>) // let a = a.into_inner(); // let a = a.identity; // let a = a.into_inner(); - let a = bot.clone(); - let a = a.lock().await; - let a = a.get_identity().await; + // let a = bot.clone(); // let a = a.lock().await; + // let a = a.get_identity(); + // let a = a.lock().await; + // let a = bot.get_identity(); + let botlock = bot.read().await; + let idlock = botlock.get_identity(); + let idlock = idlock.read().await; let sproles = match targetchnl { None => { // let bot = Rc::clone(&bot); @@ -352,7 +371,8 @@ pub fn init(mgr:Arc>) // let a = a.ok().getspecialuserroles(String::from(targetuser),None); // let a = bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None); // println!("Retrieved User Roles >> {:?}",a); - a.lock().await.getspecialuserroles(String::from(targetuser),None).await + // let a = idlock.read().await; + idlock.getspecialuserroles(String::from(targetuser),None).await }, Some(targetchnl) => { // let bot = Rc::clone(&bot); @@ -365,7 +385,8 @@ pub fn init(mgr:Arc>) // let a = bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None); // println!("Retrieved User Roles >> {:?}",a); // bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None) - a.lock().await.getspecialuserroles(String::from(targetuser),None).await + // let a = a.read().await; + idlock.getspecialuserroles(String::from(targetuser),None).await }, }; @@ -445,7 +466,8 @@ impl IdentityManager { // pub fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec) -> Result> // pub fn can_user_run_PRVMSG(&self,msg:&PrivmsgMessage,cmdreqroles:Vec) -> Permissible - pub async fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec) -> Permissible + // pub async fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec) -> Permissible + pub fn can_user_run_PRVMSG(&self,msg:&PrivmsgMessage,cmdreqroles:Vec) -> Permissible { // println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); @@ -497,9 +519,11 @@ impl IdentityManager { // [ ] Call can_user_run() + // (self,Permissible::Block) Permissible::Block } + pub async fn can_user_run(&self, usr:String, channelname:ChType, @@ -661,7 +685,8 @@ impl IdentityManager { Permissible::Block } - pub async fn promote(&mut self,trgchatter:String,channel:Option,trg_role:Option) -> ChangeResult { + // pub async fn promote(&mut self,trgchatter:String,channel:Option,trg_role:Option) -> ChangeResult { + pub async fn promote(&self,trgchatter:String,channel:Option,trg_role:Option) -> ChangeResult { // Note : If channel is none, getspecialuserroles() returns all roles for the user diff --git a/src/main.rs b/src/main.rs index 3ef7654..4278813 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,16 @@ use std::process::Output; use crate::core::botinstance::ArcBox; use crate::core::botinstance::BotInstance; +use tokio::sync::RwLock; +use std::sync::Arc; +pub type BotAR = Arc>; #[tokio::main] pub async fn main() { let bot = BotInstance::init(); - bot.clone().runner().await; + bot.runner().await; println!("ERROR : EXIT Game loop"); diff --git a/src/modules.rs b/src/modules.rs index 13e39ae..5d533c6 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -27,7 +27,7 @@ mod experiments; // // F : Send, // F : Send + ?Sized, // pub fn init(mgr:&mut ModulesManager) -pub fn init(mgr:Arc>) +pub fn init(mgr:Arc) { // Modules initializer loads modules into the bot // this is achieved by calling submodules that also have fn init() defined diff --git a/src/modules/experiments.rs b/src/modules/experiments.rs index 828d9f2..6548320 100644 --- a/src/modules/experiments.rs +++ b/src/modules/experiments.rs @@ -15,7 +15,7 @@ use std::future::Future; use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait, BotCommand,ChType}; -use crate::core::botmodules::bot_actions::actions_util; +use crate::core::botmodules::bot_actions::actions_util::{self, BotAR}; use crate::core::botinstance::{self,BotInstance}; use futures::lock::Mutex; @@ -32,7 +32,7 @@ use std::sync::{Arc, RwLock}; // pub fn init(mgr:&mut ModulesManager) -pub fn init(mgr:Arc>) +pub fn init(mgr:Arc) { @@ -49,7 +49,7 @@ pub fn init(mgr:Arc>) // }.add_to_modmgr(mgr); - BotCommand { + let botc1 = BotCommand { module : BotModule(String::from("experiments001")), command : String::from("test1"), // command call name alias : vec![String::from("tester1"),String::from("testy1")], // String of alternative names @@ -58,7 +58,9 @@ pub fn init(mgr:Arc>) required_roles : vec![ identity::UserRole::BotAdmin ], - }.add_to_modmgr(mgr); + }; + + botc1.add_to_modmgr(Arc::clone(&mgr)); @@ -69,13 +71,13 @@ pub fn init(mgr:Arc>) help : String::from("") }; - list1.add_to_modmgr(mgr); + list1.add_to_modmgr(Arc::clone(&mgr)); } -async fn good_girl(mut bot:Arc>,msg:PrivmsgMessage) +async fn good_girl(mut bot:BotAR,msg:PrivmsgMessage) { println!("In GoodGirl() Listener"); //println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); @@ -120,18 +122,22 @@ async fn good_girl(mut bot:Arc>,msg:PrivmsgMessage) // None => (), // } // Arc::try_unwrap(bot).ok().unwrap().botmgrs.chat.say_in_reply_to(&msg, String::from("GoodGirl xdd ")).await; - let a = bot.clone(); + // let a = bot.clone(); + let a = Arc::clone(&bot); // let a = a.read().ok().unwrap(); // let a = a.lock().await.get_botmgrs(); // let a = a.lock().await.rChat(); - let a = (*bot).lock().await.get_botmgrs(); - let a = a.lock().await.rChat(); - let mut a = (*a).lock().await; + // let a = (*bot).lock().await.get_botmgrs(); + // let a = a.lock().await.rChat(); + // let mut a = (*a).lock().await; + // let a = a.botmgrs.chat.say_in_reply_to(&msg, outmsg) // a.rbotmgrs().chat.say_in_reply_to(&msg, String::from("GoodGirl xdd ")).await; // a.lock().await.say_in_reply_to(&msg, String::from("GoodGirl xdd ")).await; - a.say_in_reply_to(&msg, String::from("GoodGirl xdd ")).await; + // a.botmgrs.chat.say_in_reply_to(&msg, String::from("GoodGirl xdd ")).await; + let botlock = a.read().await; + botlock.botmgrs.chat.say_in_reply_to(&msg, String::from("GoodGirl xdd ")).await; } @@ -140,7 +146,7 @@ async fn good_girl(mut bot:Arc>,msg:PrivmsgMessage) } -async fn testy(mut _chat:Arc>,_msg:PrivmsgMessage) +async fn testy(mut _chat:BotAR,_msg:PrivmsgMessage) { println!("testy triggered!")