Welcome to my site... Have a nice day...

Friday, 6 September 2013

[Java] Writing a IRC Bot


Hi! bro's and sis's..

today i will publish whit you a bot code Big Grin


First need a java library Smile)

i am using pircbot library for coding a irc bot!

Pircbot Library Home Page:http://www.jibble.org/pircbot.php
Pircbot Library Download Link:http://www.jibble.org/files/pircbot-1.5.0.zip

Download this library and open a project in netbeans.
[Image: iam8.png]

add library
[Image: H39adgt.png]

select your pircbot.jar file.

and open your main java file.


Codes:

Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package botexample;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;
import org.jibble.pircbot.PircBot;

/**
*
* @author Murat
*/
public class BotExample extends PircBot {
    public BotExample() {
        this.setName("YourBotName");
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         // genereate bot
        BotExample bot = new BotExample();
    
        // Enable debugging output.
        bot.setVerbose(true);
        try {
            // server name for connect
            bot.connect("irc.rizon.net");
        } catch (IOException ex) {
            Logger.getLogger(BotExample.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IrcException ex) {
            Logger.getLogger(BotExample.class.getName()).log(Level.SEVERE, null, ex);
        }

        // channel name for connect
        bot.joinChannel("#channel");
    }

    @Override
    protected void onMessage(String channel, String sender, String login, String hostname, String message) {
        super.onMessage(channel, sender, login, hostname, message); //To change body of generated methods, choose Tools | Templates.

        //if msj= hi bot answer hi!+his nick
        if(message.equalsIgnoreCase("Hi"))
            sendMessage(channel, "Hi!"+sender);
        else if(message.startsWith(".kick") && sender.equalsIgnoreCase("Kacak41"))
        {
            String[] msg=message.split(" ");
            kick(channel, msg[1], "Kicked by Kacak41 request!");
            //first channel = which channel kicked user, msg[1] his nick 3. items= reason
        }
    }

    @Override
    protected void onJoin(String channel, String sender, String login, String hostname) {
        super.onJoin(channel, sender, login, hostname); //To change body of generated methods, choose Tools | Templates.
    sendMessage(channel, sender+" Welcome to Channel:"+channel);
    //sending welcome msj
    }

  

}



Screenshot:
[Image: NrMzsAS.png]

1 comment:

  1. Its really helpful for me to understand and apply this for my future use. Thanks.
    If anyone wants to Learn Java in Chennai go to the Besant Technologies which is No.1 Training Institute in Chennai.

    http://www.javatraininginchennai.in
    http://www.javatrainingchennai.in
    http://www.besanttechnologies.com/training-courses/java-training
    http://www.besanttechnologies.in/java-training-in-chennai.html
    http://www.besanttech.com/best-java-j2ee-training-institute-in-chennai.html

    ReplyDelete