Customizable Rust Twitch Bot
Find a file
2025-01-28 11:43:57 -05:00
src fun_bot init 2025-01-28 11:43:57 -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 fun_bot init 2025-01-28 11:43:57 -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 Empty Bot

Run a simple bot that logs into chat based on env

cargo run --bin simple_bot

Fun Bot

Run a forcebot with fun catered customizations

cargo run --bin fun_bot

Simple Bot with Example Custom Listener

Run a bot with some custom listeners

cargo run --bin simple_bot_listener

Bot with Example Custom Command

Run a bot with some custom listeners

cargo run --bin bot_cmd_example

Example Code

Simple Bot

Uses Env defined variables to create and run the bot

use forcebot_rs_v2::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::Bot;
use forcebot_rs_v2::asyncfn_box;
use forcebot_rs_v2::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;

}

Bot with Custom command

use std::sync::Arc;

use forcebot_rs_v2::Bot;
use forcebot_rs_v2::asyncfn_box;
use forcebot_rs_v2::Command;
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 cmd */
    let mut cmd = Command::new("tester".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 {
            match bot.client.say_in_reply_to(&msg, String::from("cmd tested")).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()) 
    }

    /* 3. Set and Store the execution body using `async_box()`  */
    cmd.set_exec_fn(asyncfn_box(execbody));

    /* 4. Load the cmd into the bot */
    bot.load_command(cmd);

    /* 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