Listeners with exec_body can be added to ModulesManager
This commit is contained in:
parent
25b73c41cc
commit
88293377e8
3 changed files with 28 additions and 12 deletions
|
@ -27,11 +27,13 @@ use crate::core::ratelimiter;
|
||||||
// println!("I was here");
|
// println!("I was here");
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// use crate::core::botmodules::BotAction
|
||||||
|
|
||||||
|
|
||||||
use crate::core::botmodules;
|
use crate::core::botmodules;
|
||||||
use crate::core::botmodules::ModulesManager;
|
use crate::core::botmodules::ModulesManager;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||||
pub enum ChType {
|
pub enum ChType {
|
||||||
Channel(String),
|
Channel(String),
|
||||||
}
|
}
|
||||||
|
@ -59,6 +61,7 @@ pub use ModType::BotModule;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Chat {
|
pub struct Chat {
|
||||||
pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
|
pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
|
||||||
pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>,
|
pub client : TwitchIRCClient<TCPTransport<TLS>,StaticLoginCredentials>,
|
||||||
|
@ -268,12 +271,16 @@ impl BotInstance
|
||||||
|
|
||||||
while let Some(message) = self.incoming_messages.recv().await {
|
while let Some(message) = self.incoming_messages.recv().await {
|
||||||
// Below can be used to debug if I want to capture all messages
|
// Below can be used to debug if I want to capture all messages
|
||||||
// println!("Received message: {:?}", message);
|
//println!("Received message: {:?}", message);
|
||||||
|
|
||||||
match message {
|
match message {
|
||||||
ServerMessage::Notice(msg) => {
|
ServerMessage::Notice(msg) => {
|
||||||
if let Some(chnl) = msg.channel_login {
|
// if let Some(chnl) = msg.channel_login {
|
||||||
println!("NOTICE : (#{}) {}", chnl, msg.message_text);
|
// println!("NOTICE : (#{}) {}", chnl, msg.message_text);
|
||||||
|
// }
|
||||||
|
match msg.channel_login {
|
||||||
|
Some(chnl) => println!("NOTICE : (#{}) {}", chnl, msg.message_text),
|
||||||
|
None => println!("NOTICE : {}", msg.message_text),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ServerMessage::Privmsg(msg) => {
|
ServerMessage::Privmsg(msg) => {
|
||||||
|
@ -282,7 +289,7 @@ impl BotInstance
|
||||||
println!("Privmsg section");
|
println!("Privmsg section");
|
||||||
|
|
||||||
// b.listener_main_prvmsg(&msg);
|
// b.listener_main_prvmsg(&msg);
|
||||||
self.listener_main_prvmsg(&msg).await;
|
self.listener_main_prvmsg(msg).await;
|
||||||
// - BotCommand listener should likely need to be called within the above
|
// - BotCommand listener should likely need to be called within the above
|
||||||
|
|
||||||
|
|
||||||
|
@ -361,7 +368,7 @@ impl BotInstance
|
||||||
// PRIVATE FUNCTIONS
|
// PRIVATE FUNCTIONS
|
||||||
|
|
||||||
|
|
||||||
async fn listener_main_prvmsg(&mut self,msg:& PrivmsgMessage) -> () {
|
async fn listener_main_prvmsg(&mut self,msg:PrivmsgMessage) -> () {
|
||||||
|
|
||||||
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
|
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
|
||||||
|
|
||||||
|
@ -394,10 +401,17 @@ impl BotInstance
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
self.chat.say_in_reply_to(msg,String::from("annytfLurk")).await;
|
// self.chat.say_in_reply_to(msg,String::from("annytfLurk")).await;
|
||||||
|
|
||||||
// // [ ] 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)
|
// // [ ] 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)
|
||||||
|
|
||||||
|
for (_m,acts) in &self.botmodules.botactions {
|
||||||
|
for a in acts {
|
||||||
|
if let crate::core::botmodules::BotAction::L(lsnr) = a {
|
||||||
|
lsnr.execute(self.chat.clone(),msg.clone()).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// // [ ] There should be a BotCommand Listener to check for prefixes ran
|
// // [ ] There should be a BotCommand Listener to check for prefixes ran
|
||||||
|
|
||||||
|
|
|
@ -201,7 +201,8 @@ pub mod bot_actions {
|
||||||
// pub type ExecBody<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output = F>>>>;
|
// pub type ExecBody<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output = F>>>>;
|
||||||
|
|
||||||
// pub type ExecBody<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output=F> + Send>> + Send + Sync>;
|
// pub type ExecBody<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output=F> + Send>> + Send + Sync>;
|
||||||
pub type ExecBody = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
|
pub type ExecBody = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
|
||||||
|
//pub type ExecBody = Box<dyn Fn(Chat,&PrivmsgMessage) -> Pin<Box<dyn Future<Output=()> + Send>> + Send + Sync>;
|
||||||
|
|
||||||
// pub fn asyncbox<F, T>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody<F>
|
// pub fn asyncbox<F, T>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody<F>
|
||||||
// where
|
// where
|
||||||
|
@ -213,6 +214,7 @@ pub mod bot_actions {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
pub fn asyncbox<T>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody
|
pub fn asyncbox<T>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody
|
||||||
|
//pub fn asyncbox<T>(f: fn(Chat,&PrivmsgMessage) -> T) -> ExecBody
|
||||||
where
|
where
|
||||||
//T: Future<Output = F> + 'static,
|
//T: Future<Output = F> + 'static,
|
||||||
//T: Future<Output = F> ,
|
//T: Future<Output = F> ,
|
||||||
|
@ -268,11 +270,11 @@ impl Listener
|
||||||
|
|
||||||
// async fn execute(&self,m:&mut Box<BotInstance<F>>,n:&PrivmsgMessage){
|
// async fn execute(&self,m:&mut Box<BotInstance<F>>,n:&PrivmsgMessage){
|
||||||
// async fn execute(&self,m:&mut botinstance::Chat,n:&PrivmsgMessage){
|
// async fn execute(&self,m:&mut botinstance::Chat,n:&PrivmsgMessage){
|
||||||
async fn execute(&self,m:botinstance::Chat,n:PrivmsgMessage){
|
pub async fn execute(&self,m:botinstance::Chat,n:PrivmsgMessage){
|
||||||
// => 12.23 - [ ] #todo requires field
|
// => 12.23 - [ ] #todo requires field
|
||||||
// (&self.exec)(String::from("Hello from BotAction Trait"))
|
// (&self.exec)(String::from("Hello from BotAction Trait"))
|
||||||
//self.exec_body(m,n)
|
//self.exec_body(m,n)
|
||||||
//(self.exec_body)(m,n).await;
|
(self.exec_body)(m,n).await;
|
||||||
// (self.exec_body)(m,n);
|
// (self.exec_body)(m,n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -362,7 +364,7 @@ struct Routine {}
|
||||||
pub struct ModulesManager
|
pub struct ModulesManager
|
||||||
{
|
{
|
||||||
statusdb: HashMap<ModType,Vec<ModStatusType>>,
|
statusdb: HashMap<ModType,Vec<ModStatusType>>,
|
||||||
botactions: HashMap<ModType,Vec<BotAction>>,
|
pub botactions: HashMap<ModType,Vec<BotAction>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl<F> ModulesManager<F>
|
// impl<F> ModulesManager<F>
|
||||||
|
|
|
@ -5,7 +5,7 @@ const TIME_THRESHOLD_S: u64 = 30;
|
||||||
const MSG_THRESHOLD: u32 = 20;
|
const MSG_THRESHOLD: u32 = 20;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct RateLimiter {
|
pub struct RateLimiter {
|
||||||
timer: Instant,
|
timer: Instant,
|
||||||
msgcounter: u32,
|
msgcounter: u32,
|
||||||
|
|
Loading…
Reference in a new issue