Skip to content

Commit 4f5affd

Browse files
AoshiWAoshiW
andauthored
[skip-ci] Example and readme (#272)
* Update README and Example --------- Co-authored-by: AoshiW <ondru@LAPTOP-7K2AEST8>
1 parent cf24c9e commit 4f5affd

4 files changed

Lines changed: 208 additions & 4 deletions

File tree

README.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,83 @@
1-
# TwitchLib.Client
1+
# TwitchLib.Client
22
Client component of TwitchLib.
33

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/)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Microsoft.Extensions.Logging;
2+
using TwitchLib.Client;
3+
using TwitchLib.Client.Events;
4+
using TwitchLib.Client.Models;
5+
6+
var loggerFactory = LoggerFactory.Create(c => c
7+
.AddConsole()
8+
//.SetMinimumLevel(LogLevel.Trace) // uncomment to view raw messages received from twitch
9+
);
10+
11+
var credentials = new ConnectionCredentials(); // anonymous user, add Username and OAuth token to get the ability to send messages
12+
var client = new TwitchClient(loggerFactory: loggerFactory)
13+
{
14+
ChatCommandIdentifiers = { '!', '?' }, // you can customize the command identifiers, if not set, defaults to '!'
15+
};
16+
17+
client.Initialize(credentials);
18+
client.OnConnected += Client_OnConnected;
19+
client.OnJoinedChannel += Client_OnJoinedChannel;
20+
client.OnMessageReceived += Client_OnMessageReceived;
21+
22+
client.OnChatCommandReceived += Client_OnChatCommandReceived;
23+
24+
await client.ConnectAsync();
25+
await Task.Delay(Timeout.Infinite);
26+
27+
28+
async Task Client_OnConnected(object? sender, OnConnectedEventArgs e)
29+
{
30+
await client.JoinChannelAsync("piratesoftware"); // replace with the channel you want to join
31+
}
32+
33+
async Task Client_OnJoinedChannel(object? sender, OnJoinedChannelArgs e)
34+
{
35+
Console.WriteLine($"Connected to {e.Channel}");
36+
await client.SendMessageAsync(e.Channel, "Hey guys! I am a bot connected via TwitchLib!");
37+
}
38+
39+
async Task Client_OnMessageReceived(object? sender, OnMessageReceivedArgs e)
40+
{
41+
Console.WriteLine($"{e.ChatMessage.Username}#{e.ChatMessage.Channel}: {e.ChatMessage.Message}");
42+
}
43+
44+
async Task Client_OnChatCommandReceived(object? sender, OnChatCommandReceivedArgs e)
45+
{
46+
var channel = e.ChatMessage.Channel;
47+
var command = e.Command;
48+
49+
switch (command.Name.ToLower())
50+
{
51+
case "info":
52+
await client.SendMessageAsync(channel, "Hi, I am a bot created using TwitchLib.Client.");
53+
break;
54+
case "repeat":
55+
if (string.IsNullOrWhiteSpace(command.ArgumentsAsString))
56+
return;
57+
await client.SendMessageAsync(channel, command.ArgumentsAsString);
58+
break;
59+
case "leave":
60+
if (!e.ChatMessage.IsBroadcaster)
61+
return;
62+
await client.SendMessageAsync(channel, "Goodbye! I am leaving the channel.");
63+
await client.LeaveChannelAsync(channel);
64+
break;
65+
case "help":
66+
if (command.ArgumentsAsList.Count is 0)
67+
{
68+
var helpText = "Available commands: !info, !repeat <message>, !help [command]";
69+
if (e.ChatMessage.IsBroadcaster)
70+
helpText += ", !leave";
71+
72+
await client.SendMessageAsync(channel, helpText);
73+
return;
74+
}
75+
switch (command.ArgumentsAsList[0])
76+
{
77+
case "info":
78+
await client.SendMessageAsync(channel, "The !info command provides information about the bot.");
79+
break;
80+
case "repeat":
81+
await client.SendMessageAsync(channel, "The !repeat command repeats the message you provide.");
82+
break;
83+
case "help":
84+
await client.SendMessageAsync(channel, "The !help command provides information about available commands.");
85+
break;
86+
case "leave":
87+
if (!e.ChatMessage.IsBroadcaster)
88+
return;
89+
90+
await client.SendMessageAsync(channel, "The !leave command makes the bot leave the channel.");
91+
break;
92+
default:
93+
await client.SendMessageAsync(channel, $"No help available for command: {command.ArgumentsAsList[0]}");
94+
break;
95+
}
96+
break;
97+
default:
98+
Console.WriteLine($"Unknown command: {command.Name}");
99+
break;
100+
}
101+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\TwitchLib.Client\TwitchLib.Client.csproj" />
16+
</ItemGroup>
17+
18+
</Project>

TwitchLib.Client.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2027
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36310.24
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TwitchLib.Client", "TwitchLib.Client\TwitchLib.Client.csproj", "{44B88EFA-1500-4005-AF27-A9BCB9DD79C1}"
77
EndProject
@@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{8C69E628
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwitchLib.Client.Benchmark", "TwitchLib.Client.Benchmark\TwitchLib.Client.Benchmark.csproj", "{7E666AC9-B717-4E00-A3CB-16950275D3FD}"
1919
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwitchLib.Client.Example", "TwitchLib.Client.Example\TwitchLib.Client.Example.csproj", "{0C5D0C17-346C-6CE8-EC3E-4DCAC37152FC}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -43,6 +45,10 @@ Global
4345
{7E666AC9-B717-4E00-A3CB-16950275D3FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
4446
{7E666AC9-B717-4E00-A3CB-16950275D3FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
4547
{7E666AC9-B717-4E00-A3CB-16950275D3FD}.Release|Any CPU.Build.0 = Release|Any CPU
48+
{0C5D0C17-346C-6CE8-EC3E-4DCAC37152FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49+
{0C5D0C17-346C-6CE8-EC3E-4DCAC37152FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
50+
{0C5D0C17-346C-6CE8-EC3E-4DCAC37152FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
51+
{0C5D0C17-346C-6CE8-EC3E-4DCAC37152FC}.Release|Any CPU.Build.0 = Release|Any CPU
4652
EndGlobalSection
4753
GlobalSection(SolutionProperties) = preSolution
4854
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)