|
1 | | -# TwitchLib.Client |
| 1 | +# TwitchLib.Client |
2 | 2 | Client component of TwitchLib. |
3 | 3 |
|
4 | | -For a general overview and example, refer to https://github.com/TwitchLib/TwitchLib/blob/master/README.md |
| 4 | +> [!TIP] |
| 5 | +> 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). |
| 6 | +
|
| 7 | +## Installation |
| 8 | + |
| 9 | +| NuGet | | [![TwitchLib.Client][1]][2] | |
| 10 | +| :--------------- | ----: | :---------------------------------------------------------------- | |
| 11 | +| Package Manager | `PM>` | `Install-Package TwitchLib.Client -Version 4.0.1` | |
| 12 | +| .NET CLI | `>` | `dotnet add package TwitchLib.Client --version 4.0.1` | |
| 13 | +| PackageReference | | `<PackageReference Include="TwitchLib.Client" Version="4.0.1" />` | |
| 14 | +| Paket CLI | `>` | `paket add TwitchLib.Client --version 4.0.1` | |
| 15 | + |
| 16 | +[1]: https://img.shields.io/nuget/v/TwitchLib.Client.svg?label=TwitchLib.Client |
| 17 | +[2]: https://www.nuget.org/packages/TwitchLib.Client |
| 18 | + |
| 19 | +## ⚠ Breaking Changes in Version 4.0.1 ⚠ |
| 20 | + |
| 21 | +Version 4.0.1 contains breaking changes. |
| 22 | +- Removed obsolete methods. |
| 23 | +- Methods are now asynchronous. (The return value changed from `void` to `Task` and gains `Async` suffix) |
| 24 | +- Events are now asynchronous (return value changed from `void` to `Task`) |
| 25 | +- `Add/RemoveChatCommandIdentifier` methods were removed, use `ChatCommandIdentifiers` property instead (same applies to whisper); |
| 26 | +- `OnLog` event was removed (you can still use `ILoggerFactory` to get logs) |
| 27 | +- removed builders classes (removed `TwitchLib.Client.Models.Builders namespace`) |
| 28 | +- changed public fields to properties |
| 29 | +- rewritten all models in `TwitchLib.Client.Models` |
| 30 | + - some props/classes can be slightly renamed |
| 31 | + - some properties (`IsModerator`, `IsSubscriber`, `HasTurbo`, `IsVip`, `IsPartner`, `IsStaff`) moved to the `UserDetails` property |
| 32 | + |
| 33 | +## Minimal Setup |
| 34 | +Step 1: Create a new Console project |
| 35 | + |
| 36 | +Step 2: Install the `TwitchLib.Client` NuGet package. (See above on how to do that) |
| 37 | + |
| 38 | +Step 2.5: (Optional) Install the [`Microsoft.Extensions.Logging.Console`](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console) NuGet package (or some other compatible logging provider) to see logs. |
| 39 | + |
| 40 | +Step 3: Add the following code to your `Program.cs` file: |
| 41 | +```cs |
| 42 | +using Microsoft.Extensions.Logging; |
| 43 | +using TwitchLib.Client; |
| 44 | +using TwitchLib.Client.Events; |
| 45 | +using TwitchLib.Client.Models; |
| 46 | + |
| 47 | +// Optional logger |
| 48 | +var loggerFactory = LoggerFactory.Create(c => c |
| 49 | + .AddConsole() |
| 50 | +// .SetMinimumLevel(LogLevel.Trace) // uncomment to view raw messages received from twitch |
| 51 | +); |
| 52 | + |
| 53 | +var credentials = new ConnectionCredentials(); // anonymous user, add Username and OAuth token to get the ability to send messages |
| 54 | +var client = new TwitchClient(loggerFactory: loggerFactory); |
| 55 | + |
| 56 | +client.Initialize(credentials); |
| 57 | +client.OnConnected += Client_OnConnected; |
| 58 | +client.OnJoinedChannel += Client_OnJoinedChannel; |
| 59 | +client.OnMessageReceived += Client_OnMessageReceived; |
| 60 | + |
| 61 | +await client.ConnectAsync(); |
| 62 | +await Task.Delay(Timeout.Infinite); |
| 63 | + |
| 64 | + |
| 65 | +async Task Client_OnConnected(object? sender, OnConnectedEventArgs e) |
| 66 | +{ |
| 67 | + await client.JoinChannelAsync("channel_name"); // replace with the channel you want to join |
| 68 | +} |
| 69 | + |
| 70 | +async Task Client_OnJoinedChannel(object? sender, OnJoinedChannelArgs e) |
| 71 | +{ |
| 72 | + Console.WriteLine($"Connected to {e.Channel}"); |
| 73 | + await client.SendMessageAsync(e.Channel, "Hey guys! I am a bot connected via TwitchLib!"); |
| 74 | +} |
| 75 | + |
| 76 | +async Task Client_OnMessageReceived(object? sender, OnMessageReceivedArgs e) |
| 77 | +{ |
| 78 | + Console.WriteLine($"{e.ChatMessage.Username}#{e.ChatMessage.Channel}: {e.ChatMessage.Message}"); |
| 79 | +} |
| 80 | +``` |
| 81 | +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. |
| 82 | + |
| 83 | +More complete examples can be found in the [TwitchLib.Client.Example](/TwitchLib.Client.Example/) |
0 commit comments