I have a class created through a few different sources to connect, stay connected, read, and interact with a selected Twitch channel, but there seems to be no information as to how to do certain things.
One of the issues is that the names of the chatters are always in all lowercase characters. The other issue is that there doesnt seem to be any information of users. Ie are they a mod, subscriber, VIP?
While the roles parts arent that high priority for me, I want for the names to be properly capitalized.
Is there any trick that can do this?
using System;
using System.IO;
using System.Net.Sockets;
using UnityEngine;
public class TwitchConnect : MonoBehaviour
{
public static TwitchConnect instance;
public TwitchAccountData TwitchSettings = new();
public event Action<string, string> OnChatMessage;
public event Action<string> OnLoggedIn;
public event Action OnLoggedOut;
public event Action<string> OnInvalidLogin;
public bool loggedIn = false;
private TcpClient Twitch;
private StreamReader Reader;
private StreamWriter Writer;
//private const string URL = "irc.chat.twitch.tv";
private const string URL = "irc.twitch.tv";
private const int PORT = 6667;
private float PingCounter = 0;
private string saveTwitchPath = "twitchauth.json";
private void Awake()
{
DontDestroyOnLoad(gameObject);
instance = this;
saveTwitchPath = Application.persistentDataPath + "/twitchauth.json";
LoadAccount();
}
public void UpdateAccount(string nUser, string nOAuth, string nChannel)
{
TwitchSettings.User = nUser;
TwitchSettings.OAuth = nOAuth;
TwitchSettings.Channel = nChannel;
}
public void ConnectToTwitch()
{
DisconnectFromTwitch();
Twitch = new TcpClient(URL, PORT);
Reader = new StreamReader(Twitch.GetStream());
Writer = new StreamWriter(Twitch.GetStream());
TwitchSettings.User = TwitchSettings.User.ToLower();
TwitchSettings.Channel = TwitchSettings.Channel.ToLower();
Writer.WriteLine("PASS " + TwitchSettings.OAuth);
Writer.WriteLine("NICK " + TwitchSettings.User);
Writer.WriteLine($"USER {TwitchSettings.User} 8 * :{TwitchSettings.User}");
Writer.WriteLine("JOIN #" + TwitchSettings.Channel);
Writer.Flush();//initiate connection
}
public void DisconnectFromTwitch()
{
loggedIn = false;
PingCounter = 0;
Twitch?.Dispose();
Reader?.Dispose();
Writer?.Dispose();
Twitch = null;
Reader = null;
Writer = null;
OnLoggedOut?.Invoke();
}
public void SaveAccount()
{
string jsonTwitch = JsonUtility.ToJson(TwitchSettings, true);
File.WriteAllText(saveTwitchPath, jsonTwitch, System.Text.Encoding.Unicode);
Debug.LogWarning("Twitch account settings saved!");
}
public void LoadAccount()
{
//twitch saved account settings - user, oauth, channel
if (File.Exists(saveTwitchPath))
{
string jsonSettings = File.ReadAllText(saveTwitchPath, System.Text.Encoding.Unicode);
JsonUtility.FromJsonOverwrite(jsonSettings, TwitchSettings);
Debug.LogWarning("Twitch account loaded, logging in...");
ConnectToTwitch();
}
}
public void SendIrcMessage(string message)
{
Writer.WriteLine(message);
Writer.Flush();
}
public void SendChatMessage(string message)
{
Writer.WriteLine($":{TwitchSettings.User}!{TwitchSettings.User}@{TwitchSettings.User}.tmi.twitch.tv PRIVMSG #{TwitchSettings.Channel} :{message}");
Writer.Flush();
}
private void Update()
{
if (Twitch == null) return;
if (!Twitch.Connected && loggedIn)
{
ConnectToTwitch();
}
PingCounter += Time.deltaTime;
if (PingCounter > 50)
{
Writer.WriteLine("PING " + URL);
Writer.Flush();
PingCounter = 0;
}
if (Twitch.Available > 0)
{
string message = Reader.ReadLine();
#if UNITY_EDITOR
Debug.Log(message);//DEBUG
#endif
if (message.Contains("PRIVMSG"))
{
int splitPoint = message.IndexOf("!");
string chatter = message[1..splitPoint];//get chatter name
splitPoint = message.IndexOf(":", 1);
string msg = message[(splitPoint + 1)..];//get message only
OnChatMessage?.Invoke(chatter, msg);
}
else if (message.Contains(":Welcome, GLHF!"))//successful login
{
SaveAccount();
loggedIn = true;
OnLoggedIn?.Invoke(TwitchSettings.User);
}
else if (message.Contains(":Invalid NICK"))
{
OnInvalidLogin?.Invoke("Invalid NICK");
}
else if (message.Contains(":Improperly formatted auth"))
{
OnInvalidLogin?.Invoke("Improperly formatted auth");
}
}
}
private void OnDestroy()
{
DisconnectFromTwitch();
}
}