twitchchat
interface to the irc portion of twitch's chat
you provide the std::io::Read
and the std::io::Write
...and this provides all of the types for Twitch chat message.
see the docs for more info
optional features:
feature |
description |
--- |
serde |
enables serde derives |
allowing you to turn stuff to json, and load it from json |
hashbrown |
enables hashbrown types |
faster hashmaps, lower memory allocations |
serde_hashbrown |
enables serde and hashbrown+serde |
basically serde+hashbrown. use this if you want serde and hashbrown |
parking_lot |
enables faster mutexes |
--- |
--- |
--- |
|
all |
enables all of the above |
--- |
they are disabled by default.
a demo of it:
fn main() {
use std::net::TcpStream;
use twitchchat::commands::PrivMsg;
use twitchchat::{Client, UserConfig};
let userconfig = UserConfig::builder()
.nick(env!("MY_TWITCH_NAME"))
.token(env!("MY_TWITCH_PASS"))
.build()
.expect("semi-valid config");
let read = TcpStream::connect(twitchchat::TWITCH_IRC_ADDRESS).expect("connect");
let write = read.try_clone().expect("must be able to clone");
let mut client = Client::new(read, write);
let mut kappa = client.clone();
let _tok = client.on(move |msg: PrivMsg| {
const KAPPA: usize = 25;
println!("{}: {}", msg.display_name().unwrap(), msg.message());
let kappas = msg
.emotes()
.iter()
.filter(|e| e.id == KAPPA)
.map(|d| d.ranges.len())
.sum::<usize>();
if kappas >= 3 {
kappa.send(msg.channel, "Kappa").unwrap();
}
});
client.on(move |msg: PrivMsg| {
use twitchchat::BadgeKind::{Broadcaster, Subscriber};
let name = msg.display_name().unwrap_or_else(|| msg.irc_name());
let badges = msg
.badges()
.iter()
.map(|badge| badge.kind.clone())
.collect::<Vec<_>>();
match (
badges.contains(&Broadcaster),
badges.contains(&Subscriber),
msg.moderator(), ) {
(true, _, _) => println!("{} is the broadcaster", name),
(_, true, _) => println!("{} is a subscriber", name),
(_, _, true) => println!("{} is a mod", name),
(_, _, _) => {
}
};
});
client.register(userconfig).expect("register with twitch");
match client.wait_for_ready() {
Ok(user) => {
println!(
"id: {}, name: {:?}, color: {:?}",
user.user_id, user.display_name, user.color
)
}
Err(twitchchat::Error::InvalidRegistration) => {
eprintln!("invalid nick/pass");
std::process::exit(1);
}
Err(err) => panic!(err),
};
client.join("museun").unwrap();
{
let mut client = client.clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(3));
client.send("museun", "VoHiYo").unwrap();
});
}
if let Err(err) = client.run() {
eprintln!("error while running: {}", err);
std::process::exit(1);
}
}