0

So I am making a bot that connects to a twitch irc chat. Here is the code:

Console.WriteLine("Joining Room");
IrcClient irc = new IrcClient("irc.twitch.tv", 6667, "ScottBots", "oauth:asdasd");

irc.joinRoom("ScottBots");
Console.WriteLine("Joined Room");

int i = 0;

while (true)
{
    string message = irc.readMessage();
    if(message.Contains("!helps"))
    {
        irc.sendChatMessage("Welcome to ScottBots! Currently in development.");
    }
    Console.WriteLine("loop: " + i);
    i++;
}

Look the the while true loop... Everything good?

Now look at what this console application gives me:

Joining Room
Joined Room
loop: 0
loop: 1
loop: 2
loop: 3
loop: 4
loop: 5
loop: 6
loop: 7
loop: 8
loop: 9

It just stops at 9?

Many of ya'll have been asking for my ircClient Code:

private string username;
    private string channel;

    private TcpClient tcpClient;
    private StreamReader inputStream;
    private StreamWriter outputSteam;

    public IrcClient(string ip, int port, string username, string password)
    {
        this.username = username;

        tcpClient = new TcpClient(ip, port);
        inputStream = new StreamReader(tcpClient.GetStream());
        outputSteam = new StreamWriter(tcpClient.GetStream());
        try
        {
            outputSteam.WriteLine("PASS " + password);
            outputSteam.WriteLine("NICK " + username);
            outputSteam.WriteLine("USER " + username + " 8 * :" + username);
            outputSteam.Flush();
        } catch (Exception e)
        {

        }

    }

    public void joinRoom(string channel)
    {
        try
        {
            this.channel = channel;
            outputSteam.WriteLine("JOIN #" + channel);
            outputSteam.Flush();
        }
        catch (Exception e)
        {
            Console.WriteLine("Failed to join room");
        }

    }

    public void sendIrcMessage(string message)
    {


        try
        {
            outputSteam.WriteLine(message);
            outputSteam.Flush();
        }
        catch (Exception e)
        {
            Console.WriteLine("failed to run sendIrcMessage() method");
        }
    }

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

    public string readMessage()
    {
        string message = inputStream.ReadLine();
        return message;
    }
Cooper Scott
  • 651
  • 8
  • 18
  • 4
    Most probably one of your `irc.XXX` methods fails and throws exception in your loop. – Eser Aug 11 '15 at 20:36
  • Should I just put a try catch around everything? @Eser – Cooper Scott Aug 11 '15 at 20:37
  • 1
    `string message = irc.readMessage();` does this wait for a message, like `Console.ReadLine()`? Maybe it's waiting on input. Pause when the program seemingly stops, and debug it – Jonesopolis Aug 11 '15 at 20:38
  • Does the irc have an API where you have a callback once a message is available? This would be a lot easier on your resources... – flq Aug 11 '15 at 20:40
  • @flp I don't believe so. I am brand new to C# and have no idea how to do that. sorry. I updated the post w/ my IrcClient Class. Maybe ya'll can take a look. – Cooper Scott Aug 11 '15 at 20:48
  • @Jonesopolis I updated the post w/ my IrcClient Class. Maybe ya'll can take a look. – Cooper Scott Aug 11 '15 at 20:48

1 Answers1

3

It looks like one of irc.SendChatMessage() or irc.ReadMessage() is blocking, perhaps when it runs out of buffered input/output and needs to wait on the socket.

EDIT: Almost certainly the irc.ReadMessage() call is blocking. It's calling ReadLine() against a Stream which is in turn linked to a TcpClient. You probably want to look into either threading and/or asynchronous callbacks if you want to build an IRC bot and test rig. Here's an example (not IRC-related): http://sunildube.blogspot.ca/2011/12/asynchronous-tcp-client-easy-example.html

Community
  • 1
  • 1
Jordan Rieger
  • 3,025
  • 3
  • 30
  • 50
  • Okay. I'm new to C# and don't know a whole lot. How do I buffer and wait for the socket? Please help. Thanks (Updated post w/ IrcClient class) – Cooper Scott Aug 11 '15 at 21:10
  • @CooperScott Updated my answer with a link to a simple example. – Jordan Rieger Aug 11 '15 at 21:15
  • I see thank you. Looking at the code. I have no idea how to implement. Saying you answered though because it probably is the answer and I am probably just stupid. If you're feeling nice. Could you help me out some more? lol, thanks for everything so far! – Cooper Scott Aug 11 '15 at 22:05
  • @CooperScott I think you would be best served by working through some online tutorials of building client-server socket code in C# at your own pace. Google for some general stuff -- even if it's not related to IRC, putting in the time to learn some basic background will serve you well. Unfortunately, at the moment I don't have time to do more than answer the occasional question on Stack Overflow. – Jordan Rieger Aug 11 '15 at 23:06
  • Well thank you for all the help you have given me. Understand you too are working on a time frame. Thanks again for all the help & tips. – Cooper Scott Aug 11 '15 at 23:28