TwitchLib  1.6.0
.NET Twitch Chat, Whisper, API and PubSub Library.
TwitchLib Documentation

About

TwitchLib is a powerful C# library that allows for interaction with various Twitch services. Currently supported services are: chat and whisper, API's (v3, v5, helix, undocumented, and third party), and PubSub event system. Below are the descriptions of the core components that make up TwitchLib.

  • TwitchClient: Handles chat and whisper Twitch services. Complete with a suite of events that fire for virtually every piece of data received from Twitch. Helper methods also exist for replying to whispers or fetching moderator lists.
  • TwitchAPI: Complete coverage of v3, v5, and Helix endpoints. The API is now a singleton class. This class allows fetching all publically accessable data as well as modify Twitch services like profiles and streams.
  • TwitchPubSub: Supports all documented Twitch PubSub topics as well as a few undocumented ones.

Features

  • TwitchClient:
    • Send formatted or raw messages to Twitch
    • Chat and Whisper command detection and parsing
    • Helper methods
      • Timeout, ban, unban users
      • Change chat color and clear chat
      • Invoke stream commercials and hosts
      • Emote only, follower only, subscriber only, and slow mode
      • Reply-to whisper support
    • Handles chat and whisper events:
      • Connected and Joined channel
      • Channel and User state changed
      • Message received/sent
      • Whisper received/sent
      • User joined/left
      • Moderator joined/left
      • New subscriptions and resubscriptions
      • Hosting and raid detection
      • Chat clear, user timeouts, user bans
  • Services:
    • FollowerService: Service for detection of new followers in somewhat real time.
    • LiveStreamMonitor: Service for detecting when a channel goes online/offline in somewhat real time.
    • MessageThrottler: Service to throttle chat messages to abide by Twitch use requirements.
  • TwitchAPI:
    • Supported Twitch API endpoitns:v3, v5, Helix
    • Supported API sections:
      • Badges, Bits, Blocks
      • ChannelFeeds, Channels, Chat, Clips, Collections, Communities,
      • Follows
      • Games
      • Ingests
      • Root
      • Search, Streams, Subscriptions
      • Teams
      • ThirdParty
      • Undocumented
        • ClipChat
        • TwitchPrimeOffers
        • ChannelHosts
        • ChatProperties
        • ChannelPanels
        • CSMaps
        • CSStreams
        • RecentMessages
        • Chatters
        • RecentChannelEvents
        • ChatUser
        • UsernameAvailable
      • User
      • Videos
  • TwitchPubSub:
    • Supported topics:
      • ChatModeratorActions
      • BitsEvents
      • VideoPlayback
      • Whispers
      • Subscriptions

Documentation

Doxygen

For complete library documentation, view the doxygen docs here.

Implementing

Below are basic examples of how to utilize each of the core components of TwitchLib. These are C# examples, but this library can also be used in Visual Basic. #### TwitchClient

using TwitchLib;
using TwitchLib.Models.Client;
using TwitchLib.Events.Client;
TwitchClient client;
ConnectionCredentials credentials = new ConnectionCredentials("twitch_username", "access_token");
client = new TwitchClient(credentials, "channel_to_join");
client.OnJoinedChannel += onJoinedChannel;
client.OnMessageReceived += onMessageReceived;
client.OnWhisperReceived += onWhisperReceived;
client.OnNewSubscriber += onNewSubscriber;
client.Connect();
private void onJoinedChannel(object sender, OnJoinedChannelArgs e) {
client.SendMessage("Hey guys! I am a bot connected via TwitchLib!");
}
private void onMessageReceived(object sender, OnMessageReceivedArgs e) {
if(e.ChatMessage.Message.Contains("badword"))
client.TimeoutUser(e.ChatMessage.Username, TimeSpan.FromMinutes(30), "Bad word! 30 minute timeout!");
}
private void onCommandReceived(object sender, OnWhisperCommandReceivedArgs e) {
if(e.Command == "help")
client.SendMessage($"Hi there {e.WhisperMessage.Username}! You can view all commands using !command");
}
private void onWhisperReceived(object sender, OnWhisperReceivedArgs e) {
if(e.WhisperMessage.Username == "my_friend")
client.SendWhisper(e.WhisperMessage.Username, "Hey! Whispers are so cool!!");
}
private void onNewSubscriber(object sender, OnNewSubscriberArgs e) {
if(e.Subscriber.IsTwitchPrime)
client.SendMessage($"Welcome {e.Subscriber.DisplayName} to the substers! You just earned 500 points! So kind of you to use your Twitch Prime on this channel!");
else
client.SendMessage($"Welcome {e.Subscriber.DisplayName} to the substers! You just earned 500 points!");
}

For a complete list of TwitchClient events and calls, click here

TwitchAPI

Note: TwitchAPI is now a singleton class that needs to be instantiated with optional clientid and auth token. Please know that failure to provide at least a client id, and sometimes an access token will result in exceptions. The v3 subclass operates almost entirely on Twitch usernames. v5 and Helix operate almost entirely on Twitch user id's. There are methods in all Twitch api versions to get corresponding usernames/userids.

using TwitchLib;
using TwitchLib.Models.API;
private static TwitchLib.TwitchAPI api;
api = new TwitchLib.TwitchAPI("client_id", "access_token");
var subscription = await api.Channels.v5.CheckChannelSubscriptionByUserAsync("channel_id", "user_id");
var allSubscriptions = await api.Channels.v5.GetAllSubscribersAsync("channel_id");
var userFollows = await api.Users.v5.GetUserFollowsAsync("user_id");
var channelFollowers = await api.Channels.v5.GetChannelFollowersAsync("channel_id");
bool userFollowsChannel = await api.Users.v5.FollowChannelAsync("user_id", "channel_id");
bool isStreaming = await api.Streams.v5.BroadcasterOnlineAsync("channel_id");]
await api.Channels.v5.UpdateChannelAsync("channel_id", "New stream title", "Stronghold Crusader");

For a complete list of TwitchAPI calls, click here #### TwitchPubSub

using TwitchLib;
TwitchPubSub pubsub = new TwitchPubSub();
pubsub.OnPubSubServiceConnected += onPubSubConnected;
pubsub.OnListenResponse += onPubSubResponse;
pubsub.OnBitsReceived += onPubSubBitsReceived;
pubsub.Connect();
private void onPubSubConnected(object sender, object e) {
// MY ACCOUNT ID, MY OAUTH
pubsub.ListenToWhispers(0, "oauth_token");
}
private void onPubSubResponse(object sender, OnListenResponseArgs e) {
if (e.Successful)
MessageBox.Show($"Successfully verified listening to topic: {e.Topic}");
else
MessageBox.Show($"Failed to listen! Error: {e.Response.Error}");
}
private void onPubSubBitsReceived() {
MessageBox.Show($"Just received {e.BitsUsed} bits from {e.Username}. That brings their total to {e.TotalBitsUsed} bits!");
}

For a complete list of TwitchPubSub functionality, click here

Using TwitchLib with Unity Guide

Björn has kindly created a guide for using TwitchLib with Unity. To view the guide, click here..

Examples, Applications, Community Work, and Projects

Installation

NuGet

To install this library via NuGet via NuGet console, use:

Install-Package TwitchLib

and via Package Manager, simply search:

TwitchLib

You are also more than welcome to clone/fork this repo and build the library yourself!

Dependencies

Contributors

License

This project is available under the MIT license. See the LICENSE file for more info.

:)