Badges & Doc adj

This commit is contained in:
modulatingforce 2025-01-29 07:52:41 -05:00
parent 92b6cd3f94
commit 33dcf4c4e7
7 changed files with 178 additions and 114 deletions

View file

@ -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 ```rust
use std::sync::Arc;
use forcebot_rs_v2::Bot; 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] #[tokio::main]
pub async fn main() { pub async fn main() {
@ -121,35 +118,56 @@ pub async fn main() {
/* Create the bot using env */ /* Create the bot using env */
let mut bot = Bot::new(); let mut bot = Bot::new();
/* 1. Create a new module */ /* load the Module */
let mut custom_mod = Module::new("test".to_string(), "".to_string()); bot.load_module(custom_mod::new());
/* 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);
/* Run the bot */ /* Run the bot */
bot.run().await; 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 ## 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 Example listener listens for a moderator badge and reply in chat

View file

@ -34,7 +34,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, Bot, Command, Module}; use forcebot_rs_v2::{asyncfn_box, 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
@ -63,7 +63,7 @@ pub mod funbot_objs {
cmd.set_exec_fn(asyncfn_box(execbody)); cmd.set_exec_fn(asyncfn_box(execbody));
cmd.set_admin_only(false); cmd.set_admin_only(false);
cmd.set_min_badge("vip".to_string()); cmd.set_min_badge(Badge::Vip);
cmd cmd
} }

View file

@ -15,6 +15,7 @@
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::asyncfn_box;
use forcebot_rs_v2::Command; use forcebot_rs_v2::Command;
@ -46,7 +47,7 @@ pub async fn main() {
cmd.set_admin_only(false); cmd.set_admin_only(false);
/* 5. optionally, set min badge*/ /* 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 */ /* 6. Load the cmd into the bot */
bot.load_command(cmd); bot.load_command(cmd);

View file

@ -17,14 +17,7 @@
//! - Get a Bot Chat Token here - <https://twitchtokengenerator.com> //! - Get a Bot Chat Token here - <https://twitchtokengenerator.com>
//! - More Info - <https://dev.twitch.tv/docs/authentication> //! - More Info - <https://dev.twitch.tv/docs/authentication>
use std::sync::Arc;
use forcebot_rs_v2::Bot; 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] #[tokio::main]
pub async fn main() { pub async fn main() {
@ -32,31 +25,52 @@ pub async fn main() {
/* Create the bot using env */ /* Create the bot using env */
let mut bot = Bot::new(); let mut bot = Bot::new();
/* 1. Create a new module */ /* load the Module */
let mut custom_mod = Module::new("test".to_string(), "".to_string()); bot.load_module(custom_mod::new());
/* 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);
/* Run the bot */ /* Run the bot */
bot.run().await; 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
}
}

View file

@ -11,6 +11,16 @@ use twitch_irc::message::ServerMessage;
use super::bot::Bot; use super::bot::Bot;
/// chat badge
#[derive(Clone)]
pub enum Badge {
Moderator,
Broadcaster,
Vip
}
pub type ExecBody = Box< 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,
>; >;
@ -32,7 +42,7 @@ pub mod built_in_objects {
use twitch_irc::message::ServerMessage; 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 /// create a vector of command build in objects
@ -71,7 +81,7 @@ pub mod built_in_objects {
cmd.set_admin_only(false); cmd.set_admin_only(false);
/* 5. optionally, set min badge*/ /* 5. optionally, set min badge*/
cmd.set_min_badge("moderator".to_string()); cmd.set_min_badge(Badge::Moderator);
cmd cmd
} }
@ -101,7 +111,7 @@ pub mod built_in_objects {
cmd.set_admin_only(false); cmd.set_admin_only(false);
/* 5. optionally, set min badge*/ /* 5. optionally, set min badge*/
cmd.set_min_badge("moderator".to_string()); cmd.set_min_badge(Badge::Moderator);
cmd cmd
} }

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use twitch_irc::message::{PrivmsgMessage, ServerMessage}; use twitch_irc::message::{PrivmsgMessage, ServerMessage};
use crate::{asyncfn_box, botcore::bot::Bot}; use crate::{asyncfn_box, botcore::bot::Bot, Badge};
use super::ExecBody; use super::ExecBody;
@ -22,7 +22,7 @@ pub struct Command
{ {
command : String, command : String,
exec_fn : Arc<ExecBody>, exec_fn : Arc<ExecBody>,
min_badge : String, min_badge : Badge,
admin_only : bool, admin_only : bool,
prefix : String, prefix : String,
custom_cond_fn : fn(Arc<Bot>,PrivmsgMessage) -> bool, custom_cond_fn : fn(Arc<Bot>,PrivmsgMessage) -> bool,
@ -47,7 +47,7 @@ impl Command
command , command ,
prefix , prefix ,
exec_fn : Arc::new(asyncfn_box(execbody)), exec_fn : Arc::new(asyncfn_box(execbody)),
min_badge : "vip".to_string(), min_badge : Badge::Vip,
admin_only : true, admin_only : true,
custom_cond_fn : |_:Arc<Bot>,_:PrivmsgMessage| 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 { fn caller_badge_ok(cmd:&Command,_bot:Arc<Bot>,message:PrivmsgMessage) -> bool {
for badge in message.badges { for badge in message.badges {
match cmd.min_badge.as_str() { match cmd.min_badge {
"broadcaster" => { Badge::Broadcaster => {
if badge.name == cmd.min_badge { return true } if badge.name == "broadcaster" { return true }
else { return false } else { return false }
}, },
"moderator" => { Badge::Moderator => {
match badge.name.as_str() { match badge.name.as_str() {
"moderator" | "broadcaster" => return true, "moderator" | "broadcaster" => return true,
_ => (), _ => (),
} }
}, },
"vip" => { Badge::Vip => {
match badge.name.as_str() { match badge.name.as_str() {
"vip" | "moderator" | "broadcaster" => return true, "vip" | "moderator" | "broadcaster" => return true,
_ => (), _ => (),
} }
}, },
_ => return false,
} }
} }
@ -151,7 +150,8 @@ impl Command
/// sets min_badge to run the cmd /// 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 self.min_badge = min_badge
} }

View file

@ -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 //! ```rust
//! use std::sync::Arc;
//!
//! use forcebot_rs_v2::Bot; //! 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] //! #[tokio::main]
//! pub async fn main() { //! pub async fn main() {
@ -45,35 +43,56 @@
//! /* Create the bot using env */ //! /* Create the bot using env */
//! let mut bot = Bot::new(); //! let mut bot = Bot::new();
//! //!
//! /* 1. Create a new module */ //! /* load the Module */
//! let mut custom_mod = Module::new("test".to_string(), "".to_string()); //! bot.load_module(custom_mod::new());
//!
//! /* 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);
//! //!
//! /* Run the bot */ //! /* Run the bot */
//! bot.run().await; //! 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 //! ## 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::asyncfn_box;
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;
pub use crate::botcore::bot_objects::Badge;