twitchchat
This crate provides a way to interace with Twitch's chat.
Along with the messages as Rust types, it provides methods for sending messages.
Demonstration
See examples/demo.rs for a larger example
A client
use twitchchat::{events, Client, Secure};
use tokio::stream::StreamExt as _;
#[tokio::main]
async fn main() {
let (nick, pass) = twitchchat::ANONYMOUS_LOGIN;
let (read, write) = twitchchat::connect_easy(&nick, &pass, Secure::UseTls)
.await
.unwrap();
let client = Client::new();
let bot = client.clone();
tokio::task::spawn(async move {
let mut privmsgs = bot.dispatcher().await.subscribe::<events::Privmsg>();
while let Some(msg) = privmsgs.next().await {
eprintln!("[{}] {}: {}", msg.channel, msg.name, msg.data);
}
});
client.writer().join("#museun").await.unwrap();
use twitchchat::client::Status;
match client.run(read, write).await {
Ok(Status::Eof) => {}
Ok(Status::Canceled) => {}
Err(err) => eprintln!("error!: {}", err),
};
}
Parsing messages
use twitchchat::messages::*;
use twitchchat::{AsOwned as _, Parse as _};
fn main() {
let input = "@badge-info=subscriber/8;color=#59517B;tmi-sent-ts=1580932171144;user-type= :tmi.twitch.tv USERNOTICE #justinfan1234\r\n";
for msg in twitchchat::decode(&input).flatten() {
let user_notice = UserNotice::parse(&msg).unwrap();
let owned: UserNotice<'static> = user_notice.as_owned();
assert_eq!(user_notice, owned);
match AllCommands::parse(&msg).unwrap() {
AllCommands::UserNotice(notice) => {
assert_eq!(user_notice, notice);
}
_ => {}
}
assert_eq!(
user_notice.color().unwrap(),
"#59517B".parse::<twitchchat::color::Color>().unwrap()
);
let ts = user_notice.tags.get("tmi-sent-ts").unwrap();
assert_eq!(ts, "1580932171144");
let ts = user_notice
.tags
.get_parsed::<_, u64>("tmi-sent-ts")
.unwrap();
assert_eq!(ts, 1580932171144);
}
let input =
":tmi.twitch.tv PING 1234567\r\n:museun!museun@museun.tmi.twitch.tv JOIN #museun\r\n";
let (d, left) = twitchchat::decode_one(input).unwrap();
assert!(d > 0);
assert_eq!(left.command, "PING");
let (i, right) = twitchchat::decode_one(&input[d..]).unwrap();
assert_eq!(i, 0);
assert_eq!(right.command, "JOIN");
}
License
twitchchat
is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.