First time poster over here!
I’ve recently started coding a Twitch Chat Bot, and it’s going pretty well except for a few runtime errors I can’t seem to figure out. I haven’t been keeping any repository of my code (stupid) so I can’t go back and see when they started.
I’ve not had any real experience with networking code until now, so these may be silly and obvious but I haven’t been able to figure them out on my own.
I currently have 3 runtimes I know of. 2 of which are more problematic. The 1st only happens on startup occasionally.
The two most problematic (equally so) are the following, which I’ll link a screenshot to what I get when the runtime occurs, as well as show the code I THINK it’s connected to.
https://pasteboard.co/iX6kmL722.png <-- Runtime #1
https://pasteboard.co/iX7vrNydT.png <— Runtime #2
These runtimes take between 3-15 minutes to hit. (Probably due to my timers, I can shorten them for debugging)
but I’m not able to be reproduce them in a controlled way.
Here is specific code I think may be helpful for RUNTIME #1: ( https://pasteboard.co/iX6kmL722.png )
private void getMessage() //Receive Messages
{
g_ServerStream = g_IRC.tcpClient.GetStream();
//Not sure what the following does exactly, only that it works.
int buffsize = 0;
byte[] inStream = new byte[10025];
buffsize = g_IRC.tcpClient.ReceiveBufferSize;
while(true)
{
try
{
g_ReadData = g_IRC.readMessage();
chatUpdate();
}
catch(Exception exception)
{
//Nothing
}
}
}
In that code, g_ReadData is being set to the return of my readMessage() from IRC.
I’d assume the return of readMessage() would only be null if g_IRC was null.
However it’s only caught later on in my chatUpdate() loop, probably because I used a try/catch
Not sure whats going on with this one honestly.
What I think is relevant for RUNTIME #2: (https://pasteboard.co/iX7vrNydT.png)
private void ViewerListUpdate()
{
activeChatUsersListBox.Items.Clear(); //Purge old viewer data
Chatters AllChatters = ChatClient.GetChatters(g_Channel);
int numChatters = ChatClient.GetChatterCount(g_Channel);
activeChatViewersLabel.Text = "Active Chat Users : " + numChatters.ToString();
activeChatUsersListBox.Text += "Retrieving viewer list...";
//Get all Admins
foreach(string admin in AllChatters.Admins)
{
activeChatUsersListBox.Items.Add(admin + Environment.NewLine);
g_activeMods.Add(admin);
}
//Get all Twitch Staff
foreach (string staff in AllChatters.Staff)
{
activeChatUsersListBox.Items.Add(staff + Environment.NewLine);
g_activeMods.Add(staff);
}
//Get all global moderators
foreach (string globalmod in AllChatters.GlobalMods)
{
activeChatUsersListBox.Items.Add(globalmod + Environment.NewLine);
g_activeMods.Add(globalmod);
}
//Get all moderators
foreach (string moderator in AllChatters.Moderators)
{
activeChatUsersListBox.Items.Add(moderator + Environment.NewLine);
g_activeMods.Add(moderator);
}
//Get all viewers
foreach (string viewer in AllChatters.Viewers)
{
activeChatUsersListBox.Items.Add(viewer + Environment.NewLine);
}
eventsBox.Text = eventsBox.Text + DateTime.Now.ToString("HH:mm:ss ", System.Globalization.DateTimeFormatInfo.InvariantInfo) + ": Viewer list updated!" + Environment.NewLine;
}
This is where the next runtime occurs, this gives me a lot less information that I can make sense of.
It throws:
System.IndexOutOfRangeException: ‘index was outside the bounds of the array.’
However… I am not using an array here that I know of… Also I’m not sure what would change when running this code later as opposed to when I first run the program.
I update the viewer list on launch, and then every 5 minutes on a timer that simple calls the function above.
I assume it works the first time on launch, then the 2nd time. By the 3rd I think it usually.
Lastly the least problematic one.
https://pasteboard.co/wZSoKlrW.png -> RUNTIME #3
As soon as I start my program it’ll occasionally get stuck here, I assume something with the port/IP (which are correct I believe, them being: irc.chat.twitch.tv" as IP and 6667 as the Port.)
relevant code: (simply what is in the screenshot, as I’m not sure where else it could be)
public IRCClient(string ip, int port, string userName, string password)
{
tcpClient = new TcpClient(ip, port);
inputStream = new StreamReader(tcpClient.GetStream());
outputStream = new StreamWriter(tcpClient.GetStream());
outputStream.WriteLine("PASS " + password);
outputStream.WriteLine("NICK " + userName);
outputStream.WriteLine("USER " + userName + " 8 * :" + userName); //Based on Twitch documentation
//Permission Request for checking viewerlist etc...
outputStream.WriteLine("CAP REQ :twitch.tv/membership");
outputStream.WriteLine("CAP REQ :twitch.tv/commands");
outputStream.Flush();
}
This one I don’t understand at all, it gives System.Net.Sockets.SocketException: ‘This is usually a temporary error during hostname resolution and means that the local server did not receive a response from the authoritative server’
I assume this means when I started it, it tried to connect to the twitch server failed.
If I start it up again it’ll usually connect in the next 1-2 tries.
Is there a more reliable way to do this? or am I simply doing something wrong?
Hopefully some of you more brilliant coders than I can spot something I can investigate…
Cheers!