2024.02.12 - WORKS

This commit is contained in:
ModulatingForce 2024-02-12 01:25:12 -05:00
parent 8d4db0c657
commit 2a7b53ddf8
6 changed files with 422 additions and 143 deletions

View file

@ -1,6 +1,7 @@
use futures::lock::Mutex; // use futures::lock::Mutex;
use tokio::sync::mpsc::UnboundedReceiver; use tokio::sync::mpsc::UnboundedReceiver;
use tokio::sync::RwLock;
use twitch_irc::login::StaticLoginCredentials; use twitch_irc::login::StaticLoginCredentials;
use twitch_irc::ClientConfig; use twitch_irc::ClientConfig;
use twitch_irc::SecureTCPTransport; use twitch_irc::SecureTCPTransport;
@ -20,6 +21,9 @@ use std::collections::HashMap;
use rand::Rng; 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; use crate::core::ratelimiter::RateLimiter;
@ -31,7 +35,7 @@ use crate::core::identity::{IdentityManager,Permissible};
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::{Arc, RwLock}; use std::sync::Arc;
// use futures::lock::Mutex; // use futures::lock::Mutex;
use std::pin::Pin; use std::pin::Pin;
@ -39,6 +43,9 @@ use std::pin::Pin;
//use std::borrow::Borrow; //use std::borrow::Borrow;
use core::borrow::Borrow; use core::borrow::Borrow;
// pub type BotAR = Arc<RwLock<BotInstance>>;
use super::botmodules::bot_actions::actions_util::BotAR;
#[derive(Debug, PartialEq, Eq, Hash, Clone)] #[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum ChType { pub enum ChType {
@ -48,11 +55,14 @@ pub enum ChType {
pub use ChType::Channel; pub use ChType::Channel;
pub enum ModType {
BotModule(String),
}
pub use ModType::BotModule;
// pub enum ModType {
// BotModule(String),
// }
// pub use ModType::BotModule;
#[derive(Clone)] #[derive(Clone)]
pub struct Chat { pub struct Chat {
@ -163,7 +173,7 @@ impl Chat {
#[derive(Clone)] #[derive(Clone)]
pub struct BotManagers { pub struct BotManagers {
// pub botmodules : ModulesManager, // pub botmodules : ModulesManager,
pub identity : Arc<Mutex<IdentityManager>>, pub identity : Arc<RwLock<IdentityManager>>,
pub chat : Chat, pub chat : Chat,
} }
@ -171,23 +181,27 @@ impl BotManagers {
pub fn init(ratelimiters:HashMap<ChType, RateLimiter>, pub fn init(ratelimiters:HashMap<ChType, RateLimiter>,
client:TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>) client:TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>)
-> Arc<Mutex<BotManagers>> { -> BotManagers {
let a = Arc::new(Mutex::new(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(), // botmodules : ModulesManager::init(),
identity : Arc::new(Mutex::new(IdentityManager::init())), identity : Arc::new(RwLock::new(IdentityManager::init())),
chat : Chat::init(ratelimiters,client), chat : Chat::init(ratelimiters,client),
})); }
a
} }
pub fn rIdentity(self) -> Arc<Mutex<IdentityManager>> { pub fn rIdentity(self) -> Arc<RwLock<IdentityManager>> {
self.identity self.identity
} }
pub fn rChat(&self) -> Arc<Mutex<Chat>> { // pub fn rChat(&self) -> Arc<Mutex<Chat>> {
Arc::new(Mutex::new(self.chat)) // Arc::new(Mutex::new(self.chat))
} // }
} }
@ -206,14 +220,16 @@ pub struct BotInstance
{ {
pub prefix : char, pub prefix : char,
pub bot_channel : ChType, pub bot_channel : ChType,
pub incoming_messages : UnboundedReceiver<ServerMessage>, // pub incoming_messages : UnboundedReceiver<ServerMessage>,
pub incoming_messages : Arc<RwLock<UnboundedReceiver<ServerMessage>>>,
// pub incoming_messages : RefCell<UnboundedReceiver<ServerMessage>>, // pub incoming_messages : RefCell<UnboundedReceiver<ServerMessage>>,
// pub chat : Chat, // pub chat : Chat,
pub botmodules : Arc<Mutex<ModulesManager>>, pub botmodules : Arc<ModulesManager>,
pub twitch_oauth : String, pub twitch_oauth : String,
pub bot_channels : Vec<ChType>, pub bot_channels : Vec<ChType>,
// pub identity : IdentityManager, // pub identity : IdentityManager,
pub botmgrs : Arc<Mutex<BotManagers>>, // pub botmgrs : Arc<Mutex<BotManagers>>,
pub botmgrs : BotManagers,
} }
@ -223,7 +239,8 @@ impl BotInstance
// pub fn init() -> BotInstance // pub fn init() -> BotInstance
pub fn init() -> Arc<BotInstance> // pub fn init() -> Arc<BotInstance>
pub fn init() -> BotInstance
{ {
dotenv().ok(); dotenv().ok();
@ -272,7 +289,7 @@ impl BotInstance
let b = BotInstance { let b = BotInstance {
prefix : prefix, prefix : prefix,
bot_channel : Channel(login_name) , bot_channel : Channel(login_name) ,
incoming_messages : incoming_messages, incoming_messages : Arc::new(RwLock::new(incoming_messages)),
//client : client, //client : client,
// chat : Chat { // chat : Chat {
// ratelimiters : ratelimiters, // ratelimiters : ratelimiters,
@ -289,7 +306,9 @@ impl BotInstance
//println!("{:?}",b.botmgrs.chat.ratelimiters); //println!("{:?}",b.botmgrs.chat.ratelimiters);
Arc::new(b) // Arc::new(b)
//Arc::new(RwLock::new(b))
b
} }
// async fn rcv_helper(self) -> Option<ServerMessage> { // async fn rcv_helper(self) -> Option<ServerMessage> {
@ -299,10 +318,14 @@ impl BotInstance
// } // }
// pub async fn runner(mut self) -> () { // 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 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 { let join_handle = tokio::spawn(async move {
@ -313,7 +336,7 @@ impl BotInstance
// let mut bot = Rc::new(RefCell::new(&self)); // let mut bot = Rc::new(RefCell::new(&self));
//let bot = Arc::new(Mutex::new(&self)); //let bot = Arc::new(Mutex::new(&self));
// let mut boxed_bot = Arc::new(RefCell::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 { // 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 { // while let Some(message) = a.incoming_messages.recv().await {
// let a = Arc::clone(&boxed_bot).into_inner(); // let a = Arc::clone(&boxed_bot).into_inner();
// while let Some(message) = a.incoming_messages.recv().await { // while let Some(message) = a.incoming_messages.recv().await {
let tempbot = boxed_bot.clone(); // let tempbot = boxed_bot.clone();
while let Some(message) = Arc::try_unwrap(tempbot.clone()).ok().unwrap().into_inner().incoming_messages.recv().await { // 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) = tempbot.into_inner().incoming_messages.recv().await {
//while let Some(message) = boxed_bot.borrow().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 // // 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; // (*a).listener_main_prvmsg(&msg).await;
// let a:Arc<RefCell<BotInstance>> = Arc::clone(&boxed_bot); // let a:Arc<RefCell<BotInstance>> = Arc::clone(&boxed_bot);
// a.into_inner().listener_main_prvmsg(&msg).await; // a.into_inner().listener_main_prvmsg(&msg).await;
let tempbot = boxed_bot.clone(); // let tempbot = boxed_bot.clone();
Arc::try_unwrap(tempbot.clone()).ok().unwrap().into_inner().listener_main_prvmsg(&msg).await ; // 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 // - BotCommand listener should likely need to be called within the above
@ -468,18 +519,32 @@ impl BotInstance
pub fn get_botmodules(self) -> Arc<Mutex<ModulesManager>> { pub fn get_botmodules(self) -> Arc<ModulesManager> {
// let a = self.botmodules; // let a = self.botmodules;
// Arc::clone(&Arc::new(Mutex::new(self.botmodules))) // Arc::clone(&Arc::new(Mutex::new(self.botmodules)))
// *self.botmodules
self.botmodules self.botmodules
} }
pub fn get_botactions(&self) -> &HashMap<ModType,Vec<BotAction>> { // pub fn get_botactions(self:&Self) -> (Self,HashMap<botmodules::ModType,Vec<BotAction>>) {
self.get_botactions() // // self.get_botactions()
} // // (*self,(*self).botmodules.rbotactions())
// (Self { bot_channel},(*self).botmodules.rbotactions())
// }
pub async fn get_botmgrs(self) -> Arc<Mutex<BotManagers>> { // pub fn get_botactions(&self) -> (Self,HashMap<botmodules::ModType,Vec<BotAction>>) {
// pub fn get_botactions(&self) -> HashMap<botmodules::ModType,Vec<BotAction>> {
// // 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::new(self.botmgrs)
// Arc::clone(&Arc::new(Mutex::new(self.botmgrs))) // Arc::clone(&Arc::new(Mutex::new(self.botmgrs)))
let a = self.botmgrs; let a = self.botmgrs;
@ -488,29 +553,75 @@ impl BotInstance
a a
} }
pub async fn get_identity(self) -> Arc<Mutex<IdentityManager>> { // pub fn get_identity(self:&Self) -> (Self,IdentityManager) {
// let a = self.botmgrs; pub fn get_identity(&self) -> Arc<RwLock<IdentityManager>> {
// Arc::clone(&Arc::new(Mutex::new(a.rIdentity()))) // // let a = self.botmgrs;
// let a = self.botmgrs; // // Arc::clone(&Arc::new(Mutex::new(a.rIdentity())))
// Arc::clone(&Arc::new(Mutex::new(a.rIdentity()))) // // let a = self.botmgrs;
let a = self.get_botmgrs().await; // // Arc::clone(&Arc::new(Mutex::new(a.rIdentity())))
let a = a.lock().await; // let a = self.get_botmgrs().await;
// let a = a.rIdentity(); // let a = a.lock().await;
let a = a.clone().identity; // // let a = a.rIdentity();
a.clone() // 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 // PRIVATE FUNCTIONS
// async fn listener_main_prvmsg(&mut 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(&mut self,msg:&PrivmsgMessage) -> () {
// async fn listener_main_prvmsg(self,msg:&PrivmsgMessage) -> () {
// async fn listener_main_prvmsg(self:Arc<Self>,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); // 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)
@ -526,7 +637,8 @@ impl BotInstance
// let mut boxed_bot = Arc::new(RwLock::new(self)); // let mut boxed_bot = Arc::new(RwLock::new(self));
// let 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 boxed_bot = Arc::new(Mutex::new(self));
// let bot = Arc::new(RwLock::new(self));
// for (_m,acts) in &self.botmodules.botactions { // for (_m,acts) in &self.botmodules.botactions {
@ -547,12 +659,27 @@ impl BotInstance
// for (_m,acts) in b.rbotactions() { // for (_m,acts) in b.rbotactions() {
// for (_m,acts) in (*Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().read().unwrap()).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.clone().into_inner();
let a = boxed_bot.lock().await; // let a = boxed_bot.lock().await;
//let a = a.lock().in //let a = a.lock().in
// for (_m,acts) in self.rbotactions() { // for (_m,acts) in self.rbotactions() {
// for (_m,acts) in a.read().ok().unwrap().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.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 { 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() {
// 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.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; 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 == Arc::try_unwrap(boxed_bot).ok().unwrap().read().unwrap().prefix.to_string() + alias.as_str() {
// if inpt == self.prefix.to_string() + alias.as_str() { // if inpt == self.prefix.to_string() + alias.as_str() {
// let a = boxed_bot.clone(); // 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().ok().unwrap().prefix.to_string() + alias.as_str() {
// if inpt == a.into_inner().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; confirmed_bot_command = true;
} }
} }
@ -671,14 +810,27 @@ impl BotInstance
// match a.lock().await.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await { // 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.lock().await;
let le = le.get_identity().await; // let le = le.clone();
let le = *le; // let le = le.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await;
let le = le.lock().await; // let le = self.get_identity().await.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await;
let le = le.clone(); // let le = self.botmgrs;
let le = le.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await; // let le = le.identity;
match le { // 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) => (), // Ok(Permissible::Allow) => (),
Permissible::Allow => { Permissible::Allow => {
println!("Executed as permissible"); println!("Executed as permissible");
@ -687,7 +839,15 @@ impl BotInstance
// c.execute(bot, msg.clone()).await; // c.execute(bot, msg.clone()).await;
// } // }
// let boxed_bot = Arc::new(RwLock::new(self)); // 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 => { Permissible::Block => {
@ -708,7 +868,10 @@ impl BotInstance
//let bot = Rc::clone(&bot).into_inner(); //let bot = Rc::clone(&bot).into_inner();
// l.execute(boxed_bot.clone(), msg.clone()).await // l.execute(boxed_bot.clone(), msg.clone()).await
// let boxed_bot = Arc::new(RwLock::new(self)); // 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"); println!("End of Separate Listener Main prvmsg");
// self
// bot
} }

View file

@ -6,16 +6,24 @@ use std::collections::HashMap;
use crate::core::identity; use crate::core::identity;
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::{Arc, RwLock}; use std::sync::Arc;
use tokio::sync::RwLock;
use std::future::Future; 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 crate::core::botinstance::{self, BotInstance};
use std::rc::Rc; use std::rc::Rc;
// use tokio::sync::RwLock;
use async_trait::async_trait; use async_trait::async_trait;
/* /*
@ -55,6 +63,9 @@ pub enum ChType {
pub use ChType::Channel; pub use ChType::Channel;
use twitch_irc::message::PrivmsgMessage; use twitch_irc::message::PrivmsgMessage;
use self::bot_actions::actions_util;
use self::bot_actions::actions_util::BotAR;
#[derive(Debug)] #[derive(Debug)]
enum StatusLvl { enum StatusLvl {
@ -78,11 +89,12 @@ pub enum BotAction
impl BotAction { impl BotAction {
// pub async fn execute(&self,m:botinstance::BotManagers,n:PrivmsgMessage){ // pub async fn execute(&self,m:botinstance::BotManagers,n:PrivmsgMessage){
pub async fn execute(&self,m:Arc<Mutex<BotInstance>>,n:PrivmsgMessage){ pub fn execute(&self,m:BotAR,n:PrivmsgMessage) -> ()
{
match self { match self {
BotAction::L(a) => a.execute(m,n).await, BotAction::L(a) => a.execute(m,n),
BotAction::C(a) => a.execute(m,n).await, BotAction::C(a) => a.execute(m,n),
_ => (), _ => (),
} }
@ -92,8 +104,9 @@ impl BotAction {
#[async_trait] #[async_trait]
pub trait BotActionTrait pub trait BotActionTrait
{ {
async fn add_to_bot(&self, bot:BotInstance); async fn add_to_bot(self, bot:BotInstance);
async fn add_to_modmgr(self,modmgr:Arc<Mutex<ModulesManager>>); // async fn add_to_modmgr(self,modmgr:&'static ModulesManager);
async fn add_to_modmgr(self,modmgr:Arc<ModulesManager>);
} }
// #[derive(Clone)] // #[derive(Clone)]
@ -111,25 +124,38 @@ impl BotCommand
{ {
// pub async fn execute(&self,m:botinstance::BotManagers,n:PrivmsgMessage){ // 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:Rc<&botinstance::BotManagers>,n:PrivmsgMessage){
pub async fn execute(&self,m:Arc<Mutex<BotInstance>>,n:PrivmsgMessage){ // pub async fn execute(&self,m:BotInstance,n:PrivmsgMessage){
(self.exec_body)(m,n).await; // (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] #[async_trait]
impl BotActionTrait for BotCommand 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 mgr = &mut bot.botmodules;
// let mut mgr = *mgr.lock().await; // let mut mgr = *mgr.lock().await;
// let mut mgr = &mut mgr; // let mut mgr = &mut mgr;
// (*self).add_to_modmgr(bot.botmodules);
self.add_to_modmgr(bot.botmodules); self.add_to_modmgr(bot.botmodules);
} }
async fn add_to_modmgr(self, modmgr:Arc<Mutex<ModulesManager>>) { // async fn add_to_modmgr(self, modmgr:Arc<Mutex<ModulesManager>>) {
async fn add_to_modmgr(self, modmgr:Arc<ModulesManager>) {
// modmgr.add_botaction(self.module.clone(), BotAction::C(self)) // modmgr.add_botaction(self.module.clone(), BotAction::C(self))
let modmgr = *modmgr.lock().await; // let modmgr = *modmgr.lock().await;
modmgr.add_botaction(self.module.clone(), BotAction::C(self)) // 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 crate::core::botinstance::{BotInstance, BotManagers, Chat};
use twitch_irc::message::PrivmsgMessage; use twitch_irc::message::PrivmsgMessage;
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::{Arc, RwLock}; use std::sync::{Arc};
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,RwLock};
pub type BotAM = Arc<Mutex<BotInstance>>;
pub type BotAR = Arc<RwLock<BotInstance>>;
// pub type ExecBody = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>; // pub type ExecBody = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
@ -170,8 +201,14 @@ pub mod bot_actions {
// pub type ExecBody = Box<dyn Fn(Arc<BotInstance>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>; // pub type ExecBody = Box<dyn Fn(Arc<BotInstance>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Arc<RefCell<BotInstance>>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>; // pub type ExecBody = Box<dyn Fn(Arc<RefCell<BotInstance>>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Arc<RwLock<BotInstance>>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>; // pub type ExecBody = Box<dyn Fn(Arc<RwLock<BotInstance>>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
pub type ExecBody = Box<dyn Fn(Arc<Mutex<BotInstance>>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>; // pub type ExecBody = Box<dyn Fn(Arc<Mutex<BotInstance>>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(BotInstance,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody<F> = Box<dyn Fn(&BotInstance,PrivmsgMessage) -> Pin<Box<dyn Future<Output=F> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(&'static BotInstance,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(&mut BotInstance,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
pub type ExecBody = Box<dyn Fn(BotAR,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Arc<BotInstance>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
//pub fn asyncbox<T,F>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody<F> //pub fn asyncbox<T,F>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody<F>
@ -187,9 +224,14 @@ pub mod bot_actions {
// pub fn asyncbox<T>(f: fn(Arc<BotInstance>,PrivmsgMessage) -> T) -> ExecBody // pub fn asyncbox<T>(f: fn(Arc<BotInstance>,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(Arc<RefCell<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody // pub fn asyncbox<T>(f: fn(Arc<RefCell<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(Arc<RwLock<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody // pub fn asyncbox<T>(f: fn(Arc<RwLock<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody
pub fn asyncbox<T>(f: fn(Arc<Mutex<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody // pub fn asyncbox<T>(f: fn(Arc<Mutex<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(BotInstance,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(&'static BotInstance,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(&mut BotInstance,PrivmsgMessage) -> T) -> ExecBody
pub fn asyncbox<T>(f: fn(BotAR,PrivmsgMessage) -> T) -> ExecBody
where where
//T: Future<Output=()> + Send + 'static //T: Future<Output=()> + Send + 'static
// T: Future<Output=F> + Send + 'static
T: Future<Output=()> + Send + 'static T: Future<Output=()> + Send + 'static
{ {
Box::new(move |a,b| Box::pin(f(a,b))) Box::new(move |a,b| Box::pin(f(a,b)))
@ -211,25 +253,41 @@ pub struct Listener
impl Listener impl Listener
{ {
pub async fn execute(&self,m:Arc<Mutex<BotInstance>>,n:PrivmsgMessage){ // pub async fn execute(&self,m:BotInstance,n:PrivmsgMessage){
(self.exec_body)(m,n).await; // (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] #[async_trait]
impl BotActionTrait for Listener 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 = &mut bot.botmodules;
let mgr = bot.botmodules; // let mgr = bot.botmodules;
self.add_to_modmgr(mgr); // self.add_to_modmgr(Arc::new(*mgr));
self.add_to_modmgr(bot.botmodules);
} }
// fn add_to_modmgr(self, modmgr:&mut ModulesManager) { // fn add_to_modmgr(self, modmgr:&mut ModulesManager) {
async fn add_to_modmgr(self, modmgr:Arc<Mutex<ModulesManager>>) { // async fn add_to_modmgr(self, modmgr:Arc<Mutex<ModulesManager>>) {
let modmgr = *modmgr.lock().await; // async fn add_to_modmgr(self, modmgr:&'static ModulesManager) {
modmgr.add_botaction(self.module.clone(), BotAction::L(self)) // async fn add_to_modmgr(self, modmgr:&'static ModulesManager) {
async fn add_to_modmgr(self, modmgr:Arc<ModulesManager>) {
// 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 pub struct ModulesManager
{ {
statusdb: HashMap<ModType,Vec<ModStatusType>>, // statusdb: HashMap<ModType,Vec<ModStatusType>>,
pub botactions: HashMap<ModType,Vec<BotAction>>, statusdb: Arc<RwLock<HashMap<ModType,Vec<ModStatusType>>>>,
// pub botactions: HashMap<ModType,Vec<BotAction>>,
pub botactions: Arc<RwLock<HashMap<ModType,Vec<BotAction>>>>,
} }
impl ModulesManager impl ModulesManager
{ {
pub fn init() -> Arc<Mutex<ModulesManager>> // pub fn init() -> Arc<Mutex<ModulesManager>>
pub fn init() -> Arc<ModulesManager>
{ {
@ -258,33 +319,40 @@ impl ModulesManager
let act = HashMap::new(); let act = HashMap::new();
let mut mgr = ModulesManager { let mut mgr = ModulesManager {
statusdb : m, statusdb : Arc::new(RwLock::new(m)),
botactions : act, botactions : Arc::new(RwLock::new(act)),
}; };
// :: [x] initialize core modules // :: [x] initialize core modules
// crate::core::identity::init(&mut mgr); // crate::core::identity::init(&mut mgr);
let a = Arc::new(Mutex::new(mgr)); // let a = Arc::new(Mutex::new(mgr));
// let a = a.clone(); // // let a = a.clone();
crate::core::identity::init(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 // initialize custom crate modules
// crate::modules::init(&mut mgr); // crate::modules::init(&mut mgr);
// let a = a.clone(); // 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"); println!(">> Modules Manager : End of Init");
// mgr // mgr
a mgra
} }
pub fn rbotactions(self) -> HashMap<ModType,Vec<BotAction>> { // pub async fn rbotactions(&self) -> HashMap<ModType,Vec<BotAction>> {
self.botactions // // (*self).botactions
} // let a = self.botactions.read().await;
// *a
// }
pub fn modstatus(&self, _:ModType, _:ChType) -> ModStatusType { 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<F> ) -> ModulesManager<F> { // pub fn add_botaction(mut self, in_module:ModType, in_action:BotAction<F> ) -> ModulesManager<F> {
//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 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 adds a BotAction to the Modules Manager - This will require a BotModule passed as well
This will including the logic of a valid add 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), // - 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 // Check All Other BotAction Command Names & Aliases to ensure they don't conflict
fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option<ModType> async fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option<ModType>
{ {
// Some(BotModule(String::from("GambaCore"))) // Some(BotModule(String::from("GambaCore")))
@ -360,7 +428,8 @@ impl ModulesManager
// let n = & mgr.botactions; // let n = & mgr.botactions;
let d = &mgr.botactions; let d = mgr.botactions.read().await;
let d = &(*d);
for (module,moduleactions) in d { for (module,moduleactions) in d {
@ -431,20 +500,30 @@ impl ModulesManager
// // () // return because there was a conflict? // // () // return because there was a conflict?
// panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , probmod ); // 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 {:?} ; 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 ), Some(c) => panic!("ERROR: Could not add module; there was a conflict with existing module {:?}", c ),
None => (), 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(BotModule(String::from("experiments")))
.entry(in_module.clone()) .entry(in_module.clone())
.or_insert(Vec::new()); .or_insert(Vec::new());
statusvector.push(ModStatusType::Enabled(StatusLvl::Instance)); // Pushes the Module as Enabled at Instance Level 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( BotModule(String::from("experiments")))
.entry( in_module.clone()) .entry( in_module.clone())
.or_insert(Vec::new()); .or_insert(Vec::new());

View file

@ -20,15 +20,20 @@ use std::cell::RefCell;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use super::botmodules::bot_actions::actions_util::BotAR;
fn adminvector() -> Vec<String> { fn adminvector() -> Vec<String> {
vec![String::from("ModulatingForce")] vec![String::from("ModulatingForce")]
} }
// pub fn init(mgr:&mut ModulesManager) // pub fn init(mgr:&mut ModulesManager)
pub fn init(mgr:Arc<Mutex<ModulesManager>>) pub fn init(mgr:Arc<ModulesManager>)
{ {
let a = actions_util::asyncbox(cmd_promote) ;
BotCommand { BotCommand {
module : BotModule(String::from("identity")), module : BotModule(String::from("identity")),
command : String::from("promote"), // command call name command : String::from("promote"), // command call name
@ -41,9 +46,11 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
UserRole::Broadcaster, UserRole::Broadcaster,
UserRole::BotAdmin, UserRole::BotAdmin,
], ],
}.add_to_modmgr(mgr); }.add_to_modmgr(Arc::clone(&mgr));
async fn cmd_promote(mut bot:Arc<Mutex<BotInstance>>,msg:PrivmsgMessage) // async fn cmd_promote(mut bot:Arc<Mutex<BotInstance>>,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!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
println!("Called cmd promote"); println!("Called cmd promote");
@ -133,9 +140,11 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
// if let Some(a) = ta { // if let Some(a) = ta {
let p = bot.lock().await.get_identity(); // let p = bot.lock().await.get_identity();
let mut p = p.lock().await; // let mut p = p.lock().await;
let ta = p.getspecialuserroles(String::from("Hello"), Some(ChType::Channel(msg.channel_login.to_lowercase()))).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 { if let Some(a) = ta {
@ -183,9 +192,13 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
// let mut mutex2 = Arc::clone(&mutex).into_inner().get_identity(); // let mut mutex2 = Arc::clone(&mutex).into_inner().get_identity();
// let a = (*bot).lock().await.get_identity(); // let a = (*bot).lock().await.get_identity();
// let mut a = a.lock().await; // let mut a = a.lock().await;
let p = bot.lock().await.get_identity(); // let p = bot.lock().await.get_identity();
let mut p = p.lock().await; // let mut p = p.lock().await;
let ta = p.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)).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 { match ta {
// Success(_) => { // Success(_) => {
@ -227,7 +240,7 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
// bot
@ -245,10 +258,11 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
UserRole::Broadcaster, UserRole::Broadcaster,
UserRole::BotAdmin, UserRole::BotAdmin,
], ],
}.add_to_modmgr(mgr); }.add_to_modmgr(Arc::clone(&mgr));
async fn cmd_demote(mut _chat:Arc<Mutex<BotInstance>>,_msg:PrivmsgMessage) { // async fn cmd_demote(mut _chat:Arc<Mutex<BotInstance>>,_msg:PrivmsgMessage) {
async fn cmd_demote(mut _chat:BotAR,_msg:PrivmsgMessage) {
println!("Called cmd demote"); println!("Called cmd demote");
} }
@ -266,10 +280,11 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
UserRole::Broadcaster, UserRole::Broadcaster,
UserRole::BotAdmin, UserRole::BotAdmin,
], ],
}.add_to_modmgr(mgr); }.add_to_modmgr(Arc::clone(&mgr));
async fn getroles(bot:Arc<Mutex<BotInstance>>,msg:PrivmsgMessage) { // async fn getroles(bot:Arc<Mutex<BotInstance>>,msg:PrivmsgMessage) {
async fn getroles(bot:BotAR,msg:PrivmsgMessage) {
println!("Called cmd getroles"); println!("Called cmd getroles");
/* /*
@ -337,10 +352,14 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
// let a = a.into_inner(); // let a = a.into_inner();
// let a = a.identity; // let a = a.identity;
// let a = a.into_inner(); // let a = a.into_inner();
let a = bot.clone(); // let a = bot.clone();
let a = a.lock().await;
let a = a.get_identity().await;
// let a = a.lock().await; // 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 { let sproles = match targetchnl {
None => { None => {
// let bot = Rc::clone(&bot); // let bot = Rc::clone(&bot);
@ -352,7 +371,8 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
// let a = a.ok().getspecialuserroles(String::from(targetuser),None); // let a = a.ok().getspecialuserroles(String::from(targetuser),None);
// let a = bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None); // let a = bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None);
// println!("Retrieved User Roles >> {:?}",a); // 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) => { Some(targetchnl) => {
// let bot = Rc::clone(&bot); // let bot = Rc::clone(&bot);
@ -365,7 +385,8 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
// let a = bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None); // let a = bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None);
// println!("Retrieved User Roles >> {:?}",a); // println!("Retrieved User Roles >> {:?}",a);
// bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None) // 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<UserRole>) -> Result<Permissible,Box<dyn Error>> // pub fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Result<Permissible,Box<dyn Error>>
// pub fn can_user_run_PRVMSG(&self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Permissible // pub fn can_user_run_PRVMSG(&self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Permissible
pub async fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Permissible // pub async fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Permissible
pub fn can_user_run_PRVMSG(&self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Permissible
{ {
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); // println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
@ -497,9 +519,11 @@ impl IdentityManager {
// [ ] Call can_user_run() // [ ] Call can_user_run()
// (self,Permissible::Block)
Permissible::Block Permissible::Block
} }
pub async fn can_user_run(&self, pub async fn can_user_run(&self,
usr:String, usr:String,
channelname:ChType, channelname:ChType,
@ -661,7 +685,8 @@ impl IdentityManager {
Permissible::Block Permissible::Block
} }
pub async fn promote(&mut self,trgchatter:String,channel:Option<ChType>,trg_role:Option<UserRole>) -> ChangeResult { // pub async fn promote(&mut self,trgchatter:String,channel:Option<ChType>,trg_role:Option<UserRole>) -> ChangeResult {
pub async fn promote(&self,trgchatter:String,channel:Option<ChType>,trg_role:Option<UserRole>) -> ChangeResult {
// Note : If channel is none, getspecialuserroles() returns all roles for the user // Note : If channel is none, getspecialuserroles() returns all roles for the user

View file

@ -7,13 +7,16 @@ use std::process::Output;
use crate::core::botinstance::ArcBox; use crate::core::botinstance::ArcBox;
use crate::core::botinstance::BotInstance; use crate::core::botinstance::BotInstance;
use tokio::sync::RwLock;
use std::sync::Arc;
pub type BotAR = Arc<RwLock<BotInstance>>;
#[tokio::main] #[tokio::main]
pub async fn main() { pub async fn main() {
let bot = BotInstance::init(); let bot = BotInstance::init();
bot.clone().runner().await; bot.runner().await;
println!("ERROR : EXIT Game loop"); println!("ERROR : EXIT Game loop");

View file

@ -27,7 +27,7 @@ mod experiments;
// // F : Send, // // F : Send,
// F : Send + ?Sized, // F : Send + ?Sized,
// pub fn init(mgr:&mut ModulesManager) // pub fn init(mgr:&mut ModulesManager)
pub fn init(mgr:Arc<Mutex<ModulesManager>>) pub fn init(mgr:Arc<ModulesManager>)
{ {
// Modules initializer loads modules into the bot // Modules initializer loads modules into the bot
// this is achieved by calling submodules that also have fn init() defined // this is achieved by calling submodules that also have fn init() defined

View file

@ -15,7 +15,7 @@
use std::future::Future; use std::future::Future;
use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait, BotCommand,ChType}; 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 crate::core::botinstance::{self,BotInstance};
use futures::lock::Mutex; use futures::lock::Mutex;
@ -32,7 +32,7 @@ use std::sync::{Arc, RwLock};
// pub fn init(mgr:&mut ModulesManager) // pub fn init(mgr:&mut ModulesManager)
pub fn init(mgr:Arc<Mutex<ModulesManager>>) pub fn init(mgr:Arc<ModulesManager>)
{ {
@ -49,7 +49,7 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
// }.add_to_modmgr(mgr); // }.add_to_modmgr(mgr);
BotCommand { let botc1 = BotCommand {
module : BotModule(String::from("experiments001")), module : BotModule(String::from("experiments001")),
command : String::from("test1"), // command call name command : String::from("test1"), // command call name
alias : vec![String::from("tester1"),String::from("testy1")], // String of alternative names alias : vec![String::from("tester1"),String::from("testy1")], // String of alternative names
@ -58,7 +58,9 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
required_roles : vec![ required_roles : vec![
identity::UserRole::BotAdmin identity::UserRole::BotAdmin
], ],
}.add_to_modmgr(mgr); };
botc1.add_to_modmgr(Arc::clone(&mgr));
@ -69,13 +71,13 @@ pub fn init(mgr:Arc<Mutex<ModulesManager>>)
help : String::from("") help : String::from("")
}; };
list1.add_to_modmgr(mgr); list1.add_to_modmgr(Arc::clone(&mgr));
} }
async fn good_girl(mut bot:Arc<Mutex<BotInstance>>,msg:PrivmsgMessage) async fn good_girl(mut bot:BotAR,msg:PrivmsgMessage)
{ {
println!("In GoodGirl() Listener"); println!("In GoodGirl() Listener");
//println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); //println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
@ -120,18 +122,22 @@ async fn good_girl(mut bot:Arc<Mutex<BotInstance>>,msg:PrivmsgMessage)
// None => (), // None => (),
// } // }
// Arc::try_unwrap(bot).ok().unwrap().botmgrs.chat.say_in_reply_to(&msg, String::from("GoodGirl xdd ")).await; // 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.read().ok().unwrap();
// let a = a.lock().await.get_botmgrs(); // let a = a.lock().await.get_botmgrs();
// let a = a.lock().await.rChat(); // let a = a.lock().await.rChat();
let a = (*bot).lock().await.get_botmgrs(); // let a = (*bot).lock().await.get_botmgrs();
let a = a.lock().await.rChat(); // let a = a.lock().await.rChat();
let mut a = (*a).lock().await; // 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.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.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<Mutex<BotInstance>>,msg:PrivmsgMessage)
} }
async fn testy(mut _chat:Arc<Mutex<BotInstance>>,_msg:PrivmsgMessage) async fn testy(mut _chat:BotAR,_msg:PrivmsgMessage)
{ {
println!("testy triggered!") println!("testy triggered!")