GitHub - desync0/TwitchLib: C# Twitch Chat, Whisper, API and PubSub Library. Allows for chatting, whispering, stream event subscription and channel/account modification.

Platform: iOS

About

TwitchLib is a powerful C# library that allows for interaction with various Twitch services like chat, whispers, API, 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.
  • TwitchAPI: With complete v3 and v5 endpoints, TwitchAPI is a static asynchronous class that allows for modifying of virtually all Twitch account properties and fetching of Twitch data. The class also sports undocumented endpoints and thirdparty endpoints.
  • TwitchPubSub: Covers the relatively new Twitch PubSub event system. Currently both topics Twitch supports are supported via this static class.

Features

  • TwitchClient:
    • Handles chat interactions and functionality.
    • Events for channel being hosted, chat being cleared, moderators/users entering/leaving chat, etc.
    • Reply to/send chat messages and whispers.
    • Native support for commands and customizable command identifiers (default is "!")
    • Ability to timeout/ban/unban users, change username color, clear chat, play commercials (for partnered streams), turn on emote/follower/sub only mode, and retrive list of moderators/followers.
    • Message throttling handling.
  • Services:
    • FollowerService: Service for detection of new followers.
    • LiveStreamMonitor: Service for detecting when a channel goes online/offline
  • TwitchAPI:
    • Retrieve uptime/current status of stream, posts in channel feed, etc.
    • Retrieve followed channels/check is particular user is a follower or not
    • Search games by viewcount, name, etc.
    • Update stream title/current game
    • Reset stream key, set stream delay
    • Follow/unfollow channels
    • (Partnerd streams only) Retrieve subscriber count/list
    • (Partnered streams) Run commercials
    • Create/moderate/update/search for communities
  • TwitchPubSub:
    • Chat interactions through Twitch's PubSub system

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 an asynchronous static class. Each subclass of TwitchAPI represents a part of the TwitchAPI. Each part of the TwitchAPI has a v3 and v5 static class. These classes represent Twitch's v3 and v5 API iterations. v5 ONLY uses UserID's, unless specified, and v3 ONLY uses Usernames, unless specified. If a part does not have v3 and v5, this indicates that the part only exists in one of the two versions. Look at the method arguments to determine if the method wants a userid or username.

using TwitchLib;
using TwitchLib.Models.API;

TwitchAPI.Settings.ClientId = "my-client-id";
TwitchAPI.Settings.AccessToken = "my-oauth-token";

bool isSubbed = await TwitchAPI.Channels.v5.CheckChannelSubscriptionByUser("channel-id", "user-id");

List<TwitchLib.Models.API.v5.Subscriptions.Subscription> allSubs = await TwitchAPI.Channels.v5.GetAllSubscribers("channel-id");

TwitchLib.Models.API.v5.Channels.ChannelFollowers followers = await TwitchAPI.Channels.v5.GetChannelFollowers("channel-id");

TwitchLib.Models.API.v5.Users.UserFollow follow = await TwitchAPI.Users.v5.FollowChannel("user-id", "channel-id");

bool isStreaming = await TwitchAPI.Streams.v5.BroadcasterOnline("channel-id");

TwitchLib.Models.API.v5.Channels.Channel channel = await TwitchAPI.Channels.v5.UpdateChannel("channel-id", "New status here", "Game here");

For a complete list of TwitchAPI calls, click here

TwitchPubSub

using TwitchLib;

TwitchPubSub pubsub = new TwitchPubSub();
pubsub.OnPubSubServiceConnected += onPubSubConnected;
pubsub.OnListResponse += 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

Examples, Applications, and Community Work

Installation

NuGet

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

Install-Package TwitchLib

and via Package Manager, simply search:

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.