[][src]Crate twitchchat

twitchchat

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.

a simple example

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, token
let mut 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 writer
let writer = client.writer();

// for each event from the client (this &mut is only needed if you want to use `wait_for_close`)
for event in &mut client {
    match event {
        // when we're connected
        Event::IrcReady(..) => {
            // join a channel
            writer.join("museun").unwrap();
        }
        // when we get a priv msg
        Event::Message(Message::PrivMsg(msg)) => {
            // print out the sender : messsage
            println!("{}: {}", msg.user(), msg.message());
        }
        // when we get a join msg
        Event::Message(Message::Join(msg)) => {
            // print out the user and the channel that was joined
            println!("*** {} joined {}", msg.user(), msg.channel())
        }
        // when we get an error
        Event::Error(err) => {
            // print it out
            eprintln!("error: {}", err);
            // and exit the loop
            break;
        }
        // not used here
        Event::TwitchReady(..) => {
            // this only happens when you're using Capability::Tags, Capability::Commands and a non-anonymous login
        }
        // make the compiler happy
        _ => unreachable!(),
    }
}

// wait for the client to close
// this isn't needed, but if you want to synchronize something to it
// this is how you would do it
client.wait_for_close();

with custom capabilities

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 config
let 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,
    }
}

by constructing the client manually with your own Read/Write types

use std::net::TcpStream;
use twitchchat::commands;
use twitchchat::*;

// or anything that implements std::io::Read + Send + Sync and std::io::Write + Send + Sync
let (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,
    }
}

Modules

commands

An assortment of Twitch commands

conversion

Message conversion types

irc

IRC-related stuff (not really intended for use with real IRC networks)

Structs

Badge

Badges attached to a message

Channel

Simple channel wrapper.

Client

Client for interacting with Twitch's chat.

Color

Color represents a Twitch color

Emotes

Emotes are little pictograms used inline in twitch messages

LocalUser

Information gathered during the GLOBALUSERSTATE event

RGB

A 24-bit triplet for hex colors. Defaults to White (0xFF,0xFF,0xFF)

Tags

Tags are IRCv3 message tags. Twitch uses them extensively

UserConfig

Configuration used to complete the 'registration' with the irc server

UserConfigBuilder

A builder type to create a UserConfig without dumb errors (like swapping nick/token)

Writer

A thread-safe, clonable writer for the Twitch client

Enums

BadgeKind

BadgeKind are the kind of badges that are associated with messages.

Capability

Capabilities allow you to get more data from twitch

Error

An error that the Client can return

Event

An event received while reading from the client

Message

Messages created by the Client.

TwitchColor

These are the default Twitch colors

Constants

ANONYMOUS_LOGIN

An anonymous login. You won't be able to send messages, but you can join channels and read messages

TWITCH_IRC_ADDRESS

The Twitch IRC address for non-TLS connections

TWITCH_IRC_ADDRESS_TLS

The Twitch IRC address for TLS connections

Traits

IntoChannel

A trait to convert types into Channel

ToMessage

Convert an IRC-like message type into something that the Twitch commands can be parsed from

Functions

colors

A helper method that returns a const array of TwitchColor colors to RGB

connect

Simple function to connect to Twitch using a TcpStream (non-TLS)

connect_easy

The simpliest way to connect with this crate. It uses a TcpStream (non-TLS)

Type Definitions

BadgeInfo

Metadata related to the chat badges.