I am trying to set up a simple IRC chat bot, but I can’t seem to get it to read chat to allow for custom commands. I can send commands like clearchat and such. I feel like I’m missing something but, after going through the documentation multiple times, I’ve almost given up. I don’t know what I’m missing.
EDIT: After looking more at the packets coming in, I see that my PC is recieving them, but it’s not being passed to the socket for some reason
OUTPUT----------------------------
Started
irc.chat.twitch.tv/52.35.74.208
Connected: true
< Wrote to server: PASS oauth:*****
< Wrote to server: NICK anthony_otc
>:tmi.twitch.tv 001 anthony_otc :Welcome, GLHF!
>:tmi.twitch.tv 002 anthony_otc :Your host is tmi.twitch.tv
>:tmi.twitch.tv 003 anthony_otc :This server is rather new
>:tmi.twitch.tv 004 anthony_otc :-
>:tmi.twitch.tv 375 anthony_otc :-
>:tmi.twitch.tv 372 anthony_otc :You are in a maze of twisty passages, all alike.
< Wrote to server: JOIN #anthony_otc
>:tmi.twitch.tv 376 anthony_otc :>
>:anthony_otc!anthony_otc@anthony_otc.tmi.twitch.tv JOIN #anthony_otc
>:anthony_otc.tmi.twitch.tv 353 anthony_otc = #anthony_otc :anthony_otc
>:anthony_otc.tmi.twitch.tv 366 anthony_otc #anthony_otc :End of /NAMES list
< Wrote to server: CAP REQ :twitch.tv/membership twitch.tv/commands
< Wrote to server: PRIVMSG #anthony_otc :/color OrangeRed
< Wrote to server: PRIVMSG #anthony_otc :Greetings! I am GulBot!
>:tmi.twitch.tv CAP * ACK :twitch.tv/membership twitch.tv/commands
>:tmi.twitch.tv NOTICE #anthony_otc :Your color has been changed.
>:tmi.twitch.tv USERSTATE #anthony_otc
CODE---------------------------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;
public class Socket_test {
static int port=6667;
static String addressString="irc.chat.twitch.tv";
static String nickname="anthony_otc";
static String channel=nickname;
static String password="oauth:*****";
static boolean joined=false;
static Socket socket;
static Scanner input;
static PrintWriter output;
public static void main(String[] args) {
System.out.println("Started");
int customTimeout=500;
try {
socket = new Socket(InetAddress.getByName(addressString),port);
socket.setKeepAlive(true);
socket.setSoTimeout(500);
input = new Scanner(socket.getInputStream());
output = new PrintWriter(socket.getOutputStream(),true);
System.out.println(socket.getInetAddress());
System.out.println("Connected: "+socket.isConnected());
connect(password,nickname);
while(customTimeout-->0||true) {
String line="";
try {
if(input.hasNext()) {
line=input.nextLine();
System.out.println(">>"+line);
}
if(line.contains("PING :tmi.twitch.tv")) {
write("PONG :tmi.twitch.tv");
}else if(line.contains("You are in a maze")) {
join(channel);
}else if(line.contains(":End of /NAMES list")) {
write("CAP REQ :twitch.tv/membership twitch.tv/commands");
writeMsg("/color OrangeRed");
writeMsg("Greetings! I am GulBot!");
}else {
//System.out.println(".");
}
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Connected: "+socket.isConnected());
System.out.println("done");
}
public static void connect(String password, String nickname) {
write("PASS "+password);
write("NICK "+nickname.toLowerCase());
}
public static void join(String channel) {
write("JOIN #"+channel.toLowerCase());
}
public static void part(String channel) {
write("PART #"+channel);
}
public static void writeMsg(String msg) {
write("PRIVMSG #"+channel+" :"+msg);
}
public static void write(String msg) {
output.println(msg);
System.out.println("< Wrote to server: "+msg+"\r\n");
}
}