Author Topic: Java IRC Bot  (Read 2250 times)

0 Members and 1 Guest are viewing this topic.

Offline ZonTa

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 0
  • -[s3cr3tz]-
    • View Profile
Java IRC Bot
« on: November 25, 2010, 06:51:24 pm »
I was going to make an AI bot , and ended up with this. lol :P

Code: [Select]
/**
 *
 * @author ZonTa
 */
import java.io.*;
import java.util.*;
import java.net.Socket;


public class JBot implements Runnable {

   Random generator = new Random();
   int r = generator.nextInt(100);

   private String server;
   private int port;
   private String channel;
   private String nick, user, name, owner;

   protected void server(String server) {
      this.server = server;
   }

   protected String server() {
      return this.server;
   }

   
   protected void port(int port) {
      this.port = port;
   }

   protected void channel(String channel) {
      this.channel = channel;
   }

   protected String channel() {
      return this.channel;
   }

   protected int port() {
      return this.port;
   }

   protected void nick(String nick) {
      this.nick = nick;
   }

   protected String nick() {
      return this.nick;
   }

   protected void user(String user) {
      this.user = user;
   }

   protected String user() {
      return this.user;
   }

   protected void name(String name) {
      this.name = name;
   }

   protected String name() {
      return this.name;
   }

   protected void owner(String owner) {
      this.owner = owner;
   }

   protected String owner() {
      return this.owner;
   }

   private boolean isActive;

   protected void isActive(boolean bool) {
      this.isActive = bool;
   }

   protected boolean isActive() {
      return this.isActive;
   }

   public static void main(String args[]) {
      System.out.println("Starting program.");
      try {
         new JBot().start();
      } catch (java.io.IOException e) {
      }
   }

      /**
* Credits to whoever wrote it.
* The AI KnowledgeBase for the bot */
      static String[][] KnowledgeBase = {
        {"HELLO",
"Hello!."
},

        {"WHAT IS YOUR NAME",
"MY\tNAME\tIS\tJ-BOT."
},

{"HI",
"HI\tTHERE!",
},

{"HOW ARE YOU",
"I'M\tDOING\tFINE!"
},

{"WHO ARE YOU",
"I'M\tAN\tA.I\tPROGRAM."
},

{"ARE YOU INTELLIGENT",
"YES,OFCORSE."
},

{"ARE YOU REAL",
"DOES\tTHAT\tQUESTION\tREALLY\tMATERS\tTO\tYOU?"
}
    };

    static String findMatch(String str) {
String result = "";
for(int i = 0; i < KnowledgeBase.length; ++i) {
if(KnowledgeBase[i][0].equalsIgnoreCase(str)) {
result = KnowledgeBase[i][1];
break;
}
}
return result;
    }

   protected void sayIt(String str) throws IOException {
       out.write(str);
       out.flush();
   }

   static String getMsg(String str) {
       String[] items = str.split(":");
       // System.out.println(items.length);
       if(items.length > 2)
           return items[2];
       else
           return "";
   }

    protected String getUser(String user) {
       String[] items = user.split("!");
       return items[0].replace(":", "");
    }

   protected JBot() {
      System.out.println("Initializing.");
      this.server("irc.evilzone.org");
      this.port(6667);
      this.nick("jBOT-" + r);
      this.user("jBOT");
      this.name("jBOT");
      this.channel("#evilzone");
      this.owner("Owner");
   }

   private Socket socket;
   private BufferedReader in;
   private BufferedWriter out;

   protected void start() throws java.io.IOException {
      this.socket = new Socket(this.server(), this.port());
      this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
      if (socket.isConnected()) {
         out.write("NICK " + this.nick() + "\r\n");
         out.write("USER " + this.user() + " \"\" \"\" :" + this.name() + "\r\n");

         this.isActive(true);
         System.out.println("Starting thread.");
         new Thread(this).start();
out.write("JOIN " + this.channel() +  " 456\r\n");
out.flush();
      }
   }

    public void run() {
      String buffer;
        try {
            out.write("PRIVMSG " + this.channel() + " Greetingz!\r\n");
            out.flush();
        } catch (IOException ex) {

        }

      while (this.isActive()) {
         try {
            while ((buffer = in.readLine()) != null) {
                System.out.println(buffer);
                String sResponse = findMatch(getMsg(buffer));
                if(sResponse.length() != 0) {
                    sayIt("PRIVMSG " + this.channel() + " " + sResponse +"\r\n");
                }
               
               if (buffer.startsWith("PING")) {
                  sayIt("PONG " + buffer.substring(5) + "\r\n");
                }
               if (buffer.contains("GiveOP")) {
                   String usr[] = buffer.split(" ");
                   if(usr.length >=4 ) {
                      try {
                          sayIt("MODE " + this.channel() + " +o "+ usr[4] + "\r\n");
                      } catch (Exception e) {
                          System.out.println(e.getMessage());
                      }
                  }else {
                       sayIt("MODE " + this.channel() + " +o "+ this.owner() + "\r\n");
                  }
                 
                }

               if (buffer.contains("kiss") ) {
                  sayIt("PRIVMSG " + this.channel() + " :* :* :* \r\n");
               }

                if (buffer.contains("thank") || buffer.contains("Thank") ) {
                  sayIt("PRIVMSG " + this.channel() + " You\tare\twelcome\t" + getUser(buffer) + "!\r\n");
               }

                 if (buffer.contains("KICK " + this.channel() + " " + this.nick())) {
                  sayIt("JOIN " + this.channel() +  " 456 \r\n");
                  sayIt("PRIVMSG " + this.channel() + " \u0002You\tcan't\tkick\tme! \r\n");
               }

               if (buffer.contains("JOIN :" + this.channel()) && !buffer.contains(this.nick)) {
                  sayIt("PRIVMSG " + this.channel() + " Welcome\t" + getUser(buffer) + " \r\n");
               }
 
            }
 
} catch (java.io.IOException e) {
             System.out.println(e.getStackTrace());
        }
      }
   }
}
- People Hate BlackHats , But They Ignore WhiteHats.
                                                                                 - NeX

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Java IRC Bot
« Reply #1 on: January 16, 2011, 02:18:15 pm »
The return of jbot ?? ? haha ^-^.

Good work, i'm learning java right now, however its just another langauge similar to c++ etc.
Will try it out, get some cool features in it like a trivia quiz :)
~Factionwars