Customizable Rust Twitch Bot
Find a file
2025-01-27 13:07:33 -05:00
src listener obj 2025-01-27 13:07:33 -05:00
.gitignore init 2025-01-25 11:45:10 -05:00
Cargo.lock init 2025-01-25 11:45:10 -05:00
Cargo.toml listener obj 2025-01-27 13:07:33 -05:00
readme.md listener obj 2025-01-27 13:07:33 -05:00

Twitch chat bot written in rust

Quick Start

Run a Simple bot with Built in functionality

  1. Generate a twitch access token

  2. Define an .env file with the following

login_name=BOTNAME
access_token=ACCESS_TOKEN
bot_channels=BOTNAME
prefix=`
bot_admins=ADMIN
  1. Build & run
cargo run

Binary Crates

Simple Bot

Run a simple bot that logs into chat based on env

cargo run --bin simple_bot

Simple Bot with Example Custom Listener

Run a bot with some custom listeners

cargo run --bin simple_bot_listener

Example Code

Simple Bot

Uses Env defined variables to create and run the bot

use forcebot_rs_v2::botcore::bot::Bot;

#[tokio::main]
pub async fn main() {

    /* 1. Create the bot using env */
    let bot = Bot::new();

    /* 2. Run the bot */
    bot.run().await;

}

Custom Bot with listener

Bot with a simple listener

Example listener listens for a moderator badge and reply in chat

use std::sync::Arc;

use forcebot_rs_v2::botcore::{bot::Bot, bot_objects::listener::asyncfn_box};
use forcebot_rs_v2::botcore::bot_objects::listener::Listener;
use twitch_irc::message::ServerMessage;


#[tokio::main]
pub async fn main() {

    /* Create the bot using env */
    let mut bot = Bot::new();

    /* 1. Create a new blank Listener */
    let mut listener = Listener::new();

    /* 2. Set a trigger condition callback  */
    listener.set_trigger_cond_fn(
        |_:Arc<Bot>,message:ServerMessage| 
            if let ServerMessage::Privmsg(msg) = message {
                for badge in msg.badges {
                    if matches!(badge, x if x.name == "moderator") {
                        return true;
                    }
                } 
                false
            } else { false }
    );

    /* 3. Define an async fn callback execution */
    async fn execbody(bot:Arc<Bot>,message:ServerMessage) -> Result<String,String> {
        if let ServerMessage::Privmsg(msg) = message {
            match bot.client.say_in_reply_to(&msg, String::from("test")).await {
                Ok(_) => return Result::Ok("Success".to_string()) ,
                Err(_) => return Result::Err("Not Valid message type".to_string()) 
            }
        }
        Result::Err("Not Valid message type".to_string()) 
    }

    /* 4. Set the execution body using `async_box()`  */
    listener.set_exec_fn(asyncfn_box(execbody));

    /* 5. Load the Listener into the bot */
    bot.load_listener(listener);

    /* Run the bot */
    bot.run().await;

}

Crate Rust Documentation

Create Crate documentation

Clean Build Documentation

cargo clean && cargo doc

Open Crate Doc

cargo doc --open