Chat bot fails to get new messages after a short amount of time

So I’m pretty new to coding, just like to get that out of the way first haha. The code seems to run perfectly for a bit. at around 5 minutes of running the program fails to register any newly added messages from my chat though. If anyone can browse through my code really quick and notice any glaring issues that would be awesome!

Start.bat

java -jar LlamaInterface.jar username oauth:code file.txt

Java code

package LlamaInterface;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class LlamaInterface {

    private static String user;
    private static String pass;
    private static String name;
    public static void main(String[] args) {
            user = args[0];
            pass = args[1];
            name = args[2];
            try {
                    Socket socket = new Socket("irc.twitch.tv", 6667);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                    writer.write("PASS " + pass + "\r\n");
                    writer.write("NICK " + user + "\r\n");
                    writer.write("JOIN #" + user + "\r\n");
                    writer.flush();
                    String line;
                    while ((line = reader.readLine()) != null) {
                            System.out.println(line);
                            String[] parts = line.split(" ");
                            if (parts[0].equals("PING")) {
                                    System.out.println("Replying to pong");
                                    writer.write("PONG irc.twitch.tv" + "\r\n");
                                    writer.flush();
                            } else if (parts[1].equals("PRIVMSG")) {
                                    if (parts[2].equals("#" + user)) {
                                            if (parts[3].substring(1).equals("!song")) {
                                                    writer.write("PRIVMSG #" + user + " :Song: " + readFile(new File(name)) + "\r\n");
                                                    writer.flush();
                                            }
                                    }
                            }
                    }
                    socket.close();
            } catch (IOException e) {
                    e.printStackTrace();
            }
    }
    public static String readFile(File file) {
            try {
                    FileInputStream reader = new FileInputStream(file);
                    byte[] data = new byte[(int) file.length()];
                    reader.read(data);
                    reader.close();
                    System.out.println("Read from file: " + new String(data, "UTF-8"));
                    return new String(data, "UTF-8");
            } catch (IOException e) {
                    e.printStackTrace();
            }
            return "Cannot read the song file!";
    }

}

I edited your title and category to clarify that this is a chat bot, not something that uses the API (Kraken). Hopefully this gets you better responses.

From your description, I’d suspect something is wrong with your PING/PONG logic, and that you’re getting disconnected from chat. However I don’t see anything obviously wrong with your code, so I’m not sure my hypothesis is right.

Really appreciated dude. This definitely seems like a much more appropriate area for the kinda post. And that’s a bummer to hear… I was hoping the issue would be something obvious. Hopefully someone can manage to spot something or i’m gonna have allot of troubleshooting to do haha.

Can you paste the output to stdout? (System.out.println)

I’ve tested your code for half an hour and didn’t have any trouble receiving messages I typed in my channel. I only hardcoded the username/password and commented out the file read stuff in readFile().

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