diff --git a/readme.md b/readme.md index e44ecf9..d192a1b 100644 --- a/readme.md +++ b/readme.md @@ -98,22 +98,19 @@ pub async fn main() { ``` -## Module with Custom Command +## Customize with Modules -A `Module` is a group of bot objects (eg `Command`) that elevated users can manage. +A `Module` is a group of bot objects (eg `Command`) that elevated users can manage through built in `disable` and `enable` commands -Bot objects are recommended to be loaded through a `Module` +Create a custom `Module` by : + +1. Defining Functions that create the Custom Bot Objects (eg `Command`) + +2. Define a function that creates a `Module` with the Custom Bot Objects loaded ```rust -use std::sync::Arc; - use forcebot_rs_v2::Bot; -use forcebot_rs_v2::asyncfn_box; -use forcebot_rs_v2::Command; -use forcebot_rs_v2::Module; -use twitch_irc::message::ServerMessage; - #[tokio::main] pub async fn main() { @@ -121,35 +118,56 @@ pub async fn main() { /* Create the bot using env */ let mut bot = Bot::new(); - /* 1. Create a new module */ - let mut custom_mod = Module::new("test".to_string(), "".to_string()); - - /* 2. Create a new cmd */ - let mut cmd = Command::new("test".to_string(),"".to_string()); - - 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, "test return".to_string()).await; - } - Result::Err("Not Valid message type".to_string()) - } - - cmd.set_exec_fn(asyncfn_box(execbody)); - cmd.set_admin_only(false); - cmd.set_min_badge("moderator".to_string()); - - /* 3. Load the cmd into a new module */ - custom_mod.load_command(cmd); - - /* 4. Load the module into the bot */ - bot.load_module(custom_mod); + /* load the Module */ + bot.load_module(custom_mod::new()); /* Run the bot */ bot.run().await; } + +pub mod custom_mod { + use std::sync::Arc; + + use forcebot_rs_v2::{asyncfn_box, Badge, Bot, Command, Module}; + use twitch_irc::message::ServerMessage; + + + /// Module with a loaded command + pub fn new() -> Module { + /* 1. Create a new module */ + let mut custom_mod = Module::new("test".to_string(), "".to_string()); + + /* 2. Load the cmd into a new module */ + custom_mod.load_command(cmd_test()); + + custom_mod + + } + + pub fn cmd_test() -> Command { + /* 1. Create a new cmd */ + let mut cmd = Command::new("test".to_string(),"".to_string()); + + /* 2. Define exec callback */ + 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, "test return".to_string()).await; + } + Result::Err("Not Valid message type".to_string()) + } + + /* 3. Set Command flags */ + cmd.set_exec_fn(asyncfn_box(execbody)); + cmd.set_admin_only(false); + cmd.set_min_badge(Badge::Moderator); + + cmd + } +} + ``` ## Simple Debug Listener @@ -194,7 +212,7 @@ pub async fn main() { ``` -## Moderator Reator +## Moderator Reactor Example listener listens for a moderator badge and reply in chat diff --git a/src/bin/fun_bot.rs b/src/bin/fun_bot.rs index 9ed8eb5..de67fda 100644 --- a/src/bin/fun_bot.rs +++ b/src/bin/fun_bot.rs @@ -34,7 +34,7 @@ pub async fn main() { pub mod funbot_objs { use std::sync::Arc; - use forcebot_rs_v2::{asyncfn_box, Bot, Command, Module}; + use forcebot_rs_v2::{asyncfn_box, Badge, Bot, Command, Module}; use twitch_irc::message::ServerMessage; /// Create a Module with a loaded Command object @@ -63,7 +63,7 @@ pub mod funbot_objs { cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_admin_only(false); - cmd.set_min_badge("vip".to_string()); + cmd.set_min_badge(Badge::Vip); cmd } diff --git a/src/bin/simple_command_bot.rs b/src/bin/simple_command_bot.rs index 0e60bc1..a6ec70e 100644 --- a/src/bin/simple_command_bot.rs +++ b/src/bin/simple_command_bot.rs @@ -15,6 +15,7 @@ use std::sync::Arc; +use forcebot_rs_v2::Badge; use forcebot_rs_v2::Bot; use forcebot_rs_v2::asyncfn_box; use forcebot_rs_v2::Command; @@ -46,7 +47,7 @@ pub async fn main() { cmd.set_admin_only(false); /* 5. optionally, set min badge*/ - cmd.set_min_badge("broadcaster".to_string()); + cmd.set_min_badge(Badge::Moderator); /* 6. Load the cmd into the bot */ bot.load_command(cmd); diff --git a/src/bin/simple_module.rs b/src/bin/simple_module.rs index 3795f8e..61d66e5 100644 --- a/src/bin/simple_module.rs +++ b/src/bin/simple_module.rs @@ -17,14 +17,7 @@ //! - Get a Bot Chat Token here - <https://twitchtokengenerator.com> //! - More Info - <https://dev.twitch.tv/docs/authentication> -use std::sync::Arc; - use forcebot_rs_v2::Bot; -use forcebot_rs_v2::asyncfn_box; -use forcebot_rs_v2::Command; -use forcebot_rs_v2::Module; -use twitch_irc::message::ServerMessage; - #[tokio::main] pub async fn main() { @@ -32,31 +25,52 @@ pub async fn main() { /* Create the bot using env */ let mut bot = Bot::new(); - /* 1. Create a new module */ - let mut custom_mod = Module::new("test".to_string(), "".to_string()); - - /* 2. Create a new cmd */ - let mut cmd = Command::new("test".to_string(),"".to_string()); - - 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, "test return".to_string()).await; - } - Result::Err("Not Valid message type".to_string()) - } - - cmd.set_exec_fn(asyncfn_box(execbody)); - cmd.set_admin_only(false); - cmd.set_min_badge("moderator".to_string()); - - /* 3. Load the cmd into a new module */ - custom_mod.load_command(cmd); - - /* 4. Load the module into the bot */ - bot.load_module(custom_mod); + /* load the Module */ + bot.load_module(custom_mod::new()); /* Run the bot */ bot.run().await; } + + +pub mod custom_mod { + use std::sync::Arc; + + use forcebot_rs_v2::{asyncfn_box, Badge, Bot, Command, Module}; + use twitch_irc::message::ServerMessage; + + + /// Module with a loaded command + pub fn new() -> Module { + /* 1. Create a new module */ + let mut custom_mod = Module::new("test".to_string(), "".to_string()); + + /* 2. Load the cmd into a new module */ + custom_mod.load_command(cmd_test()); + + custom_mod + + } + + pub fn cmd_test() -> Command { + /* 1. Create a new cmd */ + let mut cmd = Command::new("test".to_string(),"".to_string()); + + /* 2. Define exec callback */ + 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, "test return".to_string()).await; + } + Result::Err("Not Valid message type".to_string()) + } + + /* 3. Set Command flags */ + cmd.set_exec_fn(asyncfn_box(execbody)); + cmd.set_admin_only(false); + cmd.set_min_badge(Badge::Moderator); + + cmd + } +} \ No newline at end of file diff --git a/src/botcore/bot_objects.rs b/src/botcore/bot_objects.rs index da787d3..9fb6985 100644 --- a/src/botcore/bot_objects.rs +++ b/src/botcore/bot_objects.rs @@ -11,6 +11,16 @@ use twitch_irc::message::ServerMessage; use super::bot::Bot; + +/// chat badge +#[derive(Clone)] +pub enum Badge { + Moderator, + Broadcaster, + Vip +} + + pub type ExecBody = Box< dyn Fn(Arc<Bot>,ServerMessage) -> Pin<Box<dyn Future<Output = Result<String,String>> + Send>> + Send + Sync, >; @@ -32,7 +42,7 @@ pub mod built_in_objects { use twitch_irc::message::ServerMessage; - use crate::{asyncfn_box, Bot, Command}; + use crate::{asyncfn_box, Badge, Bot, Command}; /// create a vector of command build in objects @@ -71,7 +81,7 @@ pub mod built_in_objects { cmd.set_admin_only(false); /* 5. optionally, set min badge*/ - cmd.set_min_badge("moderator".to_string()); + cmd.set_min_badge(Badge::Moderator); cmd } @@ -101,7 +111,7 @@ pub mod built_in_objects { cmd.set_admin_only(false); /* 5. optionally, set min badge*/ - cmd.set_min_badge("moderator".to_string()); + cmd.set_min_badge(Badge::Moderator); cmd } diff --git a/src/botcore/bot_objects/command.rs b/src/botcore/bot_objects/command.rs index d904b06..574a22e 100644 --- a/src/botcore/bot_objects/command.rs +++ b/src/botcore/bot_objects/command.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use twitch_irc::message::{PrivmsgMessage, ServerMessage}; -use crate::{asyncfn_box, botcore::bot::Bot}; +use crate::{asyncfn_box, botcore::bot::Bot, Badge}; use super::ExecBody; @@ -22,7 +22,7 @@ pub struct Command { command : String, exec_fn : Arc<ExecBody>, - min_badge : String, + min_badge : Badge, admin_only : bool, prefix : String, custom_cond_fn : fn(Arc<Bot>,PrivmsgMessage) -> bool, @@ -47,7 +47,7 @@ impl Command command , prefix , exec_fn : Arc::new(asyncfn_box(execbody)), - min_badge : "vip".to_string(), + min_badge : Badge::Vip, admin_only : true, custom_cond_fn : |_:Arc<Bot>,_:PrivmsgMessage| true, } @@ -87,24 +87,23 @@ impl Command fn caller_badge_ok(cmd:&Command,_bot:Arc<Bot>,message:PrivmsgMessage) -> bool { for badge in message.badges { - match cmd.min_badge.as_str() { - "broadcaster" => { - if badge.name == cmd.min_badge { return true } + match cmd.min_badge { + Badge::Broadcaster => { + if badge.name == "broadcaster" { return true } else { return false } }, - "moderator" => { + Badge::Moderator => { match badge.name.as_str() { "moderator" | "broadcaster" => return true, _ => (), } }, - "vip" => { + Badge::Vip => { match badge.name.as_str() { "vip" | "moderator" | "broadcaster" => return true, _ => (), } }, - _ => return false, } } @@ -151,7 +150,8 @@ impl Command /// sets min_badge to run the cmd - pub fn set_min_badge(&mut self,min_badge:String) { + // pub fn set_min_badge(&mut self,min_badge:String) { + pub fn set_min_badge(&mut self,min_badge:Badge) { self.min_badge = min_badge } diff --git a/src/lib.rs b/src/lib.rs index f99d4b3..9ecb36f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,22 +22,20 @@ //! //! ``` //! -//! ## Module with Custom Command //! -//! A `Module` is a group of bot objects (eg `Command`) that elevated users can manage. +//! ## Customize with Modules //! -//! Bot objects are recommended to be loaded through a `Module` +//! A `Module` is a group of bot objects (eg `Command`) that elevated users can manage through built in `disable` and `enable` commands +//! +//! Create a custom `Module` by : +//! +//! 1. Defining Functions that create the Custom Bot Objects (eg `Command`) +//! +//! 2. Define a function that creates a `Module` with the Custom Bot Objects loaded //! //! //! ```rust -//! use std::sync::Arc; -//! //! use forcebot_rs_v2::Bot; -//! use forcebot_rs_v2::asyncfn_box; -//! use forcebot_rs_v2::Command; -//! use forcebot_rs_v2::Module; -//! use twitch_irc::message::ServerMessage; -//! //! //! #[tokio::main] //! pub async fn main() { @@ -45,35 +43,56 @@ //! /* Create the bot using env */ //! let mut bot = Bot::new(); //! -//! /* 1. Create a new module */ -//! let mut custom_mod = Module::new("test".to_string(), "".to_string()); -//! -//! /* 2. Create a new cmd */ -//! let mut cmd = Command::new("test".to_string(),"".to_string()); -//! -//! 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, "test return".to_string()).await; -//! } -//! Result::Err("Not Valid message type".to_string()) -//! } -//! -//! cmd.set_exec_fn(asyncfn_box(execbody)); -//! cmd.set_admin_only(false); -//! cmd.set_min_badge("moderator".to_string()); -//! -//! /* 3. Load the cmd into a new module */ -//! custom_mod.load_command(cmd); -//! -//! /* 4. Load the module into the bot */ -//! bot.load_module(custom_mod); +//! /* load the Module */ +//! bot.load_module(custom_mod::new()); //! //! /* Run the bot */ //! bot.run().await; //! //! } //! +//! +//! pub mod custom_mod { +//! use std::sync::Arc; +//! +//! use forcebot_rs_v2::{asyncfn_box, Badge, Bot, Command, Module}; +//! use twitch_irc::message::ServerMessage; +//! +//! +//! /// Module with a loaded command +//! pub fn new() -> Module { +//! /* 1. Create a new module */ +//! let mut custom_mod = Module::new("test".to_string(), "".to_string()); +//! +//! /* 2. Load the cmd into a new module */ +//! custom_mod.load_command(cmd_test()); +//! +//! custom_mod +//! +//! } +//! +//! pub fn cmd_test() -> Command { +//! /* 1. Create a new cmd */ +//! let mut cmd = Command::new("test".to_string(),"".to_string()); +//! +//! /* 2. Define exec callback */ +//! 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, "test return".to_string()).await; +//! } +//! Result::Err("Not Valid message type".to_string()) +//! } +//! +//! /* 3. Set Command flags */ +//! cmd.set_exec_fn(asyncfn_box(execbody)); +//! cmd.set_admin_only(false); +//! cmd.set_min_badge(Badge::Moderator); +//! +//! cmd +//! } +//! } +//! //! ``` //! //! ## Simple Debug Listener @@ -182,4 +201,6 @@ pub use crate::botcore::bot::Bot; pub use crate::botcore::bot_objects::asyncfn_box; pub use crate::botcore::bot_objects::listener::Listener; pub use crate::botcore::bot_objects::command::Command; -pub use crate::botcore::modules::Module; \ No newline at end of file +pub use crate::botcore::modules::Module; +pub use crate::botcore::bot_objects::Badge; +