Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • Issues connecting bot to IRC Server


    Tasemu
     Share

    Recommended Posts

    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

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.