2024.02.12 - WORKS

This commit is contained in:
ModulatingForce 2024-02-12 01:25:12 -05:00
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::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<RwLock<BotInstance>>;
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<Mutex<IdentityManager>>,
pub identity : Arc<RwLock<IdentityManager>>,
pub chat : Chat,
}
@ -171,23 +181,27 @@ impl BotManagers {
pub fn init(ratelimiters:HashMap<ChType, RateLimiter>,
client:TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>)
-> Arc<Mutex<BotManagers>> {
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<Mutex<IdentityManager>> {
pub fn rIdentity(self) -> Arc<RwLock<IdentityManager>> {
self.identity
}
pub fn rChat(&self) -> Arc<Mutex<Chat>> {
Arc::new(Mutex::new(self.chat))
}
// pub fn rChat(&self) -> Arc<Mutex<Chat>> {
// Arc::new(Mutex::new(self.chat))
// }
}
@ -206,14 +220,16 @@ pub struct BotInstance
{
pub prefix : char,
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 chat : Chat,
pub botmodules : Arc<Mutex<ModulesManager>>,
pub botmodules : Arc<ModulesManager>,
pub twitch_oauth : String,
pub bot_channels : Vec<ChType>,
// 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() -> Arc<BotInstance>
// pub fn init() -> Arc<BotInstance>
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<ServerMessage> {
@ -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<RefCell<BotInstance>> = 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<Mutex<ModulesManager>> {
pub fn get_botmodules(self) -> Arc<ModulesManager> {
// let a = self.botmodules;
// Arc::clone(&Arc::new(Mutex::new(self.botmodules)))
// *self.botmodules
self.botmodules
}
pub fn get_botactions(&self) -> &HashMap<ModType,Vec<BotAction>> {
self.get_botactions()
}
// pub fn get_botactions(self:&Self) -> (Self,HashMap<botmodules::ModType,Vec<BotAction>>) {
// // 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::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<Mutex<IdentityManager>> {
// 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<RwLock<IdentityManager>> {
// // 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<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);
// // [ ] 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
}