Search in sources :

Example 1 with DynamicCommand

use of pcl.lc.irc.entryClasses.DynamicCommand 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 2 with DynamicCommand

use of pcl.lc.irc.entryClasses.DynamicCommand 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)

Example 3 with DynamicCommand

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

the class Help method getHelpRow.

public static String getHelpRow(Command command, String permFilter) {
    if (includedCommands.contains(command.getCommand()))
        return "";
    int permLevelFilter = Permissions.getPermLevel(permFilter);
    int permLevelCommand = Permissions.getPermLevel(command.getPermissionLevel());
    if (permLevelFilter < permLevelCommand)
        return "";
    String item = "";
    String help = StringEscapeUtils.escapeHtml4(command.getHelpText());
    if (command instanceof DynamicCommand && help.equals(DynamicCommands.defaultHelpText)) {
        help = "<span class='fad'>No description, raw command:</span> " + ((DynamicCommand) command).content;
    } else if (help.isEmpty()) {
        help = "<span class='fad'>No help text set for this command.</span>";
    }
    ArrayList<String> extraInfoEntries = new ArrayList<>();
    if (Permissions.getPermLevel(command.getPermissionLevel()) > 0)
        extraInfoEntries.add("Permission: " + command.getPermissionLevel());
    else
        extraInfoEntries.add("Permission: Anyone");
    if (command.getRateLimit() != null)
        extraInfoEntries.add("Cooldown: " + Helper.timeString(Helper.parseSeconds(command.getRateLimit().getLimit())));
    String extraInfo = "<div class='fad'>" + String.join(" | ", extraInfoEntries) + "</div>";
    String argumentSyntax = "";
    if (command.argumentParser != null) {
        argumentSyntax = "<div class='fad'>Argument" + (command.argumentParser.argumentCount == 1 ? "" : "s") + ": " + command.argumentParser.getArgumentSyntax(true) + "</div>";
    }
    item += "<tr><td style='white-space: nowrap;'>" + Config.commandprefix + command.getCommand() + "</td><td>" + help + argumentSyntax + extraInfo + "</td><td style='white-space: nowrap;'>" + String.join("<br/>", command.getAliasesDisplay()) + "</td></tr>";
    int i = 0;
    ArrayList<Command> printableSubCommands = new ArrayList<>();
    for (Command subCommand : command.getSubCommands()) {
        int permLevelSubCommand = Permissions.getPermLevel(subCommand.getPermissionLevel());
        if (permLevelFilter >= permLevelSubCommand)
            printableSubCommands.add(subCommand);
    }
    for (Command subCommand : printableSubCommands) {
        String character = "├";
        if (i == (printableSubCommands.size() - 1)) {
            character = "└";
        }
        String subHelp = StringEscapeUtils.escapeHtml4(subCommand.getHelpText());
        if (subHelp.isEmpty())
            subHelp = "<span class='fad'>No help text set for this sub-command.</span>";
        extraInfoEntries = new ArrayList<>();
        if (Permissions.getPermLevel(subCommand.getPermissionLevel()) > 0)
            extraInfoEntries.add("Permission: " + subCommand.getPermissionLevel());
        else if (Permissions.getPermLevel(command.getPermissionLevel()) > 0)
            extraInfoEntries.add("Permission: " + command.getPermissionLevel());
        else
            extraInfoEntries.add("Permission: Anyone");
        if (command.getRateLimit() != null)
            extraInfoEntries.add("Cooldown: " + Helper.timeString(Helper.parseSeconds(command.getRateLimit().getLimit())));
        extraInfo = "<div class='fad'>" + String.join(" | ", extraInfoEntries) + "</div>";
        String subArgumentSyntax = "";
        if (subCommand.argumentParser != null)
            subArgumentSyntax = "<div class='fad'>Argument" + (subCommand.argumentParser.argumentCount == 1 ? "" : "s") + ": " + StringEscapeUtils.escapeHtml4(subCommand.argumentParser.getArgumentSyntax(true)) + "</div>";
        item += "<tr><td style='white-space: nowrap;'> " + character + " " + subCommand.getCommand() + "</td><td>" + subHelp + subArgumentSyntax + extraInfo + "</td><td style='white-space: nowrap;'>" + String.join("<br/>", subCommand.getAliasesDisplay()) + "</td></tr>";
        i++;
    }
    return item;
}
Also used : DynamicCommand(pcl.lc.irc.entryClasses.DynamicCommand) DynamicCommand(pcl.lc.irc.entryClasses.DynamicCommand) Command(pcl.lc.irc.entryClasses.Command) ArrayList(java.util.ArrayList)

Aggregations

Command (pcl.lc.irc.entryClasses.Command)3 DynamicCommand (pcl.lc.irc.entryClasses.DynamicCommand)3 ArrayList (java.util.ArrayList)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 GenericMessageEvent (org.pircbotx.hooks.types.GenericMessageEvent)1