Last active
March 5, 2024 08:23
-
-
Save neon-sunset/a296b43d0a93736c4f9c31597de5f555 to your computer and use it in GitHub Desktop.
Simple Twitch IRC chat reader with U8String abstractions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using System.Net.Sockets; | |
using System.Runtime.InteropServices; | |
using U8; | |
using U8.IO; | |
using Warpskimmer; | |
using var cts = new CancellationTokenSource(); | |
using var sigint = PosixSignalRegistration.Create( | |
PosixSignal.SIGINT, ctx => | |
{ | |
ctx.Cancel = true; | |
cts.Cancel(); | |
}); | |
if (args is not [var chan] || chan is []) | |
{ | |
U8Console.WriteLine("Usage: <channel>"u8); | |
return; | |
} | |
using var sock = new Socket( | |
AddressFamily.InterNetwork, | |
SocketType.Stream, | |
ProtocolType.Tcp); | |
var addr = await Dns.GetHostAddressesAsync("irc.chat.twitch.tv"); | |
await sock.ConnectAsync(addr, 6667, cts.Token); | |
await sock.SendAsync(u8("PASS SCHMOOPIIE\r\n"), cts.Token); | |
await sock.SendAsync(u8("NICK justinfan54970\r\n"), cts.Token); | |
await sock.SendAsync(u8("USER justinfan54970 8 * :justinfan54970\r\n"), cts.Token); | |
await sock.SendAsync(u8($"JOIN #{chan}\r\n"), cts.Token); | |
try | |
{ | |
await foreach (var line in sock | |
.AsU8Reader() | |
.Lines | |
.WithCancellation(cts.Token)) | |
{ | |
var msg = Message.Parse(line); | |
if (msg.Command == Command.Ping) | |
{ | |
await sock.SendAsync(u8("PONG :tmi.twitch.tv\r\n")); | |
U8Console.WriteLine(u8("-- PONG --")); | |
continue; | |
} | |
PrintMessage(msg); | |
} | |
} | |
catch (OperationCanceledException) { } | |
await sock.SendAsync(u8($"PART #{chan}\r\n")); | |
await sock.DisconnectAsync(false); | |
U8Console.WriteLine(u8("Goodbye!")); | |
static void PrintMessage(Message msg) => | |
U8Console.WriteLine($"{msg.Prefix?.Nickname}: {msg.Parameters}"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment