Tasemu 1 Share Posted March 3, 2020 Hi, i have a version of an IRC class made by Roma that I have modified slightly. It appears I am able to connect to the server fine, however I never see my bot join the channel. Could anyone help me out with some advice on getting this working please? It would help so much! I noticed it was not connecting and first and on inspection the server appeared to be pinging me with a hash, i replied with PONG and the same hash and now I can connect to the server. However joining the channel is still eluding me. package tools; import org.dreambot.api.Client; import org.dreambot.api.methods.MethodContext; import java.io.*; import java.net.Socket; import java.util.ArrayList; /** * Created by Roma on 21.11.2015 * */ public class IRCConnection extends Thread { BufferedWriter writer; private String server = "irc.SwiftIRC.net"; Socket socket; private boolean running = true; private ArrayList<IRCListener> listeners; private String username; private String channel; private String channelPassword; public IRCConnection(String channel) { this.listeners = new ArrayList<>(); this.channel = channel; this.username = generateIRCName(); this.channelPassword = ""; } public IRCConnection(String channel, String channelPassword) { this.listeners = new ArrayList<>(); this.channel = channel; this.username = generateIRCName(); this.channelPassword = channelPassword; } public IRCConnection(String username, String channel, String channelPassword){ this.listeners = new ArrayList<>(); this.username = username; this.channel = channel; this.channelPassword = channelPassword; } public IRCConnection(String server, String username, String channel, String channelPassword){ this.listeners = new ArrayList<>(); this.channel = channel; this.username = username; this.server = server; this.channelPassword = channelPassword; } @Override public void run(){ try { MethodContext.log("Trying to connect to IRC Server"); socket = new Socket(server, 6667); writer = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())); BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); writer.write("NICK " + username + "\r\n"); writer.write("USER " + username + " 8 * : IRC bot\r\n"); writer.flush(); MethodContext.log("Connected to server"); final long timeout = System.currentTimeMillis() + 10000; String line; while ((line = reader.readLine()) != null) { MethodContext.log("beep"); if (System.currentTimeMillis() >= timeout) { MethodContext.log("Connection timed out"); break; } if (line.contains("004")) { break; } else if (line.contains("433")) { MethodContext.log("Nickname is already in use"); return; } else if (line.contains("PING")) { MethodContext.log("Sending: " + line.replaceAll("\\bPING\\b", "PONG")); writer.write(line.replaceAll("\\bPING\\b", "PONG")); break; } } MethodContext.log("Finished line loop"); writer.write("JOIN " + channel + " " + channelPassword + "\r\n"); MethodContext.log("Writing: JOIN " + channel + " " + channelPassword + "\r\n"); writer.flush(); while ((line = reader.readLine()) != null) { if(!running) break; if(line.contains("PING ")) { writer.write("PONG " + line.substring(5) + "\n"); writer.flush(); } else { MethodContext.log(line); } } } catch(Exception e) { MethodContext.logError(e.getMessage()); e.printStackTrace(); } } public void addListener(IRCListener ircListener) { listeners.add(ircListener); } public void removeListener(IRCListener ircListener) { listeners.remove(ircListener); } private void addTrigger(IRCMessage message){ for (IRCListener listener : listeners) listener.messageReceived(message); } public void closeConnection() { MethodContext.log("Closing connection"); while (!socket.isClosed()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } running = false; MethodContext.log("Connection closed"); } private void send(String buffer) { try { this.writer.write(buffer); this.writer.flush(); } catch (IOException e) { e.printStackTrace(); } } public void sendChannelMessage(String message) { this.send("PRIVMSG " + this.channel + " :" + message + "\n"); } public void sendPrivateMessage(String name, String message) { this.send("PRIVMSG " + name + " :" + message + "\n"); } public void sendNotice(String target, String message) { this.send("NOTICE " + target + " :" + message + "\n"); } private String getRSN() { return Client.getClient().getLocalPlayer().getName(); } private String generateIRCName() { String outputName; int t = 0; for (int i = 0; i < getRSN().length(); i++) { if(Character.isDigit(getRSN().charAt(i))) t++; else break; } outputName = getRSN().substring(t); if (outputName.contains(" ")) outputName = outputName.replaceAll(" ", "_"); return outputName; } } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.