All pastes #1683288 Raw Edit

nBot.java

public java v1 · immutable
#1683288 ·published 2009-11-23 05:28 UTC
rendered paste body
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package ircbot;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import org.jibble.pircbot.IrcException;import org.jibble.pircbot.PircBot;import org.jibble.pircbot.User;/** * * @author Admin */public class nBot extends PircBot {    /**     * Main method so we can run the program     * @param args Arguments from the executer     * @throws InterruptedException Thrown if Main thread if interrupted while executting the Thread.sleep     */    public static void main(String[] args) throws InterruptedException {        nBot bot = new nBot("irc.swiftirc.net", 6667, new String[] { "#nBot" }, "nBot2", "rawawrawrawrawrawr", new String[] { "IFreaper", "AllThatRemains" });    }    /**     * The people on the channels that have rights to special commands.     */    private String[] owners;    /**     * Most primative constructor.     * @param server Server we will connect to.     * @param channels Channel(s) we will connect to. If multiple channels, they must be on the same server!     * @param nick The nickname of the bot.     * @param password The password to the nickname.     * @param owners The owner(s) of the channel, it can be one owner: new String[] { "IFreaper" }.     */    public nBot(String server, String[] channels, String nick, String password, String[] owners) {        this.owners = owners;        this.setName(nick);        try {            connect(server);            identify(password);        } catch (IOException ex) {        } catch (IrcException ex) {        }        for (int i = 0; i < channels.length; i++) {            joinChannel(channels[i]);        }    }    /**     * Second-most primative constructor.     * @param server Server we will connect to.     * @param serverport Port of the server we will connect to.     * @param channels Channel(s) we will connect to. If multiple channels, they must be on the same server!     * @param nick The nickname of the bot.     * @param password The password to the nickname.     */    public nBot(String server, int serverport, String[] channels, String nick, String password, String[] owners) {        this.owners = owners;        this.setName(nick);        try {            connect(server, serverport);            identify(password);        } catch (IOException ex) {        } catch (IrcException ex) {        }        for (int i = 0; i < channels.length; i++) {            joinChannel(channels[i]);        }    }    /**     * Called when we are invited to a channel by a user.     * @param targetNick The nick of the user being invited - should be us!     * @param sourceNick The nick of the user that sent the invitation.     * @param sourceLogin The login of the user that sent the invitation.     * @param sourceHostname The hostname of the user that sent the invitation.     * @param channel The channel that we're being invited to.     */    @Override    public void onInvite(String targetNick, String sourceNick, String sourceLogin, String sourceHostname, String channel) {        sendNotice(channel, sourceNick + " has invited " + targetNick + " to join " + channel + ".");        joinChannel(channel);    }    /**     * This method is called whenever a message is sent to a channel.     * @param channel Channel the message is received from.     * @param sender The nick of the person who sent the message.     * @param login The login of the person who sent the message.     * @param hostname The hostname of the person who sent the message.     * @param message The actual message sent to the channel.     */    @Override    public void onMessage(String channel, String sender, String login, String hostname, String message) {        if (message.equalsIgnoreCase("!info")) {            sendNotice(sender, "nBot programmed by I.F.reaper and [---Nick---]");        }        if (message.startsWith("!suggest")) {            String msg = message.replace("!suggest", sender + ":");            User[] users = this.getUsers(channel);            for (User i : users) {                for (String ii : owners) {                    if (i.getNick().equals(ii)) {                        sendMessage(i.getNick(), msg);                    }                }            }            try {                BufferedWriter bw = new BufferedWriter(new FileWriter("C:/SUGGESTIONS/suggestions.txt"));                bw.write(msg, 0, msg.length());                bw.newLine();                bw.flush();                bw.close();            } catch (IOException ex) {                ex.printStackTrace();            }        }        if (message.startsWith("!kick")) {            User bot = us(channel);            if (bot.isOp()) {                for (String ii : owners) {                    if (ii.equals(sender)) {                        String msg1 = message;                        String msg = msg1.replace("!kick", "");                        User[] users = getUsers(channel);                        for (int i = 0; i < users.length; i++) {                            if (users[i].getNick().equalsIgnoreCase(msg.trim()) && !users[i].isOp()) {                                kick(channel, users[i].getNick());                                break;                            }                        }                        break;                    }                }            } else {                sendNotice(sender, "This bot must have op to make this command work!");            }        }        if (message.startsWith("!ban")) {            User bot = us(channel);            if (bot.isOp()) {                for (String ii : owners) {                    if (ii.equals(sender)) {                        String msg1 = message;                        String msg = msg1.replace("!ban", "");                        User[] users = getUsers(channel);                        for (int i = 0; i < users.length; i++) {                            if (users[i].getNick().equalsIgnoreCase(msg.trim()) && !users[i].isOp()) {                                banUser(channel, users[i].getNick());                                break;                            }                        }                        break;                    }                }            } else {                sendNotice(sender, "This bot must have op to make this command work!");            }        }    }    /**     * This method is called whenever someone (possibly us) joins a channel which we are on.     * @param channel The channel which somebody joined.     * @param sender The nick of the user who joined the channel.     * @param login The login of the user who joined the channel.     * @param hostname The hostname of the user who joined the channel.     */    @Override    public void onJoin(String channel, String sender, String login, String hostname) {        sendNotice(sender, "Welcome " + sender + " to " + channel);    }    /**     * This method is called whenever someone (possibly us) is kicked from any of the channels that we are in.      * @param channel The channel from which the recipient was kicked.     * @param kickerNick The nick of the user who performed the kick.     * @param kickerLogin The login of the user who performed the kick.     * @param kickerHostname The hostname of the user who performed the kick.     * @param recipientNick The unfortunate recipient of the kick.     * @param reason The reason given by the user who performed the kick.     */    @Override    public void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason) {        if (recipientNick.equalsIgnoreCase(this.getNick())) {            joinChannel(channel);        }    }    /**     * This method is called whenever a private message is sent to the PircBot.     * @param sender The nick of the person who sent the private message.     * @param login The login of the person who sent the private message.     * @param hostname The hostname of the person who sent the private message.     * @param message The actual message.     */    @Override    public void onPrivateMessage(String sender, String login, String hostname, String message) {        sendNotice(sender, "Sorry, I am a bot and I can't respond to your message!");        sendNotice(sender, "If it was a command, type it in the main chat of this channel!");    }    /**     * Kicks and bans a specified nick on a specified nick.     * @param channel Channel to kick and ban the nick.     * @param nick Nick to kick and ban.     */    public void banUser(String channel, String nick) {        setMode("" + channel, "+b " + nick);        kick(channel, nick);    }    /**     * Returns the object instance of our nick.     * @param channel Channel this bot is in     * @return Returns null if we cannot be found, or returns our instance of the User object.     */    public User us(String channel) {        User[] people = this.getUsers(channel);        for (User u : people) {            if (u.getNick().equals(this.getNick())) {                return u;            }        }        return null;    }}