fmt & clippy

This commit is contained in:
ModulatingForce 2024-03-01 23:36:37 -05:00
commit d998e2f290
8 changed files with 228 additions and 161 deletions

View file

@ -11,9 +11,9 @@ use twitch_irc::SecureTCPTransport;
use twitch_irc::TwitchIRCClient;
// use std::borrow::Borrow;
use dotenv::dotenv;
use std::borrow::BorrowMut;
use std::boxed;
use std::cell::Ref;
// use std::borrow::BorrowMut;
// use std::boxed;
// use std::cell::Ref;
use std::env;
use std::collections::HashMap;
@ -23,30 +23,29 @@ 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::botmodules::BotAction;
// use crate::core::botmodules::BotAction;
use crate::core::ratelimiter;
use crate::core::ratelimiter::RateLimiter;
use crate::core::botmodules;
// use crate::core::botmodules;
use crate::core::botmodules::ModulesManager;
use crate::core::identity::{ChangeResult, IdentityManager, Permissible};
use std::cell::RefCell;
use std::rc::Rc;
// use std::cell::RefCell;
// use std::rc::Rc;
use std::sync::Arc;
// use futures::lock::Mutex;
use std::pin::Pin;
// use std::pin::Pin;
//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;
use casual_logger::{Level, Log};
// use casual_logger::{Level, Log};
use casual_logger::Log;
pub mod botlog {
@ -57,7 +56,8 @@ pub mod botlog {
- Option<PrivmsgMessage> - this is used to parse out Chatter & Channel into the logs
*/
use casual_logger::{Level, Log};
// use casual_logger::{Level, Log};
use casual_logger::Log;
use twitch_irc::message::PrivmsgMessage;
// trace, debug, info, notice, warn, error, fatal
@ -72,11 +72,7 @@ pub mod botlog {
*/
pub fn trace(
in_msg: &str,
in_module: Option<String>,
in_prvmsg: Option<&PrivmsgMessage>,
) -> () {
pub fn trace(in_msg: &str, in_module: Option<String>, in_prvmsg: Option<&PrivmsgMessage>) {
let (chnl, chatter) = match in_prvmsg {
Some(prvmsg) => {
//Log::trace(&format!("(#{}) {}: {}", prvmsg.channel_login, prvmsg.sender.name, prvmsg.message_text));
@ -97,11 +93,7 @@ pub mod botlog {
);
}
pub fn debug(
in_msg: &str,
in_module: Option<String>,
in_prvmsg: Option<&PrivmsgMessage>,
) -> () {
pub fn debug(in_msg: &str, in_module: Option<String>, in_prvmsg: Option<&PrivmsgMessage>) {
let (chnl, chatter) = match in_prvmsg {
Some(prvmsg) => {
//Log::trace(&format!("(#{}) {}: {}", prvmsg.channel_login, prvmsg.sender.name, prvmsg.message_text));
@ -122,7 +114,7 @@ pub mod botlog {
);
}
pub fn info(in_msg: &str, in_module: Option<String>, in_prvmsg: Option<&PrivmsgMessage>) -> () {
pub fn info(in_msg: &str, in_module: Option<String>, in_prvmsg: Option<&PrivmsgMessage>) {
let (chnl, chatter) = match in_prvmsg {
Some(prvmsg) => {
//Log::trace(&format!("(#{}) {}: {}", prvmsg.channel_login, prvmsg.sender.name, prvmsg.message_text));
@ -143,11 +135,7 @@ pub mod botlog {
);
}
pub fn notice(
in_msg: &str,
in_module: Option<String>,
in_prvmsg: Option<&PrivmsgMessage>,
) -> () {
pub fn notice(in_msg: &str, in_module: Option<String>, in_prvmsg: Option<&PrivmsgMessage>) {
let (chnl, chatter) = match in_prvmsg {
Some(prvmsg) => {
//Log::trace(&format!("(#{}) {}: {}", prvmsg.channel_login, prvmsg.sender.name, prvmsg.message_text));
@ -168,7 +156,7 @@ pub mod botlog {
);
}
pub fn warn(in_msg: &str, in_module: Option<String>, in_prvmsg: Option<&PrivmsgMessage>) -> () {
pub fn warn(in_msg: &str, in_module: Option<String>, in_prvmsg: Option<&PrivmsgMessage>) {
let (chnl, chatter) = match in_prvmsg {
Some(prvmsg) => {
//Log::trace(&format!("(#{}) {}: {}", prvmsg.channel_login, prvmsg.sender.name, prvmsg.message_text));
@ -189,11 +177,7 @@ pub mod botlog {
);
}
pub fn error(
in_msg: &str,
in_module: Option<String>,
in_prvmsg: Option<&PrivmsgMessage>,
) -> () {
pub fn error(in_msg: &str, in_module: Option<String>, in_prvmsg: Option<&PrivmsgMessage>) {
let (chnl, chatter) = match in_prvmsg {
Some(prvmsg) => {
//Log::trace(&format!("(#{}) {}: {}", prvmsg.channel_login, prvmsg.sender.name, prvmsg.message_text));
@ -262,17 +246,18 @@ impl Chat {
) -> Chat {
Chat {
ratelimiters: Arc::new(Mutex::new(ratelimiters)),
client: client,
// client: client,
client,
}
}
pub async fn init_channel(&mut self, chnl: ChType) -> () {
pub async fn init_channel(&mut self, chnl: ChType) {
let n = RateLimiter::new();
self.ratelimiters.lock().await.insert(chnl, n);
}
// 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) -> () {
pub async fn say_in_reply_to(&self, msg: &PrivmsgMessage, mut outmsg: String) {
/*
formats message before sending to TwitchIRC
@ -308,7 +293,7 @@ impl Chat {
msg.channel_login, "rate limit counter increase"
),
Some("Chat > say_in_reply_to".to_string()),
Some(&msg),
Some(msg),
);
contextratelimiter.increment_counter();
// println!("{:?}",self.ratelimiters);
@ -316,29 +301,29 @@ impl Chat {
botlog::trace(
&format!("{:?}", self.ratelimiters),
Some("Chat > say_in_reply_to".to_string()),
Some(&msg),
Some(msg),
);
}
ratelimiter::LimiterResp::Skip => {
(); // do nothing otherwise
// (); // do nothing otherwise
}
}
Log::flush();
}
async fn say(&self, _: String, _: String) -> () {
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) -> () {
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) -> () {
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();
@ -363,7 +348,7 @@ impl BotManagers {
}
}
pub fn rIdentity(self) -> Arc<RwLock<IdentityManager>> {
pub fn r_identity(self) -> Arc<RwLock<IdentityManager>> {
self.identity
}
}
@ -446,7 +431,8 @@ impl BotInstance {
// };
BotInstance {
prefix: prefix,
// prefix: prefix,
prefix,
bot_channel: Channel(login_name),
incoming_messages: Arc::new(RwLock::new(incoming_messages)),
botmodules: ModulesManager::init().await,
@ -458,7 +444,7 @@ impl BotInstance {
// b
}
pub async fn runner(self) -> () {
pub async fn runner(self) {
let bot = Arc::new(RwLock::new(self));
let join_handle = tokio::spawn(async move {
@ -503,7 +489,8 @@ impl BotInstance {
// println!("Privmsg section");
// Log::debug(&format!("Privmsg section"));
botlog::trace(
&format!("Privmsg section"),
// &format!("Privmsg section"),
"Privmsg section",
Some("BotInstance > runner()".to_string()),
Some(&msg),
);
@ -551,8 +538,9 @@ impl BotInstance {
}
pub async fn get_botmgrs(self) -> BotManagers {
let a = self.botmgrs;
a
// let a = self.botmgrs;
// a
self.botmgrs
}
pub fn get_identity(&self) -> Arc<RwLock<IdentityManager>> {
@ -560,19 +548,20 @@ impl BotInstance {
}
pub fn get_prefix(&self) -> char {
(*self).prefix
// (*self).prefix
self.prefix
}
// -----------------
// PRIVATE FUNCTIONS
pub async fn listener_main_prvmsg(bot: BotAR, msg: &PrivmsgMessage) -> () {
pub async fn listener_main_prvmsg(bot: BotAR, msg: &PrivmsgMessage) {
// println!(">> Inner listenermain_prvmsg()");
// Log::trace(">> Inner listenermain_prvmsg()");
botlog::trace(
">> Inner listenermain_prvmsg()",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
Some(msg),
);
// let a = a;
@ -589,7 +578,7 @@ impl BotInstance {
botlog::trace(
&format!("hacts size : {}", (*a).len()),
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
Some(msg),
);
// println!(">> Inner listenermain_prvmsg() >> before for loop of bot actions");
@ -597,16 +586,17 @@ impl BotInstance {
botlog::trace(
">> Inner listenermain_prvmsg() >> before for loop of bot actions",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
Some(msg),
);
for (_m, acts) in &*hacts.read().await {
// for (_m, acts) in &*hacts.read().await {
for acts in (*hacts.read().await).values() {
// println!(">> Inner listenermain_prvmsg() >> checking bot actions");
// Log::trace(">> Inner listenermain_prvmsg() >> checking bot actions");
botlog::trace(
">> Inner listenermain_prvmsg() >> checking bot actions",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
Some(msg),
);
// let bot = bot;
@ -617,10 +607,11 @@ impl BotInstance {
botlog::trace(
">> Inner listenermain_prvmsg() >> checking bot actions >> 2",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
Some(msg),
);
let _act = match a {
// let _act = match a {
match a {
crate::core::botmodules::BotAction::C(c) => {
/*
BotCommand handling -
@ -641,13 +632,13 @@ impl BotInstance {
botlog::trace(
"Reviewing internal commands",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
Some(msg),
);
// let inpt = msg.message_text.split("\n").next().expect("ERROR during BotCommand");
let inpt = msg
.message_text
.split(" ")
.split(' ')
.next()
.expect("ERROR during BotCommand");
@ -675,7 +666,8 @@ impl BotInstance {
botlog::debug(
"Confirmed bot command",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
// println!("Going for botlock");
@ -683,7 +675,8 @@ impl BotInstance {
botlog::trace(
"Going for botlock",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
let botlock = bot.read().await;
@ -692,7 +685,8 @@ impl BotInstance {
botlog::trace(
"Going for identity",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
let id = botlock.get_identity();
@ -704,11 +698,14 @@ impl BotInstance {
botlog::trace(
"Unpacking identity",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
// let (a, b) =
// id.can_user_run_prvmsg(&msg, c.required_roles.clone()).await;
let (a, b) =
id.can_user_run_PRVMSG(&msg, c.required_roles.clone()).await;
id.can_user_run_prvmsg(msg, c.required_roles.clone()).await;
// // [-] #todo : need ot add functionality around here to do an o7 when a mod has been promoted => Preferring to do this outside the mutex
// if let ChangeResult::Success(b) = b {
// // let b = b.to_lowercase();
@ -724,7 +721,8 @@ impl BotInstance {
botlog::trace(
"Checking if permissible",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
let (eval, rolechange) = eval;
@ -736,7 +734,8 @@ impl BotInstance {
botlog::notice(
"Assigning Mod UserRole to Mod",
Some("botinstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
// println!("Read() lock Bot");
@ -744,13 +743,15 @@ impl BotInstance {
botlog::trace(
"Read() lock Bot",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
let botlock = bot.read().await;
let outstr =
"o7 a Mod. I kneel to serve! pepeKneel ".to_string();
(*botlock).botmgrs.chat.say_in_reply_to(msg, outstr).await;
// (*botlock).botmgrs.chat.say_in_reply_to(msg, outstr).await;
botlock.botmgrs.chat.say_in_reply_to(msg, outstr).await;
}
}
@ -761,7 +762,8 @@ impl BotInstance {
botlog::debug(
"Executed as permissible",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
let a = Arc::clone(&bot);
c.execute(a, msg.clone()).await;
@ -770,7 +772,8 @@ impl BotInstance {
botlog::trace(
"exit out of execution",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
}
Permissible::Block => {
@ -779,7 +782,8 @@ impl BotInstance {
botlog::info(
"User Not allowed to run command",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
} // _ => (),
};
@ -803,7 +807,8 @@ impl BotInstance {
botlog::trace(
"End of Separate Listener Main prvmsg",
Some("BotInstance > listener_main_prvmsg()".to_string()),
Some(&msg),
// Some(&msg),
Some(msg),
);
// self