Search in sources :

Example 1 with Command

use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.

the class Weather method initHook.

@Override
protected void initHook() {
    local_command = new Command("weather", new CommandArgumentParser(1, new CommandArgument(ArgumentTypes.STRING, "Location"))) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
            Helper.sendMessage(target, getWeather(this.argumentParser.getArgument("Location")));
            return new CommandChainStateObject();
        }
    };
    local_command.registerAlias("w");
    local_command.setHelpText("Returns weather data for the supplied postal code, or Place name");
    IRCBot.registerCommand(local_command);
}
Also used : Command(pcl.lc.irc.entryClasses.Command) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CommandArgument(pcl.lc.irc.entryClasses.CommandArgument) CommandArgumentParser(pcl.lc.irc.entryClasses.CommandArgumentParser) CommandChainStateObject(pcl.lc.utils.CommandChainStateObject) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent) SAXException(org.xml.sax.SAXException)

Example 2 with Command

use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.

the class WhoPinged method initHook.

@Override
protected void initHook() {
    Database.addStatement("CREATE TABLE IF NOT EXISTS Pings(id INTEGER PRIMARY KEY, whowaspinged, whopinged, message, time, channel)");
    Database.addPreparedStatement("addPing", "INSERT INTO Pings(whowaspinged, whopinged, message, time, channel) VALUES (?,?,?,?,?);");
    Database.addPreparedStatement("getPings", "SELECT id, whopinged, message, time, channel FROM Pings WHERE LOWER(whowaspinged) = ?;");
    Database.addPreparedStatement("getAllPings", "SELECT id, time FROM Pings;");
    Database.addPreparedStatement("delPings", "DELETE FROM Pings WHERE id = ?;");
    command_WhoPinged = new Command("whopinged") {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
            try {
                PreparedStatement statement = Database.getPreparedStatement("getPings");
                statement.setString(1, nick.toLowerCase());
                ResultSet results = statement.executeQuery();
                if (Config.httpdEnable.equals("true") && results.next()) {
                    Helper.sendMessage(target, "Who Pinged you: " + httpd.getBaseDomain() + "/whopinged?nick=" + nick, nick);
                } else if (!results.next()) {
                    Helper.sendMessage(target, "No pings! :(");
                }
            } catch (Exception e) {
                Helper.sendMessage(target, "Error: " + e.getClass() + " " + e.getMessage());
                e.printStackTrace();
            }
            return new CommandChainStateObject();
        }
    };
    command_WhoPinged.setHelpText("Shows you the last pings you had.");
    command_ClearPings = new Command("clearpings") {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
            try {
                PreparedStatement statement = Database.getPreparedStatement("delPings");
                statement.setString(1, nick);
                statement.execute();
                Helper.sendMessage(target, "Ok");
            } catch (Exception e) {
                Helper.sendMessage(target, "Error: " + e.getClass() + " " + e.getMessage());
                e.printStackTrace();
            }
            return new CommandChainStateObject();
        }
    };
    command_ClearPings.setHelpText("Clears your pings from the DB");
    IRCBot.registerCommand(command_WhoPinged);
    IRCBot.registerCommand(command_ClearPings);
    ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
    executor = ses.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                long epoch = System.currentTimeMillis();
                PreparedStatement getPings = Database.getPreparedStatement("getAllPings");
                ResultSet results = getPings.executeQuery();
                while (results.next()) {
                    if ((results.getLong(2) + 259200000) <= epoch) {
                        PreparedStatement delPings = Database.getPreparedStatement("delPings");
                        delPings.setLong(1, results.getLong(1));
                        delPings.execute();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1, TimeUnit.SECONDS);
    httpd.registerContext("/whopinged", new WhoPingedHandler(), "WhoPinged");
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Command(pcl.lc.irc.entryClasses.Command) ResultSet(java.sql.ResultSet) CommandChainStateObject(pcl.lc.utils.CommandChainStateObject) PreparedStatement(java.sql.PreparedStatement) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent) IOException(java.io.IOException)

Example 3 with Command

use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.

the class Exclamation method initCommands.

private void initCommands() {
    CommandRateLimit limit = new CommandRateLimit(0, 5, 0, true, false);
    command_curse_word = new Command("curseword", limit) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
            Helper.sendMessage(target, getRandomExpression(TypeFilter.CURSE_WORD) + TablesOfRandomThings.getRandomExclamations(true, false), nick);
            return new CommandChainStateObject();
        }
    };
    command_curse_word.registerAlias("curses");
    command_curse_word.registerAlias("cursewd");
    command_curse_word.registerAlias("cursew");
    command_curse_word.registerAlias("swear");
    command_curse_word.registerAlias("swearword");
    command_curse_word.setHelpText("Holy manbats Batman!");
    command_exclamation = new Command("exclamation", limit) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
            Helper.sendMessage(target, getRandomExpression(TypeFilter.ALL) + TablesOfRandomThings.getRandomExclamations(true, false), nick);
            return new CommandChainStateObject();
        }
    };
    command_exclamation.registerAlias("excl");
    command_exclamation.setHelpText("Wot in tarnation?!");
}
Also used : Command(pcl.lc.irc.entryClasses.Command) CommandRateLimit(pcl.lc.irc.entryClasses.CommandRateLimit) CommandChainStateObject(pcl.lc.utils.CommandChainStateObject) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent)

Example 4 with Command

use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.

the class GenericEventListener method onGenericMessage.

