Search in sources :

Example 1 with GenericMessageEvent

use of org.pircbotx.hooks.types.GenericMessageEvent in project LanteaBot by PC-Logix.

the class Translator method initCommands.

private void initCommands() {
    local_command = new Command("translate", new CommandArgumentParser(1, new CommandArgument(ArgumentTypes.STRING, "Text"), new CommandArgument(ArgumentTypes.STRING, "FromLanguage", "If this argument is omitted the API will try to determine it automatically."), new CommandArgument(ArgumentTypes.STRING, "ToLanguage", "If this argument is omitted the API will try to determine it automatically.")), new CommandRateLimit(5)) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
            String str = this.argumentParser.getArgument("Text");
            String from = this.argumentParser.getArgument("FromLanguage");
            String to = this.argumentParser.getArgument("ToLanguage");
            if (to == null || to.equals(""))
                to = "auto";
            if (from == null || from.equals(""))
                from = "auto";
            Helper.sendMessage(target, doTranslate(from, to, str));
            return new CommandChainStateObject(CommandChainState.FINISHED);
        }
    };
    local_command.registerAlias("t");
}
Also used : CommandChainStateObject(pcl.lc.utils.CommandChainStateObject) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent)

Example 2 with GenericMessageEvent

use of org.pircbotx.hooks.types.GenericMessageEvent 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 3 with GenericMessageEvent

use of org.pircbotx.hooks.types.GenericMessageEvent 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 4 with GenericMessageEvent

use of org.pircbotx.hooks.types.GenericMessageEvent 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 5 with GenericMessageEvent

use of org.pircbotx.hooks.types.GenericMessageEvent 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)

Aggregations

GenericMessageEvent (org.pircbotx.hooks.types.GenericMessageEvent)73 CommandChainStateObject (pcl.lc.utils.CommandChainStateObject)58 Command (pcl.lc.irc.entryClasses.Command)51 CommandArgument (pcl.lc.irc.entryClasses.CommandArgument)34 CommandArgumentParser (pcl.lc.irc.entryClasses.CommandArgumentParser)34 PreparedStatement (java.sql.PreparedStatement)15 ResultSet (java.sql.ResultSet)15 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 MessageEvent (org.pircbotx.hooks.events.MessageEvent)5 PrivateMessageEvent (org.pircbotx.hooks.events.PrivateMessageEvent)5 SQLException (java.sql.SQLException)4 DecimalFormat (java.text.DecimalFormat)4 CommandRateLimit (pcl.lc.irc.entryClasses.CommandRateLimit)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 JSONException (org.json.JSONException)3 User (org.pircbotx.User)3 InetAddress (java.net.InetAddress)2