I’m working on a Unity project that connects to Twitch IRC chat using a custom class. Everything works fine for basic chat functionality, but I’m running into two specific problems that I can’t find good documentation for.
First problem is that all usernames come through in lowercase only, even when the actual Twitch username has mixed case letters. I need to get the proper capitalization for display purposes.
Second issue is that I can’t figure out how to detect user roles like moderators, subscribers, or VIPs from the IRC messages. The role detection isn’t super urgent but the username capitalization is really important for my project.
Has anyone dealt with this before? Is there a way to get properly formatted usernames through IRC or do I need to use a different approach?
using System;
using System.IO;
using System.Net.Sockets;
using UnityEngine;
public class TwitchIrcClient : MonoBehaviour
{
public TwitchCredentials credentials = new();
public event Action<string, string> OnMessageReceived;
public event Action<string> OnConnected;
public event Action OnDisconnected;
private TcpClient client;
private StreamReader streamReader;
private StreamWriter streamWriter;
private const string SERVER = "irc.chat.twitch.tv";
private const int PORT = 6667;
public void EstablishConnection()
{
client = new TcpClient(SERVER, PORT);
streamReader = new StreamReader(client.GetStream());
streamWriter = new StreamWriter(client.GetStream());
streamWriter.WriteLine("PASS " + credentials.Token);
streamWriter.WriteLine("NICK " + credentials.Username.ToLower());
streamWriter.WriteLine("JOIN #" + credentials.ChannelName.ToLower());
streamWriter.Flush();
}
private void Update()
{
if (client?.Available > 0)
{
string rawMessage = streamReader.ReadLine();
if (rawMessage.Contains("PRIVMSG"))
{
int userEnd = rawMessage.IndexOf("!");
string username = rawMessage.Substring(1, userEnd - 1);
int messageStart = rawMessage.IndexOf(":", 1);
string chatMessage = rawMessage.Substring(messageStart + 1);
OnMessageReceived?.Invoke(username, chatMessage);
}
}
}
}