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!"; }
}