Help me with my bot

Could someone tell me what I’m doing wrong with this code?
My visual studio is saying this line of code is causing the issue
public void joinRoom(string channel)
{
outputStream.WriteLine(“JOIN #” + channel);
outputStream.Flush();
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace TwitchBot
{
class IrcClient
{
private string userName;
private string channel;

    private TcpClient tcpClient;
    private StreamReader inputStream;
    private StreamWriter outputStream;
    private string v1;
    private string v2;
    private string v3;

    public IrcClient(string ip, int port, string userName, string password)
    {

        this.userName = userName; 

        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);
        outputStream.Flush(); 

    }

    public IrcClient(string v1, string v2, string v3)
    {
        this.v1 = v1;
        this.v2 = v2;
        this.v3 = v3;
    }

    public void joinRoom(string channel)
    {
        outputStream.WriteLine("JOIN #" + channel); 
        outputStream.Flush(); 
    }
    public void sendIrcMessage(string message)
    {
        outputStream.WriteLine(message);
        outputStream.Flush(); 
    }

    public void sendChatMessage(string message)
    {
        sendIrcMessage(":" + userName + "!" + userName  + "@" + userName + ".tmi.twitch.tv PRIVMSG#"  + channel + " :" + message);

    }

    public string readMessage()
    {
        string message = inputStream.ReadLine();
        return message; 
    }

}

}

What’s the error/exception Visual Studio gives you? Are you sure the connection is still alive at that point?

Also, clients don’t need to send the hostmask, just PRIVMSG #channel :message is enough, and you’re missing a space between PRIVMSG and # in sendChatMessage.

It saying
An unhandled exception of type ‘System.NullReferenceException’ occurred in TwitchBot.exe

Additional information: Object reference not set to an instance of an object.

This would suggest that outputStream is null, which usually means it was never initialized in the first place.

You seem to have two constructors that does two different things. One that initializes outputStream and one that does not. Every constructor should initialize every member variable. Multiple constructors are usually set up so that one takes the most possible parameters and the others takes less by explicitly calling the first constructor with hard-coded default values.

You cannot divide initialization over several constructors. If you feel that the parameter list would be too long to initialize all variables you can wrap related parameters in other classes or containers

Besides my above point being a fault in your code I see the real problem now. You have a private string member variable called channel which is never initialized (and does not look like it is intended to be used). The joinRoom method has a parameter with the same name, and the member variable takes precedence in the confusion. So the line outputStream.WriteLine("JOIN #" + channel); is actually called on the uninitialized member variable instead of the parameter with the same name.

Change the line to:
this.channel = channel;
outputStream.WriteLine("JOIN #" + this.channel);

or better yet, adopt a better naming convention to avoid future mixups.

The problem isn’t the channel parameter in the method with the same name as a parameter of the class, it’s still using the one of the method unless you call it up using the this prefix. It’s still a good idea to get a better naming convention. ReSharper will warn for issues like this on the fly.

I think something goes wrong in the actual code calling the method, or even better at the constructor.
Can you show use the line of code where you are constructing the IrcClient?

If the login fails, the writer stream will be null.

The easiest way to debug this yourself is to use visual studio’s code stepper, set a break point on the first entry point to your code, and use step into until you reach the exception. You can also use try{}catch(Exception ex) {Console.WriteLine(ex.ToString())} to find the method that is causing the issue.

My guess is that your password is incorrect, and the login is failing, you aren’t handling the error and instead try to get the stream for the reader/writer, and that’s failing and throwing the error. Your GetStream object shouldn’t be null, and the reader should immediately return the new server message with GL HF or w/e it is.

    public void sendChatMessage(string message)
    {
        sendIrcMessage(":" + userName + "!" + userName  + "@" + userName + ".tmi.twitch.tv PRIVMSG#"  + channel + " :" + message);

    }

I assume this is just a test message? This is not a valid format for a twitch IRC message. You only send PRIVMSG #channel :message

You also don’t show the local classes ‘userName’, depending on how you set it up, that could also be part of the issue.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.