Client component of TwitchLib.
Tip
With the introduction of Chat on EventSub, it is recommended to upgrade your chatbots that are using Twitch IRC to use EventSub (for reading chat messages and roomstates) and Twitch API (for sending chat messages).
Version 4.0.1 contains breaking changes.
- Removed obsolete methods.
- Methods are now asynchronous. (The return value changed from
voidtoTaskand gainsAsyncsuffix) - Events are now asynchronous (return value changed from
voidtoTask) Add/RemoveChatCommandIdentifiermethods were removed, useChatCommandIdentifiersproperty instead (same applies to whisper) and usesstringinstead ofcharOnLogevent was removed (you can still useILoggerFactoryto get logs)- removed builders classes (removed
TwitchLib.Client.Models.Builders namespace) - changed public fields to properties
- rewritten all models in
TwitchLib.Client.Models- some props/classes can be slightly renamed
- some properties (
IsModerator,IsSubscriber,HasTurbo,IsVip,IsPartner,IsStaff) moved to theUserDetailsproperty
Step 1: Create a new Console project
Step 2: Install the TwitchLib.Client NuGet package. (See above on how to do that)
Step 2.5: (Optional) Install the Microsoft.Extensions.Logging.Console NuGet package (or some other compatible logging provider) to see logs.
Step 3: Add the following code to your Program.cs file:
using Microsoft.Extensions.Logging;
using TwitchLib.Client;
using TwitchLib.Client.Events;
using TwitchLib.Client.Models;
// Optional logger
var loggerFactory = LoggerFactory.Create(c => c
.AddConsole()
// .SetMinimumLevel(LogLevel.Trace) // uncomment to view raw messages received from twitch
);
var credentials = new ConnectionCredentials(); // anonymous user, add Username and OAuth token to get the ability to send messages
var client = new TwitchClient(loggerFactory: loggerFactory);
client.Initialize(credentials);
client.OnConnected += Client_OnConnected;
client.OnJoinedChannel += Client_OnJoinedChannel;
client.OnMessageReceived += Client_OnMessageReceived;
await client.ConnectAsync();
await Task.Delay(Timeout.Infinite);
async Task Client_OnConnected(object? sender, OnConnectedEventArgs e)
{
await client.JoinChannelAsync("channel_name"); // replace with the channel you want to join
}
async Task Client_OnJoinedChannel(object? sender, OnJoinedChannelArgs e)
{
Console.WriteLine($"Connected to {e.Channel}");
await client.SendMessageAsync(e.Channel, "Hey guys! I am a bot connected via TwitchLib!");
}
async Task Client_OnMessageReceived(object? sender, OnMessageReceivedArgs e)
{
Console.WriteLine($"{e.ChatMessage.Username}#{e.ChatMessage.Channel}: {e.ChatMessage.Message}");
}Step 4: Change the channel_name in the Client_OnConnected method to the name of the channel you want to join and run the application.
More complete examples can be found in the TwitchLib.Client.Example