Merge pull request 'ModulesManager' (#37) from modulesmanager into main
All checks were successful
ci/woodpecker/push/cargo-checks Pipeline was successful
ci/woodpecker/pr/cargo-checks Pipeline was successful

Reviewed-on: #37
This commit is contained in:
modulatingforce 2024-03-22 21:34:07 -04:00
commit 45e3f02297
4 changed files with 2005 additions and 96 deletions

View file

@ -19,12 +19,21 @@ use crate::core::ratelimiter::RateLimiter;
use crate::core::bot_actions::actions_util::BotAR;
use crate::core::botmodules::ModulesManager;
use crate::core::identity::{ChangeResult, IdentityManager, Permissible};
use crate::core::identity::{IdentityManager, Permissible,self};
use crate::core::botlog;
use crate::core::chat::Chat;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ChangeResult {
Success(String),
Failed(String),
NoChange(String),
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum ChType {
Channel(String),
@ -32,6 +41,8 @@ pub enum ChType {
pub use ChType::Channel;
use super::botmodules::StatusType;
#[derive(Clone)]
pub struct BotManagers {
pub identity: Arc<RwLock<IdentityManager>>,
@ -235,7 +246,7 @@ impl BotInstance {
/*
BotCommand handling -
- [x] Checks if the input message is a prefix with command name or alias
- [ ] Validate User can run based on identityModule(From_Bot)::can_user_run(
- [x] Validate User can run based on identityModule(From_Bot)::can_user_run(
_usr:String,
_channelname:ChType,
_chat_badge:ChatBadge,
@ -284,6 +295,54 @@ impl BotInstance {
let botlock = bot.read().await;
let id = botlock.get_identity();
// [x] Check first if the Module for that Given Command is Enabled or Disabled on the given Channel
let modmgr = Arc::clone(&botlock.botmodules);
let modstatus = modmgr.modstatus(
c.module.clone(),
ChType::Channel(msg.channel_login.to_string())).await;
if let StatusType::Disabled(a) = modstatus {
// [x] Should only respond if a BotAdmin , Mod , SupMod , BroadCaster
// - Specifically it should respond only to those who may be able to enable the module
botlog::trace(
&format!("Identified cmd is associated with Disabled Module : StatusLvl = {:?}", a),
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(msg),
);
const OF_CMD_CHANNEL:ChType = Channel(String::new());
let elevated_access = {
let mut idlock = id.write().await;
let (permissability, _) = idlock
.can_user_run_prvmsg(msg,
vec![
identity::UserRole::BotAdmin,
identity::UserRole::Mod(OF_CMD_CHANNEL),
identity::UserRole::SupMod(OF_CMD_CHANNEL),
identity::UserRole::Broadcaster,
])
.await;
permissability
};
if let Permissible::Allow = elevated_access {
let botlock = bot.read().await;
let outstr =
format!("sadg Module is disabled : {:?}",a);
botlock.botmgrs.chat.say_in_reply_to(msg, outstr).await;
}
return;
};
let eval = {
let mut idlock = id.write().await;
let (permissability, chngrslt) = idlock
@ -322,7 +381,7 @@ impl BotInstance {
match eval {
Permissible::Allow => {
botlog::debug(
"Executed as permissible",
"Executing as permissible",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(msg),
);
@ -350,8 +409,33 @@ impl BotInstance {
}
crate::core::botmodules::BotAction::L(l) => {
let a = Arc::clone(&bot);
l.execute(a, msg.clone()).await;
let botlock = bot.read().await;
// let id = botlock.get_identity();
// [x] Check first if the Module for that Given Command is Enabled or Disabled on the given Channel
let modmgr = Arc::clone(&botlock.botmodules);
let modstatus = modmgr.modstatus(
l.module.clone(),
ChType::Channel(msg.channel_login.to_string())).await;
if let StatusType::Disabled(a) = modstatus {
// [x] Should only respond if a BotAdmin , Mod , SupMod , BroadCaster
// - Specifically it should respond only to those who may be able to enable the module
botlog::trace(
&format!("Identified listener is associated with Disabled Module : StatusLvl = {:?}", a),
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(msg),
);
} else {
let a = Arc::clone(&bot);
l.execute(a, msg.clone()).await;
}
}
_ => (),

File diff suppressed because it is too large Load diff

View file

@ -8,7 +8,7 @@ use twitch_irc::message::PrivmsgMessage;
use casual_logger::Log;
use crate::core::bot_actions::actions_util::{self, BotAR};
use crate::core::botinstance::ChType;
use crate::core::botinstance::{ChType,ChangeResult};
use crate::core::botlog;
use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, ModulesManager};
@ -67,7 +67,8 @@ pub async fn init(mgr: Arc<ModulesManager>) {
],
};
tempb.add_to_modmgr(Arc::clone(&mgr)).await;
// tempb.add_to_modmgr(Arc::clone(&mgr)).await;
tempb.add_core_to_modmgr(Arc::clone(&mgr)).await;
async fn cmd_promote(bot: BotAR, msg: PrivmsgMessage) {
botlog::trace(
@ -238,7 +239,9 @@ pub async fn init(mgr: Arc<ModulesManager>) {
],
};
tempb.add_to_modmgr(Arc::clone(&mgr)).await;
// tempb.add_to_modmgr(Arc::clone(&mgr)).await;
// add_core_to_modmgr
tempb.add_core_to_modmgr(Arc::clone(&mgr)).await;
async fn cmd_demote(bot: BotAR, msg: PrivmsgMessage) {
botlog::debug(
@ -428,7 +431,9 @@ pub async fn init(mgr: Arc<ModulesManager>) {
],
};
tempcomm.add_to_modmgr(Arc::clone(&mgr)).await;
// tempcomm.add_to_modmgr(Arc::clone(&mgr)).await;
// add_core_to_modmgr
tempcomm.add_core_to_modmgr(Arc::clone(&mgr)).await;
async fn getroles(bot: BotAR, msg: PrivmsgMessage) {
botlog::debug(
@ -597,18 +602,18 @@ pub struct IdentityManager {
>
*/
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum ChatBadge {
Broadcaster,
Mod,
}
#[derive(Debug, PartialEq, Eq)]
pub enum ChangeResult {
Success(String),
Failed(String),
NoChange(String),
}
// #[derive(Debug, PartialEq, Eq)]
// pub enum ChangeResult {
// Success(String),
// Failed(String),
// NoChange(String),
// }
impl IdentityManager {
pub fn init() -> IdentityManager {
@ -625,7 +630,8 @@ impl IdentityManager {
}
}
async fn add_role(&self, trgchatter: String, trg_role: UserRole) {
// => 03.22 - Force - Made public because botmodules unit tests
pub async fn add_role(&self, trgchatter: String, trg_role: UserRole) {
let mut srulock = self.special_roles_users.write().await;
let mut usrrolelock = srulock
.get_mut(&trgchatter)
@ -647,7 +653,8 @@ impl IdentityManager {
}
}
async fn affirm_chatter_in_db(&self, trgchatter: String) {
// => 03.22 - Force - Made public because botmodules unit tests
pub async fn affirm_chatter_in_db(&self, trgchatter: String) {
let mut srulock = self.special_roles_users.write().await;
srulock
.entry(trgchatter.clone())
@ -689,59 +696,6 @@ impl IdentityManager {
}
}
// if &msg.badges.contains(Badge{}) {
// }
// if let Some(sender_badge) = sender_badge {
// match sender_badge {
// Some(sender_badge) => {
// return &self.can_user_run(msg.sender.name.to_owned(),
// ChType::Channel(msg.channel_login.to_owned()),
// sender_badge,
// cmdreqroles
// return self.can_user_run(msg.sender.name.to_owned(),
// let a = Arc::new(Mutex::new(self));
// let mut a = a.lock().await;
// let a = **a;
// let a = a.can_user_run(msg.sender.name.to_owned(),
// ChType::Channel(msg.channel_login.to_owned()),
// sender_badge,
// cmdreqroles
// ) ;
// let a = *self;
// let a = Arc::new(Mutex::new(a));
// let a = a.lock().await.can_user_run(msg.sender.name.to_owned(),
// ChType::Channel(msg.channel_login.to_owned()),
// sender_badge,
// cmdreqroles
// ) ;
// return a;
// return self.can_user_run(msg.sender.name.to_owned(),
// ChType::Channel(msg.channel_login.to_owned()),
// sender_badge,
// cmdreqroles
// ).await
// * NOTE : We're preferring to pass the ChangeResult up , where we have access to Chat via BotInstance
// that have more strained chatting rules
// let evalpermissible = self.can_user_run(msg.sender.name.to_owned(),
// ChType::Channel(msg.channel_login.to_owned()),
// sender_badge,
// cmdreqroles
// ).await ;
// evalpermissible
// // }
// None => {
// }
// here , sender_badge is likely None
// This could be a regular chatter, BotAdmin,SupserMod
// [ ] Call can_user_run()
// (self,Permissible::Block)
// (Permissible::Block,ChangeResult::NoChange("".to_string()))
self.can_user_run(
msg.sender.name.to_owned(),
ChType::Channel(msg.channel_login.to_owned()),
@ -749,6 +703,7 @@ impl IdentityManager {
cmdreqroles,
)
.await
}
pub async fn can_user_run(

View file

@ -125,7 +125,7 @@ async fn good_girl(bot: BotAR, msg: PrivmsgMessage) {
Some(&msg),
);
let rollwin = rand::thread_rng().gen_ratio(1, 8);
let rollwin = rand::thread_rng().gen_ratio(1, 1);
if rollwin {
botlog::debug(