This crate provides a way to interact with Twitch's chat
Along with the messages/commands as Rust types, it provides methods for sending messags/commands.
use twitchchat::commands;use twitchchat::*;// use an anonymous login (you should probably use your name and your chat oauth token)let(nick, token) = twitchchat::ANONYMOUS_LOGIN;// connect with this nick, tokenletmut client = twitchchat::connect_easy(nick, token).unwrap()// this is an error if// the network connection can't be opened,// the nick/pass is invalid, etc// add some filters.filter::<commands::PrivMsg>()// filter to PrivMsg commands.filter::<commands::Join>();// filter to Join commands// get a clonable, threadsafe writerlet writer = client.writer();// for each event from the client, blocking// a client.nonblocking_iter() also existsfor event in client {match event {// when we're connectedEvent::IrcReady(..) => {// join a channel
writer.join("museun").unwrap();}// when we get a priv msgEvent::Message(Message::PrivMsg(msg)) => {// print out the sender : messsageprintln!("{}: {}", msg.user(), msg.message());}// when we get a join msgEvent::Message(Message::Join(msg)) => {// print out the user and the channel that was joinedprintln!("*** {} joined {}", msg.user(), msg.channel())}// when we get an errorEvent::Error(err) => {// print it outeprintln!("error: {}", err);// and exit the loopbreak;}// not used hereEvent::TwitchReady(..) => {// this only happens when you're using Capability::Tags, Capability::Commands and a non-anonymous login}// make the compiler happy
_ => unreachable!(),}}use twitchchat::commands;use twitchchat::*;// use an anonymous login (you should probably use your name and your chat oauth token)let(nick, token) = twitchchat::ANONYMOUS_LOGIN;let config = UserConfig::builder().token(token)// your oauth token.nick(nick)// your nickname.commands()// command capabilites (see: https://dev.twitch.tv/docs/irc/commands/ ).membership()// command capabilites (see: https://dev.twitch.tv/docs/irc/membership/ ).tags()// command capabilites (see: https://dev.twitch.tv/docs/irc/tags/ ).build()// verify the settings.unwrap();// connect with the configlet client = twitchchat::connect(&config).unwrap().filter::<commands::PrivMsg>();let writer = client.writer();for event in client {match event {Event::IrcReady(..) => writer.join("museun").unwrap(),Event::Message(Message::PrivMsg(msg)) => {println!("{}: {}", msg.user(), msg.message());}Event::Error(..) => break,
_ => continue,}}use std::net::TcpStream;use twitchchat::commands;use twitchchat::*;// or anything that implements std::io::Read + Send + Sync and std::io::Write + Send + Synclet(read, write) = TcpStream::connect(twitchchat::TWITCH_IRC_ADDRESS).map(|w| (w.try_clone().unwrap(), w)).unwrap();// use an anonymous login (you should probably use your name and your chat oauth token)let(nick, token) = twitchchat::ANONYMOUS_LOGIN;let config = UserConfig::builder().token(token)// your oauth token.nick(nick)// your nickname.commands()// command capabilites (see: https://dev.twitch.tv/docs/irc/commands/ ).membership()// command capabilites (see: https://dev.twitch.tv/docs/irc/membership/ ).tags()// command capabilites (see: https://dev.twitch.tv/docs/irc/tags/ ).build()// verify the settings.unwrap();let client = Client::register(config, read, write).unwrap();let client = client.filter::<commands::PrivMsg>();let writer = client.writer();for event in client {match event {Event::IrcReady(..) => writer.join("museun").unwrap(),Event::Message(Message::PrivMsg(msg)) => {println!("{}: {}", msg.user(), msg.message());}Event::Error(..) => break,
_ => continue,}}License: 0BSD