@Override
public void onGenericMessage(final GenericMessageEvent event) {
    if (event.getUser() == null) {
        return;
    }
    for (String str : Config.ignoreMessagesEndingWith) {
        if (event.getMessage().endsWith(str)) {
            System.out.println("Ignored '" + event.getMessage() + "' because it ends with '" + str + "'");
            return;
        }
    }
    String user;
    String callingRelay = null;
    String bracketsPre = "";
    String bracketsPost = "";
    for (String brackets : Config.overBridgeUsernameBrackets) {
        bracketsPre += "\\" + brackets.substring(0, 1);
        bracketsPost += "\\" + brackets.substring(1, 2);
    }
    Pattern pattern = Pattern.compile("[" + bracketsPre + "](.*)[" + bracketsPost + "] ");
    Matcher matcher = pattern.matcher(event.getMessage());
    if (Config.parseBridgeCommandsFromUsers.contains(event.getUser().getNick()) && matcher.find()) {
        user = Helper.cleanNick(matcher.group(1));
        callingRelay = event.getUser().getNick();
    } else {
        user = event.getUser().getNick();
    }
    ArrayList<ArrayList<String>> commands = CommandHelper.findCommandInString(event.getMessage());
    for (ArrayList<String> thisCmd : commands) {
        try {
            String command = thisCmd.get(0).toLowerCase();
            String actualCommand = command.replaceFirst("\\" + Config.commandprefix, "");
            ;
            String[] params = new String[] {};
            if (thisCmd.size() > 1)
                params = thisCmd.subList(1, thisCmd.size()).toArray(new String[] {});
            // System.out.println("CMD: " + command + ", params: " + String.join(";", params));
            if (IRCBot.commands.containsKey(actualCommand)) {
                Command cmd = IRCBot.commands.get(actualCommand);
                cmd.callingRelay = callingRelay;
                String target = Helper.getTarget(event);
                CommandChainStateObject stateObj = cmd.tryExecute(command, user, target, event, params);
                System.out.println("Executed command '" + cmd.getCommand() + "': " + stateObj.state + (stateObj.msg != null ? " => '" + stateObj.msg + "'" : ""));
                DbStatCounter.Increment("commands", cmd.getCommand());
            } else if (IRCBot.dynamicCommands.containsKey(actualCommand)) {
                DynamicCommand cmd = IRCBot.dynamicCommands.get(actualCommand);
                cmd.callingRelay = callingRelay;
                String target = Helper.getTarget(event);
                CommandChainStateObject stateObj = cmd.tryExecute(command, user, target, event, params);
                System.out.println("Executed dynamic command '" + cmd.getCommand() + "': " + stateObj.state + (stateObj.msg != null ? " => '" + stateObj.msg + "'" : ""));
                DbStatCounter.Increment("commands_dynamic", cmd.getCommand());
            } else {
                System.out.println("No command '" + actualCommand + "'");
            }
        } catch (Exception e) {
            System.out.println("Command exception!");
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            String pasteURL = PasteUtils.paste(sw.toString(), PasteUtils.Formats.NONE);
            Helper.sendMessage(Helper.getTarget(event), "I had an exception... ow. Here's the stacktrace: " + pasteURL);
            e.printStackTrace();
        }
    }
    super.onGenericMessage(event);
    if (!(event instanceof MessageEvent))
        IRCBot.log.info("<-- Query: " + event.getUser().getNick() + ": " + event.getMessage());
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent) ArrayList(java.util.ArrayList) DynamicCommand(pcl.lc.irc.entryClasses.DynamicCommand) StringWriter(java.io.StringWriter) DynamicCommand(pcl.lc.irc.entryClasses.DynamicCommand) Command(pcl.lc.irc.entryClasses.Command) PrintWriter(java.io.PrintWriter)

Example 5 with Command

use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.

the class Help method getHelpRows.

public static String getHelpRows(String permFilter) {
    String items = "";
    try {
        items = "";
        includedCommands.clear();
        for (Map.Entry<String, Command> entry : IRCBot.commands.entrySet()) {
            Command command = entry.getValue();
            String item = getHelpRow(command, permFilter);
            if (!item.equals(""))
                includedCommands.add(command.getCommand());
            items += item;
        }
        if (IRCBot.dynamicCommands.size() > 0)
            items += "<tr><th colspan='3'>Dynamic commands</th></tr>";
        for (Map.Entry<String, DynamicCommand> entry : IRCBot.dynamicCommands.entrySet()) {
            DynamicCommand command = entry.getValue();
            String item = getHelpRow(command, permFilter);
            if (!item.equals(""))
                includedCommands.add(command.getCommand());
            items += item;
        }
        items = StringUtils.strip(items, "\n");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return items;
}
Also used : DynamicCommand(pcl.lc.irc.entryClasses.DynamicCommand) DynamicCommand(pcl.lc.irc.entryClasses.DynamicCommand) Command(pcl.lc.irc.entryClasses.Command) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Command (pcl.lc.irc.entryClasses.Command)54 GenericMessageEvent (org.pircbotx.hooks.types.GenericMessageEvent)51 CommandChainStateObject (pcl.lc.utils.CommandChainStateObject)42 CommandArgument (pcl.lc.irc.entryClasses.CommandArgument)34 CommandArgumentParser (pcl.lc.irc.entryClasses.CommandArgumentParser)34 ResultSet (java.sql.ResultSet)10 ArrayList (java.util.ArrayList)10 PreparedStatement (java.sql.PreparedStatement)9 IOException (java.io.IOException)6 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4 CommandRateLimit (pcl.lc.irc.entryClasses.CommandRateLimit)4 DynamicCommand (pcl.lc.irc.entryClasses.DynamicCommand)4 SQLException (java.sql.SQLException)3 DecimalFormat (java.text.DecimalFormat)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 JSONException (org.json.JSONException)3 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 HashMap (java.util.HashMap)2