forcebot_rs/src/core/botmodules.rs

565 lines
21 KiB
Rust
Raw Normal View History

2023-12-21 20:11:32 -05:00
use core::{panic};
2023-12-21 00:48:09 -05:00
use std::error::Error;
use std::collections::HashMap;
2024-01-29 11:09:33 -05:00
use crate::core::identity;
2024-02-04 14:28:37 -05:00
use std::cell::RefCell;
2024-02-12 01:25:12 -05:00
use std::sync::Arc;
use tokio::sync::RwLock;
2024-02-04 14:28:37 -05:00
use std::future::Future;
2024-02-12 01:25:12 -05:00
// use futures::lock::Mutex;
// Important to use tokios Mutex here since std Mutex doesn't work with async functions
use tokio::sync::Mutex;
2024-02-04 14:28:37 -05:00
use crate::core::botinstance::{self, BotInstance};
use std::rc::Rc;
2024-02-12 01:25:12 -05:00
// use tokio::sync::RwLock;
2024-02-04 14:28:37 -05:00
use async_trait::async_trait;
2023-12-21 00:48:09 -05:00
/*
2023-12-22 09:52:01 -05:00
ModulesManager is used to manage Modules and BotActions associated with those modules
pub struct ModulesManager {
statusdb: HashMap<ModType,Vec<ModStatusType>>,
botactions: HashMap<ModType,Vec<BotAction>>,
}
- statusdb: HashMap<ModType,Vec<ModStatusType>> - Defines Modules and their ModStatusType (e.g., Enabled at an Instance level, Disabled at a Channel Level)
- botactions: HashMap<ModType,Vec<BotAction>> - Defines Modules and their BotActions (e.g., BotCommand , Listener, Routine)
2023-12-21 00:48:09 -05:00
Example
{
2023-12-22 09:52:01 -05:00
ModulesManager {
statusdb: {BotModule("experiments 004"): [Enabled(Instance)]},
botactions: {BotModule("experiments 004"): [C(BotCommand { module: BotModule("experiments 004"), command: "DUPCMD4", alias: ["DUPALIAS4A", "DUPALIAS4B"], help: "DUPCMD4 tester" })]} }
2023-12-21 00:48:09 -05:00
}
*/
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
2023-12-21 00:48:09 -05:00
pub enum ModType {
BotModule(String),
}
pub use ModType::BotModule;
2024-01-29 22:57:07 -05:00
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
2023-12-21 00:48:09 -05:00
pub enum ChType {
Channel(String),
}
pub use ChType::Channel;
2023-12-26 20:00:32 -05:00
use twitch_irc::message::PrivmsgMessage;
2023-12-21 00:48:09 -05:00
2024-02-12 01:25:12 -05:00
use self::bot_actions::actions_util;
use self::bot_actions::actions_util::BotAR;
2023-12-21 00:48:09 -05:00
#[derive(Debug)]
enum StatusLvl {
Instance,
Ch(ChType),
}
#[derive(Debug)]
pub enum ModStatusType {
Enabled(StatusLvl),
Disabled(StatusLvl),
}
2024-02-04 14:28:37 -05:00
// #[derive(Clone)]
pub enum BotAction
{
2024-01-29 04:09:53 -05:00
C(BotCommand),
L(Listener),
2023-12-21 00:48:09 -05:00
R(Routine),
}
2023-12-26 20:00:32 -05:00
2024-01-29 02:27:11 -05:00
impl BotAction {
2024-02-04 14:28:37 -05:00
// pub async fn execute(&self,m:botinstance::BotManagers,n:PrivmsgMessage){
2024-02-12 05:25:38 -05:00
pub async fn execute(&self,m:BotAR,n:PrivmsgMessage) -> ()
2024-02-12 01:25:12 -05:00
{
2024-01-29 02:27:11 -05:00
match self {
2024-02-12 05:25:38 -05:00
BotAction::L(a) => a.execute(m,n).await,
BotAction::C(a) => a.execute(m,n).await,
2024-01-29 02:27:11 -05:00
_ => (),
}
}
}
2024-02-04 14:28:37 -05:00
#[async_trait]
pub trait BotActionTrait
{
2024-02-12 01:25:12 -05:00
async fn add_to_bot(self, bot:BotInstance);
// async fn add_to_modmgr(self,modmgr:&'static ModulesManager);
async fn add_to_modmgr(self,modmgr:Arc<ModulesManager>);
2023-12-26 20:00:32 -05:00
}
2024-02-04 14:28:37 -05:00
// #[derive(Clone)]
2024-01-29 04:09:53 -05:00
pub struct BotCommand {
pub module : ModType,
pub command : String, // command call name
pub alias : Vec<String>, // String of alternative names
// bot_prefix : char, // although should be global?
pub exec_body : bot_actions::actions_util::ExecBody,
pub help : String,
2024-01-29 11:09:33 -05:00
pub required_roles : Vec<identity::UserRole>,
2024-01-29 04:09:53 -05:00
}
impl BotCommand
{
2024-02-04 14:28:37 -05:00
// pub async fn execute(&self,m:botinstance::BotManagers,n:PrivmsgMessage){
// pub async fn execute(&self,m:Rc<&botinstance::BotManagers>,n:PrivmsgMessage){
2024-02-12 01:25:12 -05:00
// pub async fn execute(&self,m:BotInstance,n:PrivmsgMessage){
// (self.exec_body)(m,n).await;
// }
// pub fn execute(&self,m:&mut BotInstance,n:PrivmsgMessage) -> () {
// pub fn execute(&self,m:actions_util::BotAR,n:PrivmsgMessage) -> () {
2024-02-12 05:25:38 -05:00
pub async fn execute(&self,m:BotAR,n:PrivmsgMessage) -> () {
2024-02-12 01:25:12 -05:00
// ((*self).exec_body)(m,n);
// ((*self).exec_body)(*m,n);
// m
// ((*self).exec_body)(
2024-02-12 05:25:38 -05:00
((*self).exec_body)(m,n).await;
2024-02-12 01:25:12 -05:00
// m
2024-01-29 04:09:53 -05:00
}
}
2024-02-04 14:28:37 -05:00
#[async_trait]
2024-01-29 04:09:53 -05:00
impl BotActionTrait for BotCommand
{
2024-02-12 01:25:12 -05:00
async fn add_to_bot(self, bot:BotInstance) {
2024-02-04 14:28:37 -05:00
// let mgr = &mut bot.botmodules;
// let mut mgr = *mgr.lock().await;
// let mut mgr = &mut mgr;
2024-02-12 01:25:12 -05:00
// (*self).add_to_modmgr(bot.botmodules);
2024-02-12 05:25:38 -05:00
self.add_to_modmgr(bot.botmodules).await;
2024-01-29 04:09:53 -05:00
}
2024-02-12 01:25:12 -05:00
// 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))
// let modmgr = *modmgr.lock().await;
2024-02-04 14:28:37 -05:00
// modmgr.add_botaction(self.module.clone(), BotAction::C(self))
2024-02-12 01:25:12 -05:00
modmgr.add_botaction(self.module.clone(), BotAction::C(self)).await
2024-01-29 04:09:53 -05:00
}
}
2024-01-27 21:40:08 -05:00
pub mod bot_actions {
pub mod actions_util {
use std::future::Future;
use std::boxed::Box;
use std::pin::Pin;
2024-02-04 14:28:37 -05:00
use std::rc::Rc;
use crate::core::botinstance::{BotInstance, BotManagers, Chat};
2024-01-27 21:40:08 -05:00
use twitch_irc::message::PrivmsgMessage;
2024-02-04 14:28:37 -05:00
use std::cell::RefCell;
2024-02-12 01:25:12 -05:00
use std::sync::{Arc};
// use futures::lock::Mutex;
// Important to use tokios Mutex here since std Mutex doesn't work with async functions
use tokio::sync::{Mutex,RwLock};
pub type BotAM = Arc<Mutex<BotInstance>>;
pub type BotAR = Arc<RwLock<BotInstance>>;
2024-02-04 14:28:37 -05:00
2024-01-27 21:40:08 -05:00
2024-01-31 18:36:23 -05:00
// pub type ExecBody = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
2024-01-31 01:07:27 -05:00
//pub type ExecBody<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output=F> + Send>> + Send + Sync>;
2024-02-04 14:28:37 -05:00
// pub type ExecBody = Box<dyn Fn(BotManagers,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Rc<&BotManagers>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Rc<&BotInstance>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Rc<RefCell<BotInstance>>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Rc<RefCell<BotInstance>>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Rc<RefCell<&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(RefCell<&BotInstance>,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
// pub type ExecBody = Box<dyn Fn(Rc<RefCell<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<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>;
2024-02-12 01:25:12 -05:00
// 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>;
2024-02-04 14:28:37 -05:00
2024-01-31 01:07:27 -05:00
//pub fn asyncbox<T,F>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody<F>
2024-01-31 18:36:23 -05:00
// pub fn asyncbox<T>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody
2024-02-04 14:28:37 -05:00
// pub fn asyncbox<T>(f: fn(BotManagers,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(Rc<&BotManagers>,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(Rc<RefCell<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(Rc<RefCell<&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(Rc<RefCell<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody
// pub fn asyncbox<T>(f: fn(Arc<Mutex<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<RwLock<BotInstance>>,PrivmsgMessage) -> T) -> ExecBody
2024-02-12 01:25:12 -05:00
// 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
2024-01-27 21:40:08 -05:00
where
2024-02-04 14:28:37 -05:00
//T: Future<Output=()> + Send + 'static
2024-02-12 01:25:12 -05:00
// T: Future<Output=F> + Send + 'static
2024-02-04 14:28:37 -05:00
T: Future<Output=()> + Send + 'static
2024-01-27 21:40:08 -05:00
{
Box::new(move |a,b| Box::pin(f(a,b)))
}
}
}
pub struct Listener
{
2023-12-26 20:00:32 -05:00
pub module : ModType,
pub name : String,
pub exec_body : bot_actions::actions_util::ExecBody,
2023-12-26 20:00:32 -05:00
pub help : String
2023-12-21 00:48:09 -05:00
}
impl Listener
{
2024-02-12 01:25:12 -05:00
// pub async fn execute(&self,m:BotInstance,n:PrivmsgMessage){
// (self.exec_body)(m,n).await;
// }
// pub fn execute(&self,m:BotInstance,n:PrivmsgMessage){
// (self.exec_body)(m,n);
// }
// pub fn execute(&self,m:&BotInstance,n:PrivmsgMessage) -> &BotInstance {
// pub fn execute(&self,m:actions_util::BotAR,n:PrivmsgMessage) -> () {
2024-02-12 05:25:38 -05:00
pub async fn execute(&self,m:BotAR,n:PrivmsgMessage) -> () {
2024-02-12 01:25:12 -05:00
// let mut m = Arc::*m;
2024-02-12 05:25:38 -05:00
((*self).exec_body)(m,n).await;
2024-02-12 01:25:12 -05:00
// *self
// &m
2023-12-26 20:00:32 -05:00
}
}
2024-02-04 14:28:37 -05:00
#[async_trait]
impl BotActionTrait for Listener
{
2024-02-12 01:25:12 -05:00
async fn add_to_bot(self, bot:BotInstance) {
2024-02-04 14:28:37 -05:00
// let mgr = &mut bot.botmodules;
2024-02-12 01:25:12 -05:00
// let mgr = bot.botmodules;
// self.add_to_modmgr(Arc::new(*mgr));
2024-02-12 02:34:32 -05:00
println!("Adding action to bot");
self.add_to_modmgr(bot.botmodules).await;
2023-12-26 20:00:32 -05:00
}
2024-02-04 14:28:37 -05:00
// fn add_to_modmgr(self, modmgr:&mut ModulesManager) {
2024-02-12 01:25:12 -05:00
// 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:&'static ModulesManager) {
async fn add_to_modmgr(self, modmgr:Arc<ModulesManager>) {
// let modmgr = *modmgr.lock().await;
2024-02-12 02:34:32 -05:00
println!("Adding action to module manager");
2024-02-12 01:25:12 -05:00
modmgr.add_botaction(self.module.clone(), BotAction::L(self)).await;
// modmgr.add_botaction(self.module.clone(), BotAction:L(self))
2023-12-26 20:00:32 -05:00
}
}
2023-12-21 00:48:09 -05:00
#[derive(Debug)]
struct Routine {}
2024-02-04 14:28:37 -05:00
// #[derive(Clone)]
pub struct ModulesManager
{
2024-02-12 01:25:12 -05:00
// statusdb: HashMap<ModType,Vec<ModStatusType>>,
statusdb: Arc<RwLock<HashMap<ModType,Vec<ModStatusType>>>>,
// pub botactions: HashMap<ModType,Vec<BotAction>>,
pub botactions: Arc<RwLock<HashMap<ModType,Vec<BotAction>>>>,
2023-12-21 00:48:09 -05:00
}
impl ModulesManager
{
2023-12-21 00:48:09 -05:00
2024-02-12 01:25:12 -05:00
// pub fn init() -> Arc<Mutex<ModulesManager>>
2024-02-12 02:34:32 -05:00
pub async fn init() -> Arc<ModulesManager>
2024-01-27 13:35:55 -05:00
{
2023-12-21 00:48:09 -05:00
2024-01-29 03:20:56 -05:00
let m = HashMap::new();
let act = HashMap::new();
2023-12-21 00:48:09 -05:00
let mut mgr = ModulesManager {
2024-02-12 01:25:12 -05:00
statusdb : Arc::new(RwLock::new(m)),
botactions : Arc::new(RwLock::new(act)),
2023-12-21 00:48:09 -05:00
};
2024-02-04 14:28:37 -05:00
// :: [x] initialize core modules
// crate::core::identity::init(&mut mgr);
2024-02-12 01:25:12 -05:00
// let a = Arc::new(Mutex::new(mgr));
// // let a = a.clone();
// crate::core::identity::init(a.clone());
// crate::core::identity::init(&mgr);
2024-02-12 02:34:32 -05:00
println!("ModulesManager > init() > Adding modules");
2024-02-12 01:25:12 -05:00
let mgra = Arc::new(mgr);
2024-02-12 02:34:32 -05:00
crate::core::identity::init(Arc::clone(&mgra)).await;
2023-12-22 09:21:49 -05:00
2024-01-29 03:20:56 -05:00
// initialize custom crate modules
2024-02-04 14:28:37 -05:00
// crate::modules::init(&mut mgr);
// let a = a.clone();
2024-02-12 01:25:12 -05:00
// crate::modules::init(a.clone());
2024-02-12 06:18:57 -05:00
crate::modules::init(Arc::clone(&mgra)).await;
2023-12-22 09:21:49 -05:00
2024-02-12 02:34:32 -05:00
2023-12-22 09:21:49 -05:00
println!(">> Modules Manager : End of Init");
2023-12-21 00:48:09 -05:00
2024-02-04 14:28:37 -05:00
// mgr
2024-02-12 01:25:12 -05:00
mgra
2023-12-21 00:48:09 -05:00
}
2024-02-12 01:25:12 -05:00
// pub async fn rbotactions(&self) -> HashMap<ModType,Vec<BotAction>> {
// // (*self).botactions
// let a = self.botactions.read().await;
// *a
// }
2024-02-04 14:28:37 -05:00
2023-12-21 00:48:09 -05:00
pub fn modstatus(&self, _:ModType, _:ChType) -> ModStatusType {
// Example usage : botmanager.modstatus(
// BotModule("GambaCore"),
// Channel("modulatingforce")
// )
// - The ModStatusType checks in the context of the given channel ,
// but also validates based on wheher the module is disabled at a bot instance
// level as well
ModStatusType::Enabled(StatusLvl::Instance)
}
pub fn togglestatus(&self, _:ModType, _:ChType) -> ModStatusType {
// enables or disables based on current status
ModStatusType::Enabled(StatusLvl::Instance)
}
pub fn setstatus(&self, _:ModType, _:ModStatusType) -> Result<&str,Box<dyn Error>> {
// sets the status based given ModSatusType
// e.g., b.setstatus(BodModule("GambaCore"), Enabled(Channel("modulatingforce"))).expect("ERROR")
Ok("")
}
//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> {
2024-01-27 13:35:55 -05:00
//pub fn add_botaction(&mut self, in_module:ModType, in_action:BotAction ) -> () {
2024-02-12 01:25:12 -05:00
pub async fn add_botaction(&self, in_module:ModType, in_action:BotAction ) {
2024-02-12 02:34:32 -05:00
println!("Add botaction called");
2023-12-21 20:11:32 -05:00
/*
adds a BotAction to the Modules Manager - This will require a BotModule passed as well
This will including the logic of a valid add
If it fails to add, either a PANIC or some default coded business rules that handles the botaction add
For example, this Should PANIC (ideally Panic?) if it does not successfully add a bot module
-- Being unable to indicates a Programming/Developer code logic issue : They cannot add botactions that already exists (?)
-- In particular to BotCommands, which must have Unique command call names and aliases that to not conflict with any other
already BotCommand added name or alias
Other types might be fine? For example, if 2 modules have their own listeners but each have the name "targetchatter" ,
both would be called separately, even if they both have the same or different logic
*/
// let newlistener = Listener {
// // module : BotModule(String::from("experiments").to_owned()),
// module : in_module.clone(),
// name : String::from("socklistener"),
// help : String::from("This will listen and react to sock randomly"),
// };
// As a Demonstration, the listener's Module is added and Enabled at Instance level
2023-12-21 20:11:32 -05:00
// [x] Before Adding, validate the following :
// - 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
2024-02-12 01:25:12 -05:00
async fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option<ModType>
{
2023-12-21 20:11:32 -05:00
// Some(BotModule(String::from("GambaCore")))
// match act {
// BotAction::C(c) => {
// Some(BotModule(String::from("GambaCore")))
// },
// BotAction::L(l) => None,
// BotAction::R(r) => None,
// }
if let BotAction::C(incmd) = act {
2023-12-21 20:11:32 -05:00
// let n = & mgr.botactions;
2023-12-21 20:11:32 -05:00
2024-02-12 01:25:12 -05:00
let d = mgr.botactions.read().await;
let d = &(*d);
2023-12-21 20:11:32 -05:00
for (module,moduleactions) in d {
2023-12-21 20:11:32 -05:00
for modact in moduleactions.iter() {
if let BotAction::C(dbcmd) = &modact {
// At this point, there is an command incmd and looked up dbcmd
2023-12-21 20:11:32 -05:00
// [x] check if given botcommand c.command:String conflicts with any in botactions
2023-12-21 20:11:32 -05:00
if incmd.command.to_lowercase() == dbcmd.command.to_lowercase() {
// Returning State - with the identified module
// return Some((module.clone(),BotAction::C(*dbcmd.clone())));
// return Some(incmd); // for some reason I keep getting issues
//return Some(BotModule(String::from("GambaCore"))); // works
return Some(module.clone()); // works
// return Some(dbcmd.clone());
}
2023-12-21 20:11:32 -05:00
for a in &dbcmd.alias {
if incmd.command.to_lowercase() == a.to_lowercase() {
// Returning State - with the identified module
// return Some((module.clone(),BotAction::C(dbcmd)));
return Some(module.clone()); // works
2023-12-21 20:11:32 -05:00
}
}
2023-12-21 20:11:32 -05:00
// [x] Then do the same check except for each c.alias
2023-12-21 20:11:32 -05:00
for inalias in &incmd.alias {
2023-12-21 20:11:32 -05:00
if inalias.to_lowercase() == dbcmd.command.to_lowercase() {
// Returning State - with the identified module
// return Some((module.clone(),BotAction::C(dbcmd)));
return Some(module.clone()); // works
2023-12-21 20:11:32 -05:00
}
2023-12-21 20:11:32 -05:00
for a in &dbcmd.alias {
if inalias.to_lowercase() == a.to_lowercase() {
// Returning State - with the identified module
// return Some((module.clone(),BotAction::C(dbcmd)));
return Some(module.clone()); // works
2023-12-21 20:11:32 -05:00
}
}
2023-12-21 20:11:32 -05:00
}
2023-12-21 20:11:32 -05:00
}
}
2023-12-21 20:11:32 -05:00
}
2023-12-21 20:11:32 -05:00
// return Some(BotModule(String::from("GambaCore")))
}
2023-12-21 20:11:32 -05:00
// for all other scenarios (e.g., Listener, Routine), find no conflicts
None
}
// if let probmod = find_conflict_module(&self, &in_action) {
// // () // return because there was a conflict?
// panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , probmod );
// }
2024-02-12 01:25:12 -05:00
match find_conflict_module(&self, &in_action).await {
// Some(c) => panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , c ),
Some(c) => panic!("ERROR: Could not add module; there was a conflict with existing module {:?}", c ),
2023-12-21 20:11:32 -05:00
None => (),
}
2024-02-12 01:25:12 -05:00
// {
// let mut lala = self;
// let statusvector = (*lala).statusdb
// // .entry(BotModule(String::from("experiments")))
// .entry(in_module.clone())
// .or_insert(Vec::new());
// }
let mut dbt = self.statusdb.write().await;
let statusvector = dbt
// .entry(BotModule(String::from("experiments")))
.entry(in_module.clone())
.or_insert(Vec::new());
2023-12-21 20:11:32 -05:00
statusvector.push(ModStatusType::Enabled(StatusLvl::Instance)); // Pushes the Module as Enabled at Instance Level
2024-02-12 01:25:12 -05:00
let mut a = self.botactions.write().await;
let modactions = a
//.entry( BotModule(String::from("experiments")))
.entry( in_module.clone())
.or_insert(Vec::new());
2023-12-21 20:11:32 -05:00
// modactions.push(BotAction::L(newlistener));
modactions.push(in_action);
2023-12-22 09:21:49 -05:00
println!(">> Modules Manager : Called Add bot Action");
2024-02-12 06:18:57 -05:00
println!("add_botaction - botactions size : {}",modactions.len());
//println!(">> Modules Manager : {:?}",&self);
2023-12-22 09:21:49 -05:00
2024-01-27 13:35:55 -05:00
//();
//let mgr = self;
//mgr
//self
}
2023-12-21 00:48:09 -05:00
fn statuscleanup(&self,_:Option<ChType>) -> () {
2023-12-21 12:10:50 -05:00
// internal cleans up statusdb . For example :
// - remove redudancies . If we see several Enabled("m"), only keep 1x
// - Clarify Conflict. If we see Enabled("m") and Disabled("m") , we remove Enabled("m") and keep Disabled("m")
// the IDEAL is that this is ran before every read/update operation to ensure quality
// Option<ChType> can pass Some(Channel("m")) (as an example) so statuscleanup only works on the given channel
// Passing None to chnl may be a heavy operation, as this will review and look at the whole table
()
}
2023-12-21 00:48:09 -05:00
}