Custom Exec Bodies for BotCommand and Listeners #9

Merged
modulatingforce merged 14 commits from dev into main 2024-01-29 05:46:43 -05:00
5 changed files with 109 additions and 70 deletions
Showing only changes of commit b9ea781ed8 - Show all commits

View file

@ -145,9 +145,10 @@ impl Chat {
pub struct BotInstance
// where
// F: std::future::Future + ?Sized,
pub struct BotInstance<F>
where
// F: std::future::Future + ?Sized,
F: std::future::Future
{
prefix : char,
bot_channel : ChType,
@ -156,7 +157,7 @@ pub struct BotInstance
// pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
pub chat : Chat,
// botmodules : HashMap<ModType,Vec<EnType>>,
pub botmodules : ModulesManager,
pub botmodules : ModulesManager<F>,
twitch_oauth : String,
pub bot_channels : Vec<ChType>,
/*bot_commands : Vec[BotCommand],
@ -168,16 +169,16 @@ pub struct BotInstance
impl BotInstance
// where
// F: std::future::Future + 'static,
// //F: 'static,
impl<F> BotInstance<F>
where
F: std::future::Future + 'static,
//F: 'static,
{
pub fn init() -> BotInstance
// where
// F: std::future::Future + 'static,
pub fn init() -> BotInstance<F>
where
F: std::future::Future + 'static,
{
dotenv().ok();
@ -297,51 +298,51 @@ impl BotInstance
pub async fn run(mut self) -> () {
// pub async fn run(mut self) -> () {
let join_handle = tokio::spawn(async move {
// let join_handle = tokio::spawn(async move {
while let Some(message) = self.incoming_messages.recv().await {
// Below can be used to debug if I want to capture all messages
// println!("Received message: {:?}", message);
// while let Some(message) = self.incoming_messages.recv().await {
// // Below can be used to debug if I want to capture all messages
// // println!("Received message: {:?}", message);
match message {
ServerMessage::Notice(msg) => {
if let Some(chnl) = msg.channel_login {
println!("NOTICE : (#{}) {}", chnl, msg.message_text);
}
}
ServerMessage::Privmsg(msg) => {
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
// match message {
// ServerMessage::Notice(msg) => {
// if let Some(chnl) = msg.channel_login {
// println!("NOTICE : (#{}) {}", chnl, msg.message_text);
// }
// }
// ServerMessage::Privmsg(msg) => {
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
println!("Privmsg section");
// println!("Privmsg section");
// b.listener_main_prvmsg(&msg);
self.listener_main_prvmsg(&msg).await;
// - BotCommand listener should likely need to be called within the above
// // b.listener_main_prvmsg(&msg);
// self.listener_main_prvmsg(&msg).await;
// // - 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);
},
_ => {}
}
}
});
// },
// 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();
}
// join_handle.await.unwrap();
// }
// -----------------

View file

@ -58,25 +58,25 @@ pub enum ModStatusType {
// pub use EnType::Enabled;
#[derive(Debug)]
pub enum BotAction
//#[derive(Debug)]
pub enum BotAction<F>
// where
// F: std::future::Future + ?Sized,
{
//C(BotCommand),
L(Listener),
L(Listener<F>),
R(Routine),
}
pub trait BotActionTrait
// where
// F: std::future::Future,
pub trait BotActionTrait<F>
where
F: std::future::Future,
{
// async fn execute(&self,m:&mut BotInstance,n:&PrivmsgMessage);
// fn add_to_bot(self, bot:BotInstance) -> Result<String,Box<dyn Error>>;
fn add_to_bot(self, bot:BotInstance);
fn add_to_modmgr(self, modmgr:ModulesManager) -> ModulesManager;
fn add_to_bot(self, bot:BotInstance<F>);
fn add_to_modmgr(self, modmgr:ModulesManager<F>) -> ModulesManager<F>;
}
@ -164,6 +164,43 @@ pub trait BotActionTrait
// }
pub mod bot_actions {
// use std::boxed::Box;
// use std::pin::Pin;
// use std::future::Future;
// use crate::core::botinstance::Chat;
// use twitch_irc::message::PrivmsgMessage;
// type ExecBody<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output = F>>>>;
pub mod actions_util {
use std::future::Future;
use std::boxed::Box;
use std::pin::Pin;
use crate::core::botinstance::Chat;
use twitch_irc::message::PrivmsgMessage;
pub type ExecBody<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output = F>>>>;
pub fn asyncbox<F, T>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody<F>
where
T: Future<Output = F> + 'static,
//T: Future<Output = F> ,
{
Box::new(move |a,b| Box::pin(f(a,b)))
}
}
}
// #[derive(Debug)]
// pub struct Listener {
// pub module : ModType,
@ -172,8 +209,8 @@ pub trait BotActionTrait
// pub help : String
// }
#[derive(Debug)]
pub struct Listener
// #[derive(Debug)]
pub struct Listener<F>
// where
// F: std::future::Future + ?Sized,
{
@ -184,10 +221,11 @@ pub struct Listener
//pub exec_body : fn(&mut botinstance::Chat,&PrivmsgMessage) -> F ,
// pub exec_body : fn(botinstance::Chat,PrivmsgMessage) -> F ,
//pub exec_body : fn(String,&PrivmsgMessage) -> F ,
pub exec_body : bot_actions::actions_util::ExecBody<F>,
pub help : String
}
impl Listener
impl<F> Listener<F>
// where
// F: std::future::Future,
{
@ -203,16 +241,16 @@ impl Listener
}
}
impl BotActionTrait for Listener
// where
// F: std::future::Future,
impl<F> BotActionTrait<F> for Listener<F>
where
F: std::future::Future,
{
// fn add_to_bot(&self) -> Result<String,Box<dyn Error>> {
// println!("Calling Add to Bot");
// Ok(String::from("Hello"))
// }
fn add_to_bot(self, bot:BotInstance) {
fn add_to_bot(self, bot:BotInstance<F>) {
// let mut mgr = bot.botmodules;
// let nmod = self.module.clone();
// mgr.add_botaction(nmod, BotAction::C(self));
@ -225,7 +263,7 @@ impl BotActionTrait for Listener
// fn add_to_modmgr(self, modmgr:&mut ModulesManager<F>) {
//fn add_to_modmgr(self, mut modmgr:ModulesManager) {
fn add_to_modmgr(self, mut modmgr:ModulesManager) -> ModulesManager {
fn add_to_modmgr(self, mut modmgr:ModulesManager<F>) -> ModulesManager<F> {
// // let mut mgr = bot.botmodules;
// // let nmod = self.module.clone();
// // mgr.add_botaction(nmod, BotAction::C(self));
@ -269,21 +307,21 @@ impl BotActionTrait for Listener
struct Routine {}
#[derive(Debug)]
pub struct ModulesManager
//#[derive(Debug)]
pub struct ModulesManager<F>
// where
// F: std::future::Future + ?Sized,
{
statusdb: HashMap<ModType,Vec<ModStatusType>>,
botactions: HashMap<ModType,Vec<BotAction>>,
botactions: HashMap<ModType,Vec<BotAction<F>>>,
}
impl ModulesManager
impl<F> ModulesManager<F>
// where
// F: std::future::Future,
{
pub fn init() -> ModulesManager
pub fn init() -> ModulesManager<F>
// where
// F: std::future::Future ,
{
@ -412,7 +450,7 @@ impl ModulesManager
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> {
//pub fn add_botaction(&mut self, in_module:ModType, in_action:BotAction ) -> () {
/*
adds a BotAction to the Modules Manager - This will require a BotModule passed as well
@ -441,7 +479,7 @@ impl ModulesManager
// - 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
fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option<ModType>
fn find_conflict_module<F>(mgr:& ModulesManager<F>, act:& BotAction<F>) -> Option<ModType>
// where
// F: std::future::Future,
{

View file

@ -24,7 +24,7 @@ use crate::core::botinstance::BotInstance;
#[tokio::main]
pub async fn main() {
// let bot:BotInstance<dyn std::future::Future<Output = ()>> = BotInstance::init();
//let bot:BotInstance<dyn std::future::Future<Output = ()>> = BotInstance::init();
let bot = BotInstance::init();

View file

@ -19,7 +19,7 @@ mod experiments;
// [ ] init() function that accepts bot instance - this is passed to init() on submodules
pub fn init(mgr:ModulesManager) -> ModulesManager
pub fn init<F>(mgr:ModulesManager<F>) -> ModulesManager<F>
// where
// F: std::future::Future,
{

View file

@ -17,7 +17,7 @@ use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait,
use crate::core::botinstance::{self};
use twitch_irc::message::PrivmsgMessage;
pub fn init(mgr:ModulesManager) -> ModulesManager
pub fn init<F>(mgr:ModulesManager<F>) -> ModulesManager<F>
// where
// F: std::future::Future,
{