From 29b5b111b2e78ff429d8539e96827aa8d9f6b5cc Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 21 Mar 2024 08:49:43 -0500 Subject: [PATCH] Added a demo that connects to a channel to print user changes and bit cheers. --- TwitchIrcClient/Program.cs | 63 +++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/TwitchIrcClient/Program.cs b/TwitchIrcClient/Program.cs index 5f28270..885e0ed 100644 --- a/TwitchIrcClient/Program.cs +++ b/TwitchIrcClient/Program.cs @@ -1 +1,62 @@ - \ No newline at end of file +using System.Collections.Concurrent; +using System.Reflection.Metadata; +using System.Security.AccessControl; +using System.Threading.Channels; +using TwitchIrcClient.IRC; +using TwitchIrcClient.IRC.Messages; + +RateLimiter limiter = new(20, 30); +bool ssl = true; +async Task CreateConnection(string channel) +{ + IrcConnection connection; + if (ssl) + connection = new IrcConnection("irc.chat.twitch.tv", 6697, limiter, true, true); + else + connection = new IrcConnection("irc.chat.twitch.tv", 6667, limiter, true, false); + connection.AddCallback(new MessageCallbackItem( + (o, m) => + { + if (m is Privmsg priv) + { + if (priv.Bits > 0) + lock (Console.Out) + Console.WriteLine($"{priv.DisplayName}: {priv.Bits}{Environment.NewLine}"); + } + else + throw new ArgumentException("Received an unrequested message type", nameof(m)); + }, [IrcMessageType.PRIVMSG])); + connection.onUserChange += (object? o, UserChangeEventArgs args) => + { + lock (Console.Out) + { + var resetColor = Console.BackgroundColor; + Console.BackgroundColor = ConsoleColor.DarkGreen; + Console.WriteLine(string.Join(", ", args.Joined.Order())); + Console.BackgroundColor = ConsoleColor.DarkRed; + Console.WriteLine(string.Join(", ", args.Left.Order())); + Console.BackgroundColor = resetColor; + Console.WriteLine(); + } + }; + if (!await connection.ConnectAsync()) + { + Console.WriteLine("failed to connect"); + Environment.Exit(-1); + } + connection.Authenticate(null, null); + connection.SendLine("CAP REQ :twitch.tv/commands twitch.tv/membership twitch.tv/tags"); + connection.JoinChannel(channel); + return connection; +} +Console.Write("Channel: "); +var channelName = Console.ReadLine(); +ArgumentNullException.ThrowIfNull(channelName, nameof(Channel)); +var connection = CreateConnection(channelName); +while (true) +{ + //all the work happens in other threads + //specifically the threadpool used by Task.Run for + //the tasks owned by the IrcConnection + await Task.Delay(1000); +}