This is an updated version / rewrite of my Twitch library from the .NET Framework to .NET Core with the purpose to move over my other Twitch related tools to the latest and greatest. At the moment Twitch chat functionality is supported and support for the Twitch API (Kraken v5 & Helix) will be added as well once the chat part is fully done and tested.
Note that this is a work in progress so if you encounter any issues feel free to let me know so I can fix it.
That said, there aren’t really any new fancy features compared to the previous .NET Framework version so I’ll just list what it does to keep things up to speed and add some examples how to get it running.
Features
– basic lightweight Twitch chat compatible client library
– send / receive chat messages & whispers
– supports various Twitch chat commands such as mod, ban (duration), vip, host, raid etc
– event based notifications, for example to receive chat messages just add a handler for that -> _client.OnPrivMsg += ClientOnPrivMsg;
– automatically reconnect active clients on connection loss (default attempts is 10)
– etc
Note
The compiled debug version (BlushyFace.Core.Tests.exe) can connect to Twitch chat as an anonymous user with all events enabled and supports /join and /part commands.
Examples
Create a new Twitch client with command / membership / tags capabilities.
_client = new Client { RegisterCapRequestCommands = true, RegisterCapRequestMembership = true, RegisterCapRequestTags = true };
Add event handlers.
_client.OnPrivMsg += ClientOnPrivMsg; _client.OnNotice += ClientOnNotice; _client.OnClearChat += ClientOnClearChat; _client.OnClearMsg += ClientOnClearMsg; _client.OnGlobalUserState += ClientOnGlobalUserState; _client.OnHostTarget += ClientOnHostTarget; _client.OnRoomState += ClientOnRoomState; _client.OnUserNotice += ClientOnUserNotice; _client.OnUserState += ClientOnUserState; _client.OnMode += ClientOnMode; _client.OnNames += ClientOnNames; _client.OnCap += ClientOnCap; _client.OnNumeric += ClientOnNumeric; _client.OnJoin += ClientOnJoin; _client.OnPart += ClientOnPart;
Add custom event handlers.
_client.OnRitual += ClientOnRitual; // triggers when a user does the ritual (welcomes new user) _client.OnBits += ClientOnBits; // triggers when a user cheers _client.OnSubscription += ClientOnSubscription; // triggers when a user subs -> sub/resub/subgift/anonsubgift/submysterygift/giftpaidupgrade/rewardgift etc _client.OnRaid += ClientOnRaid; // triggers when channel gets raided
Connect to the Twitch chat with username / token.
Note: the Twitch lib currently does not have any authentication methods implemented so you need to generate your own token to be able to send messages to the Twitch chat. Once updated you can connect to Twitch chat by supplying a token on connect -> _client.Connect(token); or let the lib create one automatically after being authorized to do so.
_client.Connect(userName, token);
Connect to the Twitch chat anonymously (note that sending messages will not work when connecting anonymously).
_client2.Connect();
Send a message to Twitch chat
_client.SendMessage("blushyface", "Hello!");
Usage of various commands.
_client.Commands.Join("blushyface"); // joins a channel _client.Commands.Mods("blushyface"); // get list of channel mods for [channel] _client.Commands.Host("blushyface","blushyfacebot"); // hosts a channel to another channel _client.Commands.UnHost("blushyface"); // unhosts a channel that is hosting another channel _client.Commands.Color("#..."); // name or hex color _client.Commands.TimeOut("blushyface", "blushyface", 60); // time out a user in a specific channel for 1 minute -> [channelName][userName][optional duration] _client.Commands.ClearTimeOut("blushyface", "blushyface"); // clear a user time out in a specific channel [channelName][userName] _client.Commands.Whisper("blushyface","Hello is a whisper."); // whisper someone
When the client is running successfully you should see various messages for example this is cohhcarnage’s chat (with permission) with all the events enabled.
Subscription events
If you want to know how many people (re)subscribe etc to cohhcarnage you can add an event handler-> _client.OnSubscription += ClientOnSubscription; and the Twitch client would look like this:
private static void ClientOnSubscription(object sender, Subscription e) { Utilities.WriteDebugConsole($"[SUBSCRIPTION] {e.Channel} {e.SystemMessage} {e.UserMessage}"); if ( (e.SubPlan == Enums.SubscriberPlan.Prime || e.SubPlan == Enums.SubscriberPlan.Tier2) && e.MonthsSubStreak >= 6 ) { // do something.. } }
Channel host events
When a channel hosts another channel you get a [HostTarget] event.
This is an example how to listen for host events and then join the hosted channel chat -> add an event handler -> _client.OnHostTarget += ClientOnHostTarget;
private static void ClientOnHostTarget(object sender, HostTarget e) { Utilities.WriteDebugConsole($"[HOSTTARGET] {e.Channel} hosting: {e.ChannelBeingHosted}"); if (e.ChannelBeingHosted.Equals("")) return; // the channel that is getting hosted shouldn't be empty but check it either way. _client.Commands.Join(e.ChannelBeingHosted); }
This should cover the basics how to use my Twitch lib for .NET Core, if you have any questions feel free to make a comment below.