forcebot_rs/src/core/botinstance.rs

944 lines
40 KiB
Rust
Raw Normal View History

2023-12-19 20:38:20 -05:00
2024-02-12 01:25:12 -05:00
// use futures::lock::Mutex;
2023-12-19 20:38:20 -05:00
use tokio::sync::mpsc::UnboundedReceiver;
2024-02-12 01:25:12 -05:00
use tokio::sync::RwLock;
2023-12-19 20:38:20 -05:00
use twitch_irc::login::StaticLoginCredentials;
use twitch_irc::ClientConfig;
use twitch_irc::SecureTCPTransport;
use twitch_irc::TwitchIRCClient;
2023-12-20 17:55:46 -05:00
use twitch_irc::message::PrivmsgMessage;
2023-12-19 20:38:20 -05:00
use twitch_irc::message::ServerMessage;
use twitch_irc::transport::tcp::TCPTransport;
use twitch_irc::transport::tcp::TLS;
2024-02-04 14:28:37 -05:00
// use std::borrow::Borrow;
use std::borrow::BorrowMut;
use std::boxed;
use std::cell::Ref;
2023-12-19 20:38:20 -05:00
use std::env;
use dotenv::dotenv;
2023-12-19 21:08:48 -05:00
use std::collections::HashMap;
2023-12-19 21:43:03 -05:00
use rand::Rng;
2024-02-12 01:25:12 -05:00
// Important to use tokios Mutex here since std Mutex doesn't work with async functions
use tokio::sync::Mutex;
2023-12-19 21:08:48 -05:00
use crate::core::botmodules::BotAction;
2024-02-01 09:43:39 -05:00
use crate::core::botmodules::BotAction;
2023-12-19 21:08:48 -05:00
use crate::core::ratelimiter::RateLimiter;
2023-12-19 21:43:03 -05:00
use crate::core::ratelimiter;
2023-12-21 00:48:09 -05:00
use crate::core::botmodules;
2024-02-04 14:28:37 -05:00
use crate::core::botmodules::{ModulesManager,BotAction};
2024-01-29 22:57:07 -05:00
use crate::core::identity::{IdentityManager,Permissible};
2024-01-29 06:18:27 -05:00
2024-02-04 14:28:37 -05:00
use std::rc::Rc;
use std::cell::RefCell;
2024-02-12 01:25:12 -05:00
use std::sync::Arc;
2024-02-04 14:28:37 -05:00
// use futures::lock::Mutex;
use std::pin::Pin;
//use std::borrow::Borrow;
use core::borrow::Borrow;
2024-02-12 01:25:12 -05:00
// pub type BotAR = Arc<RwLock<BotInstance>>;
use super::botmodules::bot_actions::actions_util::BotAR;
2023-12-21 00:48:09 -05:00
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
2023-12-20 20:25:20 -05:00
pub enum ChType {
Channel(String),
2023-12-19 22:21:56 -05:00
}
2023-12-19 21:08:48 -05:00
2023-12-20 20:25:20 -05:00
pub use ChType::Channel;
2023-12-20 20:52:20 -05:00
2024-02-12 01:25:12 -05:00
// pub enum ModType {
// BotModule(String),
// }
// pub use ModType::BotModule;
2023-12-20 20:52:20 -05:00
#[derive(Clone)]
2023-12-22 21:09:36 -05:00
pub struct Chat {
2024-02-04 14:28:37 -05:00
// pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
pub ratelimiters : Arc<Mutex<HashMap<ChType,RateLimiter>>>, // used to limit messages sent per channel
2023-12-22 21:09:36 -05:00
pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>,
}
impl Chat {
2024-01-31 01:07:27 -05:00
pub fn init(ratelimiters:HashMap<ChType, RateLimiter>,
client:TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>) -> Chat {
Chat{
2024-02-04 14:28:37 -05:00
ratelimiters : Arc::new(Mutex::new(ratelimiters)),
2024-01-31 01:07:27 -05:00
client : client,
}
}
2024-02-04 14:28:37 -05:00
// pub fn init_channel(&mut self, chnl:ChType) -> () {
pub async fn init_channel(&mut self, chnl:ChType) -> () {
2023-12-22 21:09:36 -05:00
let n = RateLimiter::new();
2024-02-04 14:28:37 -05:00
self.ratelimiters.lock().await.insert(chnl,n);
2023-12-22 21:09:36 -05:00
}
2024-02-04 14:28:37 -05:00
// pub async fn say_in_reply_to(&mut self, msg:& PrivmsgMessage , mut outmsg:String) -> () {
pub async fn say_in_reply_to(&self, msg:& PrivmsgMessage , mut outmsg:String) -> () {
2024-01-29 03:20:56 -05:00
/*
formats message before sending to TwitchIRC
2024-01-29 04:09:53 -05:00
2024-01-29 03:20:56 -05:00
- [x] Custom String Formatting (e.g., adding random black spaces)
- [x] Ratelimiter Handling
- [ ] Checkf if BotActions is Enabled & Caller is Allowed to Run
*/
2023-12-22 21:09:36 -05:00
// self.client.say_in_reply_to(msg,outmsg).await.unwrap();
// // let contextratelimiter = ratelimiters.get_mut(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
2024-02-04 14:28:37 -05:00
let a = Arc::clone(&self.ratelimiters);
let mut a = a.lock().await;
// let contextratelimiter = self.ratelimiters
let contextratelimiter = a
// .get_mut()
2023-12-22 21:09:36 -05:00
.get_mut(&Channel(String::from(&msg.channel_login)))
.expect("ERROR: Issue with Rate limiters");
// let contextratelimiter = self.ratelimiters.get(&msg.channel_login).expect("ERROR: Issue with Rate limiters");
match contextratelimiter.check_limiter() {
ratelimiter::LimiterResp::Allow => {
let maxblanks = rand::thread_rng().gen_range(1..=20);
//let mut outmsg = "GotTrolled ".to_owned();
// let mut outmsg = "annytfLurk ".to_owned();
for _i in 1..maxblanks {
let blankspace: &str = "󠀀";
outmsg.push_str(blankspace);
}
// client.say_in_reply_to(&msg,outmsg).await.unwrap();
self.client.say_in_reply_to(msg,outmsg).await.unwrap();
println!("(#{}) > {}", msg.channel_login, "rate limit counter increase");
contextratelimiter.increment_counter();
println!("{:?}",self.ratelimiters);
},
ratelimiter::LimiterResp::Skip => {
(); // do nothing otherwise
}
}
}
async fn say(&self, _:String, _:String) -> () {
// more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say
// self.client.say(msg,outmsg).await.unwrap();
}
async fn me(&self, _:String, _:String) -> () {
// more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say
// self.client.me(msg,outmsg).await.unwrap();
}
async fn me_in_reply_to(&self, _:String, _:String) -> () {
// more info https://docs.rs/twitch-irc/latest/twitch_irc/client/struct.TwitchIRCClient.html#method.say
// self.client.me(msg,outmsg).await.unwrap();
}
}
2024-01-31 18:36:23 -05:00
#[derive(Clone)]
2024-01-30 20:21:58 -05:00
pub struct BotManagers {
2024-01-31 18:36:23 -05:00
// pub botmodules : ModulesManager,
2024-02-12 01:25:12 -05:00
pub identity : Arc<RwLock<IdentityManager>>,
2024-01-31 01:11:45 -05:00
pub chat : Chat,
2024-01-30 20:21:58 -05:00
}
impl BotManagers {
2024-01-31 01:11:45 -05:00
pub fn init(ratelimiters:HashMap<ChType, RateLimiter>,
client:TwitchIRCClient<TCPTransport<TLS>, StaticLoginCredentials>)
-> BotManagers {
2024-02-12 01:25:12 -05:00
// let a = Arc::new(Mutex::new(BotManagers {
// // botmodules : ModulesManager::init(),
// identity : Arc::new(Mutex::new(IdentityManager::init())),
// chat : Chat::init(ratelimiters,client),
// }));
// a
2024-01-30 20:21:58 -05:00
BotManagers {
2024-01-31 18:36:23 -05:00
// botmodules : ModulesManager::init(),
2024-02-12 01:25:12 -05:00
identity : Arc::new(RwLock::new(IdentityManager::init())),
2024-01-31 01:11:45 -05:00
chat : Chat::init(ratelimiters,client),
2024-01-30 20:21:58 -05:00
}
2024-02-04 14:28:37 -05:00
}
2024-02-12 01:25:12 -05:00
pub fn rIdentity(self) -> Arc<RwLock<IdentityManager>> {
2024-02-04 14:28:37 -05:00
self.identity
}
2024-01-31 09:31:14 -05:00
2024-02-12 01:25:12 -05:00
// pub fn rChat(&self) -> Arc<Mutex<Chat>> {
// Arc::new(Mutex::new(self.chat))
// }
2024-02-04 14:28:37 -05:00
}
2024-01-31 09:31:14 -05:00
2024-02-04 14:28:37 -05:00
pub struct ArcBox<T: Clone>(pub Arc<Mutex<T>>);
2024-01-31 09:31:14 -05:00
2024-02-04 14:28:37 -05:00
impl<T: Clone> ArcBox<T>{
pub fn inst(&self) -> &Mutex<T> {
&self.0
2024-01-30 20:21:58 -05:00
}
2024-02-04 14:28:37 -05:00
2024-01-30 20:21:58 -05:00
}
2024-02-04 14:28:37 -05:00
//#[derive(Clone)]
// #[derive(Copy)] // <-- Cannot be derived
pub struct BotInstance
{
2024-02-04 14:28:37 -05:00
pub prefix : char,
pub bot_channel : ChType,
2024-02-12 01:25:12 -05:00
// pub incoming_messages : UnboundedReceiver<ServerMessage>,
pub incoming_messages : Arc<RwLock<UnboundedReceiver<ServerMessage>>>,
2024-02-04 14:28:37 -05:00
// pub incoming_messages : RefCell<UnboundedReceiver<ServerMessage>>,
2024-01-31 01:11:45 -05:00
// pub chat : Chat,
2024-02-12 01:25:12 -05:00
pub botmodules : Arc<ModulesManager>,
2024-02-04 14:28:37 -05:00
pub twitch_oauth : String,
2023-12-20 20:25:20 -05:00
pub bot_channels : Vec<ChType>,
2024-01-30 20:21:58 -05:00
// pub identity : IdentityManager,
2024-02-12 01:25:12 -05:00
// pub botmgrs : Arc<Mutex<BotManagers>>,
2024-01-30 20:21:58 -05:00
pub botmgrs : BotManagers,
2023-12-19 20:38:20 -05:00
}
2023-12-20 17:55:46 -05:00
impl BotInstance
{
2024-02-04 14:28:37 -05:00
// pub fn init() -> BotInstance
2024-02-12 01:25:12 -05:00
// pub fn init() -> Arc<BotInstance>
2024-02-12 02:34:32 -05:00
pub async fn init() -> BotInstance
{
2023-12-19 20:38:20 -05:00
dotenv().ok();
2023-12-20 19:12:53 -05:00
let login_name = env::var("login_name").unwrap().to_owned();
2023-12-19 20:38:20 -05:00
let oauth_token = env::var("access_token").unwrap().to_owned();
2023-12-20 19:22:45 -05:00
let prefix = env::var("prefix").unwrap().to_owned().chars().next().expect("ERROR : when defining prefix");
2023-12-19 20:38:20 -05:00
/*
Vector of channels to join
*/
let mut botchannels = Vec::new();
for chnl in env::var("bot_channels").unwrap().split(',') {
// println!("(Env Var # {})",chnl);
2023-12-20 20:25:20 -05:00
botchannels.push(Channel(String::from(chnl)));
2023-12-19 20:38:20 -05:00
}
let config = ClientConfig::new_simple(
StaticLoginCredentials::new(login_name.to_owned(), Some(oauth_token.to_owned()))
);
let (incoming_messages, client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(config);
2023-12-20 20:25:20 -05:00
// hashmap for channels and their associated ratelimiters
let mut ratelimiters = HashMap::new();
for Channel(chnl) in &botchannels {
// For each channel in botchannels
2023-12-19 20:38:20 -05:00
client.join(chnl.to_owned()).unwrap();
2023-12-20 20:25:20 -05:00
2023-12-20 20:27:01 -05:00
// ratelimiters are a hashmap of channel and a corresponding rate limiter
2023-12-20 20:25:20 -05:00
let n = RateLimiter::new();
ratelimiters.insert(Channel(String::from(chnl)),n);
2023-12-22 21:09:36 -05:00
//self.chat.ratelimiters.insert(Channel(String::from(chnl)),n);
2023-12-19 20:38:20 -05:00
}
2023-12-22 21:09:36 -05:00
// let bm = &mut ModulesManager::init();
2023-12-19 21:08:48 -05:00
2023-12-20 20:25:20 -05:00
let b = BotInstance {
2023-12-20 19:22:45 -05:00
prefix : prefix,
2023-12-20 20:25:20 -05:00
bot_channel : Channel(login_name) ,
2024-02-12 01:25:12 -05:00
incoming_messages : Arc::new(RwLock::new(incoming_messages)),
2023-12-22 21:09:36 -05:00
//client : client,
2024-01-31 01:11:45 -05:00
// chat : Chat {
// ratelimiters : ratelimiters,
// client : client,
// } ,
2024-02-12 02:34:32 -05:00
botmodules : ModulesManager::init().await,
2023-12-19 20:38:20 -05:00
twitch_oauth : oauth_token,
2024-01-29 03:20:56 -05:00
bot_channels : botchannels,
2024-01-30 20:21:58 -05:00
// identity : IdentityManager::init(),
2024-01-31 01:11:45 -05:00
botmgrs : BotManagers::init(ratelimiters,client),
2023-12-19 21:08:48 -05:00
};
2024-02-04 14:28:37 -05:00
//println!("{:?}",b.botmgrs.chat.ratelimiters);
2023-12-19 21:08:48 -05:00
2024-02-12 01:25:12 -05:00
// Arc::new(b)
//Arc::new(RwLock::new(b))
2023-12-19 21:08:48 -05:00
b
2023-12-19 20:38:20 -05:00
}
2024-02-04 14:28:37 -05:00
// async fn rcv_helper(self) -> Option<ServerMessage> {
// // self.incoming_messages.get_mut().recv().await
// let mut a = self.incoming_messages;
// a.get_mut().recv().await
// }
// pub async fn runner(mut self) -> () {
2024-02-12 01:25:12 -05:00
// pub async fn runner(&'static mut self) -> () {
pub async fn runner(self) -> () {
2024-02-04 14:28:37 -05:00
2024-02-12 01:25:12 -05:00
// let bot_am = Arc::new(Mutex::new(self));
2024-02-04 14:28:37 -05:00
// let mut boxed_bot = Arc::new(RefCell::new(self)); // <-- [ERROR] Future cannot be handled safely
2024-02-12 01:25:12 -05:00
let bot = Arc::new(RwLock::new(self));
2024-02-04 14:28:37 -05:00
let join_handle = tokio::spawn(async move {
2024-02-04 14:28:37 -05:00
// let boxed_bot = Arc::new(Mutex::new(self));
// let mut boxed_bot = Arc::new(self);
// let bot = Rc::new(RefCell::new(self));
// let mut bot = Rc::new(RefCell::new(&self));
//let bot = Arc::new(Mutex::new(&self));
// let mut boxed_bot = Arc::new(RefCell::new(self));
2024-02-12 01:25:12 -05:00
// let mut boxed_bot = Arc::new(Mutex::new(self));
2024-02-04 14:28:37 -05:00
// while let Some(message) = bot.borrow_mut().incoming_messages.recv().await {
// let bot = Arc::clone(&botinit);
// while let Some(message) = bot.lock().unwrap().incoming_messages.recv().await {
//let b = Arc::clone(&bot);
// let mut bot = RefCell::new(&self);
// let mut bota = bot.clone().borrow();
// let boxed_bot = Rc::new(RefCell::new(self));
// let boxed_bot = Rc::new(self);
// let boxed_bot = Pin::new(Rc::new(self));
//let mut boxed_bot = Rc::new(RefCell::new(self));
// let mut a = (*boxed_bot).clone().into_inner();
// let mut a = Rc::clone(&boxed_bot).borrow_mut();
//let mut a = Rc::<Rc<RefCell<BotInstance>>>::Borrow(Rc::clone(&boxed_bot));
// while let Some(message) = Rc::clone(&boxed_bot).into_inner().incoming_messages.recv().await {
// while let Some(message) = Rc::<BotInstance>::borrow(Rc::<BotInstance>::as_ref(boxed_bot)) {
// let a = boxed_bot.borrow();
// let boxed_bota = boxed_bot.borrow_mut();
// let a = Rc::try_unwrap(boxed_bot).ok().unwrap().into_inner();
// let boxed_bot = RefCell::new(Rc::new(self));
//let boxed_bot = Rc::new(RefCell::new(self));
// Rc::try_unwrap(boxed_bot).ok().unwrap().into_inner().incoming_messages.recv().await;
// let a:Borrowed = boxed_bot.borrow();
// while let Some(message) = Rc::try_unwrap(boxed_bot).ok().unwrap().into_inner().incoming_messages.recv().await {
// while let Some(message) = RefCell::new(self).borrow_mut().incoming_messages.recv().await {
// while let Some(message) = Rc::try_unwrap(boxed_bot).ok().unwrap().into_inner().incoming_messages.recv().await {
// while let Some(message) = Rc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().rcv_helper().await {
// while let Some(message) = Rc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().incoming_messages.recv().await {
// let mut a = Arc::try_unwrap(boxed_bot.clone())
// .ok().unwrap()
// .into_inner()
// .ok().unwrap();
// let a = Arc::clone(&boxed_bot).into_inner().unwrap().incoming_messages;
// .into_inner()
// .try_into().
// .ok().unwrap();
// while let Some(message) = a.lock().unwrap().incoming_messages.recv().await {
// while let Some(message) = a.recv().await {
// let a = Arc::try_unwrap(boxed_bot.clone()).ok().unwrap();
// let mut a = Arc::try_unwrap(boxed_bot.clone())
// .ok()
// .unwrap();
// .into_inner()
// .get_mut()
// .ok();
// .unwrap();
//let mut a = a.lock().unwrap();
// let a = *a;
// while let Some(message) = a.lock().ok().unwrap().incoming_messages.recv().await {
// while let Some(message) = a.get_mut().expect("Error").incoming_messages.recv().await {
//let tempbot = boxed_bot.clone();
// while let Some(message) = Arc::try_unwrap(tempbot.clone()).ok().unwrap().into_inner().ok().unwrap().incoming_messages.recv().await {
// while let Some(message) = Arc::try_unwrap(tempbot.clone()).ok().unwrap().incoming_messages.recv().await {
// while let Some(message) = boxed_bot.to_owned().incoming_messages.recv().await {
// while let Some(message) = self.incoming_messages.recv().await {
// let a:Arc<RefCell<BotInstance>> = Arc::clone(&boxed_bot);
// 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 {
2024-02-12 01:25:12 -05:00
// 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 {
2024-02-04 14:28:37 -05:00
// 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
2024-01-29 22:57:07 -05:00
// println!("Received message: {:?}", message);
2024-02-04 14:28:37 -05:00
// let boxed_bot = Arc::new(self);
match message {
ServerMessage::Notice(msg) => {
// if let Some(chnl) = msg.channel_login {
// println!("NOTICE : (#{}) {}", chnl, msg.message_text);
// }
2024-01-31 09:31:14 -05:00
match &msg.channel_login {
Some(chnl) => println!("NOTICE : (#{}) {}", chnl, msg.message_text),
None => println!("NOTICE : {}", msg.message_text),
}
}
ServerMessage::Privmsg(msg) => {
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
println!("Privmsg section");
// b.listener_main_prvmsg(&msg);
2024-02-04 14:28:37 -05:00
// self.listener_main_prvmsg(&msg).await;
// bot.into_inner().listener_main_prvmsg(&msg).await;
//let bot = Rc::<RefCell<&BotInstance>>::clone(&bot);
// bot.borrow().listener_main_prvmsg(&msg).await;
// let mut a = Rc::Clone(&bot);
// a.borrow_mut().listener_main_prvmsg(&msg).await;
// bot.borrow_mut().into_inner().listener_main_prvmsg(&msg).await;
// bot.listener_main_prvmsg(&msg).await;
// bot.lock().unwrap().listener_main_prvmsg(&msg).await;
// bot.borrow_mut().listener_main_prvmsg(&msg).await;
// Rc::clone(&boxed_bot).into_inner().listener_main_prvmsg(&msg).await;
// boxed_bot.borrow().listener_main_prvmsg(&msg).await;
// let bottemp = boxed_bot.borrow_mut();
// let a = **bottemp;
// Rc::try_unwrap(boxed_bot).ok().unwrap().into_inner().listener_main_prvmsg(&msg).await;
// Rc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().listener_main_prvmsg(&msg).await;
// Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().ok().unwrap().listener_main_prvmsg(&msg).await;
// let a = Arc::try_unwrap(boxed_bot.clone()).ok().unwrap();
// let mut a = a.lock().unwrap();
// let a = Arc::try_unwrap(boxed_bot.clone()).ok().unwrap();
// a.listener_main_prvmsg(&msg).await;
// (*a).listener_main_prvmsg(&msg).await;
// let a:Arc<RefCell<BotInstance>> = Arc::clone(&boxed_bot);
// a.into_inner().listener_main_prvmsg(&msg).await;
2024-02-12 01:25:12 -05:00
// 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;
2024-02-04 14:28:37 -05:00
// - BotCommand listener should likely need to be called within the above
},
ServerMessage::Whisper(msg) => {
println!("(w) {}: {}", msg.sender.name, msg.message_text);
},
ServerMessage::Join(msg) => {
println!("JOINED: {}", msg.channel_login);
},
ServerMessage::Part(msg) => {
println!("PARTED: {}", msg.channel_login);
},
_ => {}
}
}
});
join_handle.await.unwrap();
2024-02-04 14:28:37 -05:00
}
2024-02-04 14:28:37 -05:00
2024-02-12 01:25:12 -05:00
pub fn get_botmodules(self) -> Arc<ModulesManager> {
2024-02-04 14:28:37 -05:00
// let a = self.botmodules;
// Arc::clone(&Arc::new(Mutex::new(self.botmodules)))
2024-02-12 01:25:12 -05:00
// *self.botmodules
2024-02-04 14:28:37 -05:00
self.botmodules
}
2024-02-12 01:25:12 -05:00
// 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())
// }
2024-02-04 14:28:37 -05:00
2024-02-12 01:25:12 -05:00
// 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 {
2024-02-04 14:28:37 -05:00
// Arc::new(self.botmgrs)
// Arc::clone(&Arc::new(Mutex::new(self.botmgrs)))
let a = self.botmgrs;
// let a = *a.lock().await;
// let a = a.rIdentity();
a
}
2024-02-12 01:25:12 -05:00
// 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)
2024-02-04 14:28:37 -05:00
}
2024-02-12 01:25:12 -05:00
// 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
2024-02-04 14:28:37 -05:00
}
2024-02-12 01:25:12 -05:00
// 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())
// }
2023-12-20 17:55:46 -05:00
// -----------------
// PRIVATE FUNCTIONS
2024-01-29 22:57:07 -05:00
// async fn listener_main_prvmsg(&mut self,msg:PrivmsgMessage) -> () {
2024-02-12 01:25:12 -05:00
// 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) -> () {
2024-02-12 02:34:32 -05:00
println!(">> Inner listenermain_prvmsg()");
2023-12-20 17:55:46 -05:00
2024-02-12 01:25:12 -05:00
// let a = a;
2024-01-29 04:09:53 -05:00
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
2023-12-20 17:55:46 -05:00
2023-12-23 12:29:20 -05:00
// // [ ] 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)
2024-02-04 14:28:37 -05:00
// let botmgr = Rc::new(&self.botmgrs);
// let mut boxedbot = Rc::new(RefCell::new(self));
// let boxed_bot = Rc::new(RefCell::new(self));
// let boxed_bot = Arc::new(Mutex::new(self));
// let boxed_bot = Arc::new(self);
//let boxed_bot = Arc::clone(self);
// let mut boxed_bot = Arc::new(RefCell::new(self));
// 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));
2024-02-12 01:25:12 -05:00
//let boxed_bot = Arc::new(Mutex::new(self));
// let bot = Arc::new(RwLock::new(self));
2024-02-04 14:28:37 -05:00
// for (_m,acts) in &self.botmodules.botactions {
// for (_m,acts) in &self.botmodules.botactions {
// for (_m,acts) in bot.into_inner().botmodules.botactions {
// let mut bot = Rc::clone(&bot);
// for (_m,acts) in bot.into_inner().botmodules.botactions {
// for (_m,acts) in bot.into_inner().botmodules.botactions {
// for (_m,acts) in Rc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().botmodules.botactions {
// for (_m,acts) in Arc::try_unwrap(boxed_bot.clone()).ok() .unwrap().into_inner().ok().unwrap().botmodules.botactions {
// for (_m,acts) in Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().botmodules.botactions {
// for (_m,acts) in Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().botmodules.botactions {
// let a = Arc::try_unwrap(boxed_bot).ok().unwrap().read().unwrap().botmodules.botactions
// let a = Arc::try_unwrap(boxed_bot.clone()).ok().unwrap();
// let b = a.read().unwrap();
// for (_m,acts) in a.read().unwrap().botmodules.botactions {
// for (_m,acts) in b.rbotmodules().botactions {
// 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();
2024-02-12 01:25:12 -05:00
// let a = boxed_bot.lock().await;
2024-02-04 14:28:37 -05:00
//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() {
2024-02-12 01:25:12 -05:00
// 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;
2024-02-12 02:34:32 -05:00
let hacts = Arc::clone(&botlock.botmodules.botactions);
// let hacts = hacts.read().await;
let a = hacts.read().await;
println!("hacts size : {}",(*a).len());
println!(">> Inner listenermain_prvmsg() >> before for loop of bot actions");
// println!(">> Inner listenermain_prvmsg() >> before for loop of bot actions : {:?}",*hacts);
2024-02-12 01:25:12 -05:00
// let hacts = hacts
2024-02-12 02:34:32 -05:00
// let l = *hacts;
for (_m,acts) in &*hacts.read().await {
2024-02-12 01:25:12 -05:00
2024-02-12 02:34:32 -05:00
println!(">> Inner listenermain_prvmsg() >> checking bot actions");
2024-02-12 01:25:12 -05:00
// let bot = bot;
2024-02-04 14:28:37 -05:00
for a in acts {
2024-01-29 04:09:53 -05:00
2024-02-12 02:34:32 -05:00
println!(">> Inner listenermain_prvmsg() >> checking bot actions >> 2");
2024-02-01 09:43:39 -05:00
2024-02-04 14:28:37 -05:00
let _act = match a {
2024-01-29 04:09:53 -05:00
crate::core::botmodules::BotAction::C(c) => {
/*
BotCommand handling -
2024-01-29 04:11:45 -05:00
- [x] Checks if the input message is a prefix with command name or alias
2024-01-29 22:57:07 -05:00
- [ ] Validate User can run based on identityModule(From_Bot)::can_user_run(
_usr:String,
_channelname:ChType,
_chat_badge:ChatBadge,
_cmdreqroles:Vec<UserRole>)
2024-01-29 04:09:53 -05:00
*/
2024-01-31 21:30:08 -05:00
// for v in msg.message_text.split(" ") {
// println!("args : {v}");
// }
2024-02-12 02:34:32 -05:00
println!("Reviewing internal commands");
2024-01-29 04:09:53 -05:00
let inpt = msg.message_text.split("\n").next().expect("ERROR during BotCommand");
2024-01-31 21:30:08 -05:00
let inpt = msg.message_text.split(" ").next().expect("ERROR during BotCommand");
2024-01-29 04:09:53 -05:00
// [x] Check if a bot command based on ...
// [x] prefix + command
2024-01-29 06:18:27 -05:00
2024-01-29 22:57:07 -05:00
let mut confirmed_bot_command = false;
2024-02-04 14:28:37 -05:00
// if inpt == self.prefix.to_string() + c.command.as_str() {
// if inpt == Rc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().prefix.to_string() + c.command.as_str() {
// let a = Rc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().prefix.to_string();
// let a = Arc::try_unwrap(boxed_bot.clone()).ok() .unwrap().into_inner().ok().unwrap().prefix.to_string();
// let a = Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().prefix.to_string();
// let a = Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().prefix.to_string();
// let a = Arc::try_unwrap(boxed_bot.clone()).ok().unwrap();
// let a = (*Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().read().unwrap());
// let a = (*Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().read().unwrap()).prefix.to_string();
// let a = self.prefix.to_string();
// let a = boxed_bot.clone();
// 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()
2024-02-12 01:25:12 -05:00
// let a = boxed_bot.lock().await;
2024-02-04 14:28:37 -05:00
//
// if inpt == a.into_inner().prefix.to_string() + c.command.as_str() {
2024-02-12 01:25:12 -05:00
// 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() {
2024-01-29 22:57:07 -05:00
confirmed_bot_command = true;
2024-01-29 04:09:53 -05:00
}
// [x] prefix + alias
for alias in &c.alias {
2024-02-04 14:28:37 -05:00
// if inpt == self.prefix.to_string() + alias.as_str() {
// if inpt == Rc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().prefix.to_string() + alias.as_str() {
//
// if inpt == Arc::try_unwrap(boxed_bot.clone()).ok() .unwrap().into_inner().ok().unwrap().prefix.to_string() + alias.as_str() {
// Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner()
// if inpt == Arc::try_unwrap(boxed_bot.clone()).ok().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() {
// let a = boxed_bot.clone();
2024-02-12 01:25:12 -05:00
// let a = boxed_bot.lock().await;
2024-02-04 14:28:37 -05:00
// if inpt == a.into_inner().ok().unwrap().prefix.to_string() + alias.as_str() {
// if inpt == a.into_inner().prefix.to_string() + alias.as_str() {
2024-02-12 01:25:12 -05:00
// 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() {
2024-01-29 22:57:07 -05:00
confirmed_bot_command = true;
2024-01-29 04:09:53 -05:00
}
}
2024-01-29 06:18:27 -05:00
2024-01-29 22:57:07 -05:00
if confirmed_bot_command {
2024-02-12 05:25:38 -05:00
println!("Confirmed bot command");
2024-01-29 22:57:07 -05:00
// self.identity.clone().can_user_run_PRVMSG(&msg, c.required_roles.clone());
// [ ] Around here, validate if permissable before executing
// match self.identity.clone().can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// Ok(Permissible::Allow) => c.execute(self.chat.clone(), msg.clone()).await,
// Ok(Permissible::Block) => println!("User Not allowed to run command"),
// _ => (),
// }
2024-01-31 09:31:14 -05:00
// match self.botmgrs.identity.to_owned().can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// match self.botmgrs.identity.can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
2024-02-01 19:23:27 -05:00
// match self.botmgrs.identity.clone().can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
2024-02-04 14:28:37 -05:00
// let botref = Rc::clone(&botmgr);
// if let Rc(botmgr) = botref {
// ()
// }
// match self.botmgrs.identity.clone().can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// match boxed_bot.clone().into_inner().botmgrs.identity.clone().can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// match Rc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner().botmgrs.identity.can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
//let boxed_bot1 = Arc::clone(&boxed_bot);
//let a = boxed_bot1.into_inner().ok().unwrap();
// match boxed_bot1.into_inner().ok().unwrap().botmgrs.identity.can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// match Arc::try_unwrap(boxed_bot.clone())
// .ok()
// .unwrap().into_inner().ok().unwrap().botmgrs.identity.can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// let a = Arc::try_unwrap(boxed_bot.clone()).ok() .unwrap().into_inner();
// let a = Arc::try_unwrap(boxed_bot.clone()).ok().unwrap();
// Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner()
// match a.botmgrs.identity.can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// match Arc::try_unwrap(boxed_bot).ok().unwrap().read().unwrap().botmgrs.identity.can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// match self.botmgrs.identity.can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// match (boxed_bot.clone().into_inner().ok().unwrap()).botmgrs.identity.can_user_run_PRVMSG(&msg, c.required_roles.clone()) {
// let a = boxed_bot.clone();
// let a = boxed_bot.lock().await;
// // let a = a.read().ok().unwrap().botmgrs.identity;
// let a = a.get_identity();
// // let a = a.lock().await.can_user_run_PRVMSG(&msg, c.required_roles.clone()) ;
// match a.lock().await.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await {
2024-02-12 05:25:38 -05:00
2024-02-12 01:25:12 -05:00
// let le = boxed_bot.lock().await;
// // let le = le.lock().await;
// let le = le.get_identity().await;
// let le = *le;
2024-02-04 14:28:37 -05:00
// let le = le.lock().await;
2024-02-12 01:25:12 -05:00
// 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());
2024-02-12 05:25:38 -05:00
println!("Going for botlock");
2024-02-12 01:25:12 -05:00
let botlock = bot.read().await;
2024-02-12 05:25:38 -05:00
println!("Going for identity");
2024-02-12 01:25:12 -05:00
let id = botlock.get_identity();
2024-02-12 05:25:38 -05:00
// let mut id = id.write().await;
// println!("Unlocking identity");
// let eval= id.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await;
let eval = {
let mut id = id.write().await;
println!("Unlocking identity");
id.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await
};
println!("Checking if permissible");
2024-02-12 01:25:12 -05:00
match eval {
2024-01-29 22:57:07 -05:00
// Ok(Permissible::Allow) => (),
Permissible::Allow => {
println!("Executed as permissible");
2024-02-04 14:28:37 -05:00
// c.execute(bot, msg.clone()).await;
// if let bot = Rc::<RefCell<BotInstance>>::clone(*bot) {
// c.execute(bot, msg.clone()).await;
// }
// let boxed_bot = Arc::new(RwLock::new(self));
2024-02-12 01:25:12 -05:00
// 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);
2024-02-12 05:25:38 -05:00
c.execute(a, msg.clone()).await;
println!("exit out of execution");
2024-02-04 14:28:37 -05:00
2024-01-29 22:57:07 -05:00
}
2024-02-01 09:43:39 -05:00
Permissible::Block => {
2024-02-04 14:28:37 -05:00
println!("User Not allowed to run command")
2024-02-01 09:43:39 -05:00
},
2024-01-29 22:57:07 -05:00
// _ => (),
2024-02-01 09:43:39 -05:00
};
};
2024-01-29 22:57:07 -05:00
// c.execute(self.chat.clone(), msg.clone()).await;
2024-02-12 05:25:38 -05:00
2024-01-29 06:18:27 -05:00
}
2024-02-01 09:43:39 -05:00
None::<&PrivmsgMessage> // [ ] Command not confirmed?
None::<&PrivmsgMessage> // [ ] Command not confirmed?
2024-01-29 04:09:53 -05:00
},
2024-02-04 14:28:37 -05:00
crate::core::botmodules::BotAction::L(l) => {
// if let bot = Rc::clone(&bot) {
// l.into_inner().execute(bot, msg.clone()).await
// }
//let bot = Rc::clone(&bot).into_inner();
// l.execute(boxed_bot.clone(), msg.clone()).await
// let boxed_bot = Arc::new(RwLock::new(self));
2024-02-12 01:25:12 -05:00
// l.execute(boxed_bot.clone(), msg.clone()).await;
// let a = Arc::clone(&self);
let a = Arc::clone(&bot);
2024-02-12 05:25:38 -05:00
l.execute(a, msg.clone()).await;
2024-02-04 14:28:37 -05:00
},
2024-02-01 09:43:39 -05:00
2024-01-29 04:09:53 -05:00
_ => (),
2024-02-01 09:43:39 -05:00
};
}
};
2023-12-23 12:29:20 -05:00
2024-02-01 09:43:39 -05:00
2023-12-23 12:29:20 -05:00
// // [ ] There should be a BotCommand Listener to check for prefixes ran
2023-12-20 18:49:28 -05:00
println!("End of Separate Listener Main prvmsg");
2023-12-22 21:09:36 -05:00
2024-02-12 01:25:12 -05:00
// self
// bot
2023-12-20 17:55:46 -05:00
}
2023-12-22 21:09:36 -05:00
2023-12-20 17:55:46 -05:00
}
2023-12-22 21:09:36 -05:00
// ======================================
// ======================================
// ======================================
// ======================================
// UNIT TEST MODULES
#[cfg(test)]
mod tests {
fn always() {
assert_eq!(1,1);
}
}