async trigger cond

This commit is contained in:
modulatingforce 2025-01-31 21:56:56 -05:00
parent 57f1a3c914
commit 739f7aeeef
13 changed files with 248 additions and 115 deletions

View file

@ -126,12 +126,10 @@ pub async fn main() {
} }
pub mod custom_mod { pub mod custom_mod {
use std::sync::Arc; use std::sync::Arc;
use forcebot_rs_v2::{asyncfn_box, Badge, Bot, Command, Module}; use forcebot_rs_v2::{execution_async, Badge, Bot, Command, Module};
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
@ -164,11 +162,12 @@ pub mod custom_mod {
} }
/* 3. Set Command flags */ /* 3. Set Command flags */
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
cmd.set_admin_only(false); cmd.set_admin_only(false);
cmd.set_min_badge(Badge::Moderator); cmd.set_min_badge(Badge::Moderator);
cmd cmd
}
} }
``` ```
@ -179,7 +178,7 @@ Bot with a simple listener that listens for all messages and prints in output
```rust ```rust
use std::sync::Arc; use std::sync::Arc;
use forcebot_rs_v2::{asyncfn_box, Bot, Listener}; use forcebot_rs_v2::{execution_async, Bot, Listener};
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
#[tokio::main] #[tokio::main]
@ -203,7 +202,7 @@ pub async fn main() {
} }
/* 2d. Set and Store the execution body using `async_box()` */ /* 2d. Set and Store the execution body using `async_box()` */
listener.set_exec_fn(asyncfn_box(execbody)); listener.set_exec_fn(execution_async(execbody));
/* 3. Load the listener into the bot */ /* 3. Load the listener into the bot */
bot.load_listener(listener); bot.load_listener(listener);
@ -220,10 +219,11 @@ pub async fn main() {
Example listener listens for a moderator badge and reply in chat Example listener listens for a moderator badge and reply in chat
```rust ```rust
use std::sync::Arc; use std::sync::Arc;
use forcebot_rs_v2::Bot; use forcebot_rs_v2::Bot;
use forcebot_rs_v2::asyncfn_box; use forcebot_rs_v2::execution_async;
use forcebot_rs_v2::Listener; use forcebot_rs_v2::Listener;
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
@ -238,10 +238,11 @@ pub async fn main() {
let mut listener = Listener::new(); let mut listener = Listener::new();
/* 2. Set a trigger condition function for listener */ /* 2. Set a trigger condition function for listener */
listener.set_trigger_cond_fn( listener.set_trigger_cond_fn(
|_:Arc<Bot>,message:ServerMessage| |_:Arc<Bot>,message:ServerMessage|
if let ServerMessage::Privmsg(msg) = message { if let ServerMessage::Privmsg(msg) = message {
// dbg!(msg.clone());
for badge in msg.badges { for badge in msg.badges {
if matches!(badge, x if x.name == "moderator") { if matches!(badge, x if x.name == "moderator") {
// dbg!("moderator found"); // dbg!("moderator found");
@ -262,7 +263,7 @@ pub async fn main() {
} }
/* 4. Set and Store the execution body using `async_box()` */ /* 4. Set and Store the execution body using `async_box()` */
listener.set_exec_fn(asyncfn_box(execbody)); listener.set_exec_fn(execution_async(execbody));
/* 5. Load the listener into the bot */ /* 5. Load the listener into the bot */
bot.load_listener(listener); bot.load_listener(listener);
@ -273,6 +274,7 @@ pub async fn main() {
} }
``` ```
## Simple Test Command ## Simple Test Command
@ -280,8 +282,9 @@ pub async fn main() {
```rust ```rust
use std::sync::Arc; use std::sync::Arc;
use forcebot_rs_v2::Badge;
use forcebot_rs_v2::Bot; use forcebot_rs_v2::Bot;
use forcebot_rs_v2::asyncfn_box; use forcebot_rs_v2::execution_async;
use forcebot_rs_v2::Command; use forcebot_rs_v2::Command;
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
@ -305,7 +308,7 @@ pub async fn main() {
} }
/* 3. Set and Store the execution body using `async_box()` */ /* 3. Set and Store the execution body using `async_box()` */
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
/* 4. optionally, remove admin only default flag */ /* 4. optionally, remove admin only default flag */
cmd.set_admin_only(false); cmd.set_admin_only(false);
@ -321,6 +324,7 @@ pub async fn main() {
} }
``` ```
# Crate Rust Documentation # Crate Rust Documentation

View file

@ -41,7 +41,7 @@ pub async fn main() {
pub mod funbot_objs { pub mod funbot_objs {
use std::sync::Arc; use std::sync::Arc;
use forcebot_rs_v2::{asyncfn_box, Badge, Bot, Command, Module}; use forcebot_rs_v2::{execution_async, Badge, Bot, Command, Module};
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
/// Create a Module with a loaded Command object /// Create a Module with a loaded Command object
@ -69,7 +69,7 @@ pub mod funbot_objs {
Result::Err("Not Valid message type".to_string()) Result::Err("Not Valid message type".to_string())
} }
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
cmd.set_admin_only(false); cmd.set_admin_only(false);
cmd.set_min_badge(Badge::Vip); cmd.set_min_badge(Badge::Vip);

View file

@ -14,7 +14,7 @@
use std::sync::Arc; use std::sync::Arc;
use forcebot_rs_v2::Bot; use forcebot_rs_v2::Bot;
use forcebot_rs_v2::asyncfn_box; use forcebot_rs_v2::execution_async;
use forcebot_rs_v2::Listener; use forcebot_rs_v2::Listener;
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
@ -54,7 +54,7 @@ pub async fn main() {
} }
/* 4. Set and Store the execution body using `async_box()` */ /* 4. Set and Store the execution body using `async_box()` */
listener.set_exec_fn(asyncfn_box(execbody)); listener.set_exec_fn(execution_async(execbody));
/* 5. Load the listener into the bot */ /* 5. Load the listener into the bot */
bot.load_listener(listener); bot.load_listener(listener);

View file

@ -17,7 +17,7 @@ use std::sync::Arc;
use forcebot_rs_v2::Badge; use forcebot_rs_v2::Badge;
use forcebot_rs_v2::Bot; use forcebot_rs_v2::Bot;
use forcebot_rs_v2::asyncfn_box; use forcebot_rs_v2::execution_async;
use forcebot_rs_v2::Command; use forcebot_rs_v2::Command;
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
@ -41,7 +41,7 @@ pub async fn main() {
} }
/* 3. Set and Store the execution body using `async_box()` */ /* 3. Set and Store the execution body using `async_box()` */
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
/* 4. optionally, remove admin only default flag */ /* 4. optionally, remove admin only default flag */
cmd.set_admin_only(false); cmd.set_admin_only(false);

View file

@ -12,7 +12,7 @@
use std::sync::Arc; use std::sync::Arc;
use forcebot_rs_v2::{asyncfn_box, Bot, Listener}; use forcebot_rs_v2::{execution_async, Bot, Listener};
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
#[tokio::main] #[tokio::main]
@ -36,7 +36,7 @@ pub async fn main() {
} }
/* 2d. Set and Store the execution body using `async_box()` */ /* 2d. Set and Store the execution body using `async_box()` */
listener.set_exec_fn(asyncfn_box(execbody)); listener.set_exec_fn(execution_async(execbody));
/* 3. Load the listener into the bot */ /* 3. Load the listener into the bot */
bot.load_listener(listener); bot.load_listener(listener);

View file

@ -37,7 +37,7 @@ pub async fn main() {
pub mod custom_mod { pub mod custom_mod {
use std::sync::Arc; use std::sync::Arc;
use forcebot_rs_v2::{asyncfn_box, Badge, Bot, Command, Module}; use forcebot_rs_v2::{execution_async, Badge, Bot, Command, Module};
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
@ -70,7 +70,7 @@ pub mod custom_mod {
} }
/* 3. Set Command flags */ /* 3. Set Command flags */
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
cmd.set_admin_only(false); cmd.set_admin_only(false);
cmd.set_min_badge(Badge::Moderator); cmd.set_min_badge(Badge::Moderator);

View file

@ -192,7 +192,7 @@ impl Bot
for listener in module.get_listeners() { for listener in module.get_listeners() {
let a = listener.clone(); let a = listener.clone();
if a.cond_triggered(bot.clone(),message.clone()) { if a.cond_triggered(bot.clone(),message.clone()) && a.cond_async_triggered(bot.clone(), message.clone()).await {
let _ = listener.execute_fn(bot.clone(),message.clone()).await; let _ = listener.execute_fn(bot.clone(),message.clone()).await;
} }

View file

@ -25,13 +25,25 @@ pub type ExecBody = Box<
dyn Fn(Arc<Bot>,ServerMessage) -> Pin<Box<dyn Future<Output = Result<String,String>> + Send>> + Send + Sync, dyn Fn(Arc<Bot>,ServerMessage) -> Pin<Box<dyn Future<Output = Result<String,String>> + Send>> + Send + Sync,
>; >;
pub fn asyncfn_box<T>(f: fn(Arc<Bot>,ServerMessage) -> T) -> ExecBody pub fn execution_async<T>(f: fn(Arc<Bot>,ServerMessage) -> T) -> ExecBody
where where
T: Future<Output = Result<String,String>> + Send + 'static, T: Future<Output = Result<String,String>> + Send + 'static,
{ {
Box::new(move |a,b| Box::pin(f(a,b))) Box::new(move |a,b| Box::pin(f(a,b)))
} }
pub type TrigBody = Box<
dyn Fn(Arc<Bot>,ServerMessage) -> Pin<Box<dyn Future<Output = bool> + Send>> + Send + Sync,
>;
pub fn condition_async<T>(f: fn(Arc<Bot>,ServerMessage) -> T) -> TrigBody
where
T: Future<Output = bool> + Send + 'static,
{
Box::new(move |a,b| Box::pin(f(a,b)))
}
/// collection of functions to create built in objects /// collection of functions to create built in objects
@ -42,7 +54,7 @@ pub mod built_in_objects {
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
use crate::{asyncfn_box, modules::Status, Badge, Bot, Command}; use crate::{execution_async, modules::Status, Badge, Bot, Command};
/// create a vector of command build in objects /// create a vector of command build in objects
@ -83,7 +95,7 @@ pub mod built_in_objects {
} }
/* 3. Set and Store the execution body using `async_box()` */ /* 3. Set and Store the execution body using `async_box()` */
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
/* 4. optionally, remove admin only default flag */ /* 4. optionally, remove admin only default flag */
cmd.set_admin_only(false); cmd.set_admin_only(false);
@ -138,7 +150,7 @@ pub mod built_in_objects {
} }
/* 3. Set and Store the execution body using `async_box()` */ /* 3. Set and Store the execution body using `async_box()` */
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
/* 4. optionally, remove admin only default flag */ /* 4. optionally, remove admin only default flag */
cmd.set_admin_only(false); cmd.set_admin_only(false);
@ -241,7 +253,7 @@ pub mod built_in_objects {
} }
/* 3. Set and Store the execution body using `async_box()` */ /* 3. Set and Store the execution body using `async_box()` */
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
/* 4. optionally, remove admin only default flag */ /* 4. optionally, remove admin only default flag */
cmd.set_admin_only(true); cmd.set_admin_only(true);

View file

@ -2,9 +2,9 @@ use std::sync::Arc;
use twitch_irc::message::{PrivmsgMessage, ServerMessage}; use twitch_irc::message::{PrivmsgMessage, ServerMessage};
use crate::{asyncfn_box, botcore::bot::Bot, Badge}; use crate::{botcore::{bot::Bot, bot_objects::condition_async}, execution_async, Badge};
use super::ExecBody; use super::{ExecBody, TrigBody};
/// Bot `Command` that stores trigger condition callback and a execution functon /// Bot `Command` that stores trigger condition callback and a execution functon
/// ///
@ -16,7 +16,7 @@ use super::ExecBody;
/// ///
/// AdminOnly commands can only be ran by admin /// AdminOnly commands can only be ran by admin
/// ///
/// Use `asyncfn_box()` on custom async execution bodies /// Use `execution_async()` on custom async execution bodies
#[derive(Clone)] #[derive(Clone)]
pub struct Command pub struct Command
{ {
@ -28,6 +28,7 @@ pub struct Command
admin_override : bool, admin_override : bool,
prefix : String, prefix : String,
custom_cond_fn : fn(Arc<Bot>,PrivmsgMessage) -> bool, custom_cond_fn : fn(Arc<Bot>,PrivmsgMessage) -> bool,
custom_cond_async : Arc<TrigBody>,
} }
impl Command impl Command
@ -44,26 +45,51 @@ impl Command
async fn execbody(_:Arc<Bot>,_:ServerMessage) -> Result<String,String> async fn execbody(_:Arc<Bot>,_:ServerMessage) -> Result<String,String>
{ Result::Ok("success".to_string()) } { Result::Ok("success".to_string()) }
async fn condition01(_:Arc<Bot>,_:ServerMessage) -> bool { true }
Command { Command {
commands , commands ,
prefix , prefix ,
exec_fn : Arc::new(asyncfn_box(execbody)), exec_fn : Arc::new(execution_async(execbody)),
min_badge : Badge::Vip, min_badge : Badge::Vip,
admin_only : true, admin_only : true,
admin_override : false , admin_override : false ,
custom_cond_fn : |_:Arc<Bot>,_:PrivmsgMessage| true, custom_cond_fn : |_:Arc<Bot>,_:PrivmsgMessage| true,
custom_cond_async : Arc::new(condition_async(condition01)),
} }
} }
/// set a trigger conditin callback that returns true if the listener shoud trigger /// set a trigger conditin callback that returns true if the command should trigger
pub fn set_custom_cond_fn(&mut self,cond_fn: fn(Arc<Bot>,PrivmsgMessage) -> bool) { pub fn set_custom_cond_fn(&mut self,cond_fn: fn(Arc<Bot>,PrivmsgMessage) -> bool) {
self.custom_cond_fn = cond_fn; self.custom_cond_fn = cond_fn;
} }
/// sets the async trigger condition for listener
///
/// Same as `set_custom_cond_fn()` , but async define
///
/// Use`execution_async()` on the async fn when storing
///
/// Example -
/// ```rust
/// /* 1. Create a new blank Listener */
/// let mut cmd = Command::new();
///
/// /* 2. define an async function */
/// async fn condition01(_:Arc<Bot>,_:ServerMessage) -> bool { true }
///
/// /* 3. Set and Store the execution body using `async_box()` */
/// cmd.set_custom_cond_async(condition_async(condition01));
/// ```
///
pub fn set_custom_cond_async(&mut self,condition:TrigBody ) {
self.custom_cond_async = Arc::new(condition);
}
/// sets the execution body of the listener for when it triggers /// sets the execution body of the listener for when it triggers
/// ///
/// Use`asyncfn_box()` on the async fn when storing /// Use`execution_async()` on the async fn when storing
/// ///
/// ///
pub fn set_exec_fn(&mut self,exec_fn:ExecBody ) { pub fn set_exec_fn(&mut self,exec_fn:ExecBody ) {

View file

@ -2,9 +2,9 @@ use std::sync::Arc;
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
use crate::{asyncfn_box, Bot}; use crate::{botcore::bot_objects::condition_async, execution_async, Bot};
use super::ExecBody; use super::{ExecBody, TrigBody};
/// Bot `Listener`` that stores trigger condition callback and a execution functon /// Bot `Listener`` that stores trigger condition callback and a execution functon
/// ///
@ -14,6 +14,8 @@ pub struct Listener
{ {
/// trigger condition /// trigger condition
trigger_cond_fn : fn(Arc<Bot>,ServerMessage) -> bool, trigger_cond_fn : fn(Arc<Bot>,ServerMessage) -> bool,
/// trigger condition for async
trigger_cond_async : Arc<TrigBody> ,
/// execution body /// execution body
exec_fn : Arc<ExecBody>, exec_fn : Arc<ExecBody>,
} }
@ -26,10 +28,11 @@ impl Listener
pub fn new() -> Listener { pub fn new() -> Listener {
async fn execbody(_:Arc<Bot>,_:ServerMessage) -> Result<String,String> {Result::Ok("success".to_string()) } async fn execbody(_:Arc<Bot>,_:ServerMessage) -> Result<String,String> {Result::Ok("success".to_string()) }
async fn condition01(_:Arc<Bot>,_:ServerMessage) -> bool { true }
Listener { Listener {
trigger_cond_fn : |_:Arc<Bot>,_:ServerMessage| false, trigger_cond_fn : |_:Arc<Bot>,_:ServerMessage| false,
exec_fn : Arc::new(asyncfn_box(execbody)) trigger_cond_async : Arc::new(condition_async(condition01)),
exec_fn : Arc::new(execution_async(execbody)),
} }
} }
@ -38,6 +41,29 @@ impl Listener
self.trigger_cond_fn = cond_fn; self.trigger_cond_fn = cond_fn;
} }
/// sets the async trigger condition for listener
///
/// Same as `set_trigger_cond_fn()` , but async define
///
/// Use`asyncfn_box()` on the async fn when storing
///
/// Example -
/// ```rust
/// /* 1. Create a new blank Listener */
/// let mut listener = Listener::new();
///
/// /* 2. define an async function */
/// async fn condition01(_:Arc<Bot>,_:ServerMessage) -> bool { true }
///
/// /* 3. Set and Store the execution body using `async_box()` */
/// listener.set_trigger_cond_async(condition_async(condition01));
/// ```
///
pub fn set_trigger_cond_async(&mut self,condition:TrigBody ) {
self.trigger_cond_async = Arc::new(condition);
}
/// sets the execution body of the listener for when it triggers /// sets the execution body of the listener for when it triggers
/// ///
/// Use`asyncfn_box()` on the async fn when storing /// Use`asyncfn_box()` on the async fn when storing
@ -60,7 +86,13 @@ impl Listener
/// checks if the trigger condition is met /// checks if the trigger condition is met
pub fn cond_triggered(&self,bot:Arc<Bot>,msg:ServerMessage) -> bool { pub fn cond_triggered(&self,bot:Arc<Bot>,msg:ServerMessage) -> bool {
(self.trigger_cond_fn)(bot,msg) (self.trigger_cond_fn)(bot,msg)
}
/// executes the listeners executon body
pub async fn cond_async_triggered(&self,bot:Arc<Bot>,msg:ServerMessage) -> bool {
// (self.exec_fn)(bot,msg)
(self.trigger_cond_async)(bot,msg).await
} }
/// executes the listeners executon body /// executes the listeners executon body

View file

@ -2,7 +2,7 @@ use std::{sync::Arc, time::{Duration, Instant}};
use twitch_irc::message::ServerMessage; use twitch_irc::message::ServerMessage;
use crate::{asyncfn_box, Badge, Bot, Command, Module}; use crate::{execution_async, Badge, Bot, Command, Module};
/// guest_badge / guest module /// guest_badge / guest module
/// ///
@ -88,7 +88,7 @@ fn create_cmd_vip() -> Command {
Result::Err("Not Valid message type".to_string()) Result::Err("Not Valid message type".to_string())
} }
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
cmd.set_admin_only(false); cmd.set_admin_only(false);
cmd.set_admin_override(true); cmd.set_admin_override(true);
@ -131,7 +131,7 @@ fn create_cmd_mod() -> Command {
Result::Err("Not Valid message type".to_string()) Result::Err("Not Valid message type".to_string())
} }
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(execution_async(execbody));
cmd.set_admin_only(false); cmd.set_admin_only(false);
cmd.set_admin_override(true); cmd.set_admin_override(true);

View file

@ -1,14 +1,17 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use twitch_irc::message::{PrivmsgMessage, ServerMessage}; use twitch_irc::message::{PrivmsgMessage, ServerMessage};
use crate::{asyncfn_box, Bot, Listener, Module}; use crate::{condition_async, Badge, Bot, Command, Listener, Module};
/// pyramid module /// pyramid module
/// ///
/// for detecting & handling pyramids /// for detecting & handling pyramids
/// ///
/// /// - listener - detects pyramid
/// - cmd & listener - interrupts some chatters temporarily
/// ///
/// ///
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -38,13 +41,21 @@ fn create_pyramid_detector() -> Listener {
/* 1. Create a new blank Listener */ /* 1. Create a new blank Listener */
let mut listener = Listener::new(); let mut listener = Listener::new();
/* 2. Set a trigger condition function for listener */ /* 2. Define an async trigger condition callback */
listener.set_trigger_cond_fn( async fn condition01(bot:Arc<Bot>,message:ServerMessage) -> bool {
|_bot:Arc<Bot>,_message:ServerMessage| if let ServerMessage::Privmsg(msg) = message {
true if detect_pyramid_complete_ok(bot.clone(), msg.clone()).await {
); return true;
}
}
false
}
/* 3. Set a trigger condition function for listener */
listener.set_trigger_cond_async(condition_async(condition01));
/* 3. Define an async fn callback execution */ /* 4. Define an async fn callback execution */
async fn execbody(bot:Arc<Bot>,message:ServerMessage) -> Result<String,String> { async fn execbody(bot:Arc<Bot>,message:ServerMessage) -> Result<String,String> {
if let ServerMessage::Privmsg(msg) = message { if let ServerMessage::Privmsg(msg) = message {
if detect_pyramid_complete_ok(bot.clone(), msg.clone()).await { if detect_pyramid_complete_ok(bot.clone(), msg.clone()).await {
@ -55,8 +66,8 @@ fn create_pyramid_detector() -> Listener {
Result::Err("Not Valid message type".to_string()) Result::Err("Not Valid message type".to_string())
} }
/* 4. Set and Store the execution body using `async_box()` */ /* 5. Set and Store the execution body using `async_box()` */
listener.set_exec_fn(asyncfn_box(execbody)); listener.set_exec_fn(Box::new(move |a,b| Box::pin(execbody(a,b))));
listener listener
@ -109,6 +120,10 @@ lazy_static!{
pub static ref START_PATTERNS_PER_CHNL: Mutex<Vec<(String,Mutex<String>)>> = Mutex::new(vec![]); pub static ref START_PATTERNS_PER_CHNL: Mutex<Vec<(String,Mutex<String>)>> = Mutex::new(vec![]);
/// temp message stack checker /// temp message stack checker
pub static ref TEMP_MSG_STACK: Mutex<Vec<String>> = Mutex::new(vec![]); pub static ref TEMP_MSG_STACK: Mutex<Vec<String>> = Mutex::new(vec![]);
/// interruptor targets - (channel:String,chatters:Vec<String>>)
pub static ref INTERRUPT_TRG_PER_CHNL: Mutex<Vec<(String,Mutex<Vec<String>>)>> = Mutex::new(vec![]);
} }
fn read_top_of_compare(channel:String) -> Option<String> { fn read_top_of_compare(channel:String) -> Option<String> {
@ -268,3 +283,46 @@ fn symmetry_ok(channel:String) -> bool {
} }
/// pyramid interruptor #todo
///
/// pick chatters that will be interrupted if they solo build
///
/// takes in arguments as chatters
///
/// chatters are then interrupted for a random duration under 15m
///
/// if a duration is given, take that duration eg 15m , 25m
///
///
fn create_interruptor_cmd() -> Command {
let mut cmd = Command::new(vec![
"no pyramid".to_string(),
"no pyramids".to_string()
],
"".to_string());
/* 2. Define an async fn callback execution */
async fn execbody(bot:Arc<Bot>,message:ServerMessage) -> Result<String,String> {
if let ServerMessage::Privmsg(msg) = message {
let _ = bot.client.say_in_reply_to(&msg, String::from("test success")).await;
return Result::Ok("Success".to_string()) ;
}
Result::Err("Not Valid message type".to_string())
}
/* 3. Set and Store the execution body using `async_box()` */
cmd.set_exec_fn(Box::new(move |a,b| Box::pin(execbody(a,b))));
/* 4. optionally, remove admin only default flag */
cmd.set_admin_only(false);
/* 5. optionally, set min badge*/
cmd.set_min_badge(Badge::Moderator);
cmd
}

View file

@ -36,65 +36,65 @@
//! //!
//! ```rust //! ```rust
//! use forcebot_rs_v2::Bot; //! use forcebot_rs_v2::Bot;
//! //!
//! #[tokio::main] //! #[tokio::main]
//! pub async fn main() { //! pub async fn main() {
//! //!
//! /* Create the bot using env */ //! /* Create the bot using env */
//! let mut bot = Bot::new(); //! let mut bot = Bot::new();
//! //!
//! /* load the Module */ //! /* load the Module */
//! bot.load_module(custom_mod::new()); //! bot.load_module(custom_mod::new());
//! //!
//! /* Run the bot */ //! /* Run the bot */
//! bot.run().await; //! bot.run().await;
//! //!
//! } //! }
//! //!
//! //! pub mod custom_mod {
//! pub mod custom_mod { //! use std::sync::Arc;
//! use std::sync::Arc; //!
//! //! use forcebot_rs_v2::{execution_async, Badge, Bot, Command, Module};
//! use forcebot_rs_v2::{asyncfn_box, Badge, Bot, Command, Module}; //! use twitch_irc::message::ServerMessage;
//! use twitch_irc::message::ServerMessage; //!
//! //!
//! //! /// Module definition with a loaded command
//! /// Module definition with a loaded command //! pub fn new() -> Module {
//! pub fn new() -> Module { //! /* 1. Create a new module */
//! /* 1. Create a new module */ //! let mut custom_mod = Module::new(
//! let mut custom_mod = Module::new( //! vec!["test".to_string()],
//! vec!["test".to_string()], //! "".to_string());
//! "".to_string()); //!
//! //! /* 2. Load the cmd into a new module */
//! /* 2. Load the cmd into a new module */ //! custom_mod.load_command(cmd_test());
//! custom_mod.load_command(cmd_test()); //!
//! //! custom_mod
//! custom_mod //!
//! //! }
//! } //!
//! //! /// Command definition
//! /// Command definition //! pub fn cmd_test() -> Command {
//! pub fn cmd_test() -> Command { //! /* 1. Create a new cmd */
//! /* 1. Create a new cmd */ //! let mut cmd = Command::new(vec!["test".to_string()],"".to_string());
//! let mut cmd = Command::new(vec!["test".to_string()],"".to_string()); //!
//! //! /* 2. Define exec callback */
//! /* 2. Define exec callback */ //! async fn execbody(bot:Arc<Bot>,message:ServerMessage) -> Result<String,String> {
//! async fn execbody(bot:Arc<Bot>,message:ServerMessage) -> Result<String,String> { //! if let ServerMessage::Privmsg(msg) = message {
//! if let ServerMessage::Privmsg(msg) = message { //! let _= bot.client.say_in_reply_to(
//! let _= bot.client.say_in_reply_to( //! &msg, "test return".to_string()).await;
//! &msg, "test return".to_string()).await; //! }
//! } //! Result::Err("Not Valid message type".to_string())
//! Result::Err("Not Valid message type".to_string()) //! }
//! } //!
//! z //! /* 3. Set Command flags */
//! /* 3. Set Command flags */ //! cmd.set_exec_fn(execution_async(execbody));
//! cmd.set_exec_fn(asyncfn_box(execbody)); //! cmd.set_admin_only(false);
//! cmd.set_admin_only(false); //! cmd.set_min_badge(Badge::Moderator);
//! cmd.set_min_badge(Badge::Moderator); //!
//! //! cmd
//! cmd //! }
//! } //! }
//! } //!
//! //!
//! ``` //! ```
//! //!
@ -104,7 +104,7 @@
//! ```rust //! ```rust
//! use std::sync::Arc; //! use std::sync::Arc;
//! //!
//! use forcebot_rs_v2::{asyncfn_box, Bot, Listener}; //! use forcebot_rs_v2::{execution_async, Bot, Listener};
//! use twitch_irc::message::ServerMessage; //! use twitch_irc::message::ServerMessage;
//! //!
//! #[tokio::main] //! #[tokio::main]
@ -128,7 +128,7 @@
//! } //! }
//! //!
//! /* 2d. Set and Store the execution body using `async_box()` */ //! /* 2d. Set and Store the execution body using `async_box()` */
//! listener.set_exec_fn(asyncfn_box(execbody)); //! listener.set_exec_fn(execution_async(execbody));
//! //!
//! /* 3. Load the listener into the bot */ //! /* 3. Load the listener into the bot */
//! bot.load_listener(listener); //! bot.load_listener(listener);
@ -147,7 +147,7 @@
//! use std::sync::Arc; //! use std::sync::Arc;
//! //!
//! use forcebot_rs_v2::Bot; //! use forcebot_rs_v2::Bot;
//! use forcebot_rs_v2::asyncfn_box; //! use forcebot_rs_v2::execution_async;
//! use forcebot_rs_v2::Listener; //! use forcebot_rs_v2::Listener;
//! use twitch_irc::message::ServerMessage; //! use twitch_irc::message::ServerMessage;
//! //!
@ -187,7 +187,7 @@
//! } //! }
//! //!
//! /* 4. Set and Store the execution body using `async_box()` */ //! /* 4. Set and Store the execution body using `async_box()` */
//! listener.set_exec_fn(asyncfn_box(execbody)); //! listener.set_exec_fn(execution_async(execbody));
//! //!
//! /* 5. Load the listener into the bot */ //! /* 5. Load the listener into the bot */
//! bot.load_listener(listener); //! bot.load_listener(listener);
@ -202,7 +202,8 @@
pub mod botcore; pub mod botcore;
pub mod custom_mods; pub mod custom_mods;
pub use crate::botcore::bot::Bot; pub use crate::botcore::bot::Bot;
pub use crate::botcore::bot_objects::asyncfn_box; pub use crate::botcore::bot_objects::execution_async;
pub use crate::botcore::bot_objects::condition_async;
pub use crate::botcore::bot_objects::listener::Listener; pub use crate::botcore::bot_objects::listener::Listener;
pub use crate::botcore::bot_objects::command::Command; pub use crate::botcore::bot_objects::command::Command;
pub use crate::botcore::modules::Module; pub use crate::botcore::modules::Module;