Search in sources :

Example 11 with Command

use of commands.model.Command in project KaellyBot by Kaysoro.

the class AdminCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        String prefixe = getPrefixMdEscaped(message);
        Language lg = Translator.getLanguageFrom(message.getChannel());
        Matcher m = getMatcher(message);
        m.find();
        StringBuilder st = new StringBuilder();
        boolean argumentFound = m.group(1) != null && m.group(1).replaceAll("^\\s+", "").length() > 0;
        for (Command command : CommandManager.getCommands()) if (command.isAdmin()) {
            if (!argumentFound)
                st.append(command.help(lg, prefixe)).append("\n");
            else if (command.getName().equals(m.group(1).trim())) {
                st.append(command.helpDetailed(lg, prefixe));
                break;
            }
        }
        if (argumentFound && st.length() == 0)
            notFound.throwException(message, this, lg);
        else
            Message.sendText(message.getChannel(), st.toString());
        return true;
    }
    return false;
}
Also used : Language(enums.Language) Matcher(java.util.regex.Matcher) AbstractCommand(commands.model.AbstractCommand) Command(commands.model.Command)

Example 12 with Command

use of commands.model.Command in project KaellyBot by Kaysoro.

the class AvailableCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Matcher m = getMatcher(message);
        m.find();
        Language lg = Translator.getLanguageFrom(message.getChannel());
        List<Command> potentialCmds = new ArrayList<>();
        String commandName = m.group(1).trim();
        for (Command command : CommandManager.getCommands()) if (!command.isAdmin() && command.getName().contains(commandName))
            potentialCmds.add(command);
        if (potentialCmds.size() == 1) {
            Command command = potentialCmds.get(0);
            String value = m.group(2);
            if (command instanceof AvailableCommand || command instanceof AboutCommand) {
                Message.sendText(message.getChannel(), Translator.getLabel(lg, "announce.request.1"));
                return false;
            }
            if (value.matches("false") || value.matches("1") || value.matches("off")) {
                if (command.isPublic()) {
                    command.setPublic(false);
                    Message.sendText(message.getChannel(), Translator.getLabel(lg, "announce.request.2") + " *" + commandName + "* " + Translator.getLabel(lg, "announce.request.3"));
                } else
                    forbiddenCmdFound.throwException(message, this, lg);
            } else if (value.matches("true") || value.matches("0") || value.matches("on")) {
                if (!command.isPublic()) {
                    command.setPublic(true);
                    Message.sendText(message.getChannel(), Translator.getLabel(lg, "announce.request.2") + "*" + commandName + "* " + Translator.getLabel(lg, "announce.request.4"));
                } else
                    forbiddenCmdNotFound.throwException(message, this, lg);
            } else
                new BadUseCommandDiscordException().throwException(message, this, lg);
        } else if (potentialCmds.isEmpty())
            notFoundCmd.throwException(message, this, lg);
        else
            tooMuchCmds.throwException(message, this, lg);
    }
    return false;
}
Also used : AboutCommand(commands.classic.AboutCommand) Language(enums.Language) Matcher(java.util.regex.Matcher) AbstractCommand(commands.model.AbstractCommand) AboutCommand(commands.classic.AboutCommand) Command(commands.model.Command) ArrayList(java.util.ArrayList)

Example 13 with Command

use of commands.model.Command in project KaellyBot by Kaysoro.

the class CommandCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Language lg = Translator.getLanguageFrom(message.getChannel());
        if (isUserHasEnoughRights(message)) {
            Guild guild = Guild.getGuild(message.getGuild());
            Matcher m = getMatcher(message);
            m.find();
            List<Command> potentialCmds = new ArrayList<>();
            String commandName = m.group(1).trim();
            for (Command command : CommandManager.getCommands()) if (command.isPublic() && !command.isAdmin() && command.getName().contains(commandName))
                potentialCmds.add(command);
            if (potentialCmds.size() == 1) {
                Command command = potentialCmds.get(0);
                String value = m.group(2);
                if (command instanceof CommandCommand) {
                    Message.sendText(message.getChannel(), Translator.getLabel(lg, "command.request.1"));
                    return false;
                }
                if (value.matches("false") || value.matches("1") || value.matches("off")) {
                    if (!guild.getForbiddenCommands().containsKey(command.getName())) {
                        new CommandForbidden(command, guild).addToDatabase();
                        Message.sendText(message.getChannel(), Translator.getLabel(lg, "command.request.2") + " *" + commandName + "* " + Translator.getLabel(lg, "command.request.3"));
                    } else
                        forbiddenCmdFound.throwException(message, this, lg);
                } else if (value.matches("true") || value.matches("0") || value.matches("on")) {
                    if (guild.getForbiddenCommands().containsKey(command.getName())) {
                        guild.getForbiddenCommands().get(command.getName()).removeToDatabase();
                        Message.sendText(message.getChannel(), Translator.getLabel(lg, "command.request.2") + " *" + commandName + "* " + Translator.getLabel(lg, "command.request.4"));
                    } else
                        forbiddenCmdNotFound.throwException(message, this, lg);
                } else
                    new BadUseCommandDiscordException().throwException(message, this, lg);
            } else if (potentialCmds.isEmpty())
                notFoundCmd.throwException(message, this, lg);
            else
                tooMuchCmds.throwException(message, this, lg);
        } else
            noEnoughRights.throwException(message, this, lg);
    }
    return false;
}
Also used : CommandForbidden(data.CommandForbidden) Language(enums.Language) Matcher(java.util.regex.Matcher) AbstractCommand(commands.model.AbstractCommand) Command(commands.model.Command) ArrayList(java.util.ArrayList) Guild(data.Guild)

Example 14 with Command

use of commands.model.Command in project KaellyBot by Kaysoro.

the class CommandPatternTest method testAlmanaxCommand.

// BASIC COMMANDS
public void testAlmanaxCommand() {
    Command cmd = new AlmanaxCommand();
    Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$");
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax 20/02/2016").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax +9").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax true").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax on").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax 0").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax false").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax off").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "almanax 1").find());
}
Also used : Pattern(java.util.regex.Pattern) Command(commands.model.Command)

Example 15 with Command

use of commands.model.Command in project KaellyBot by Kaysoro.

the class CommandPatternTest method testAlignmentCommand.

public void testAlignmentCommand() {
    Command cmd = new AlignmentCommand();
    Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$");
    assertTrue(pattern.matcher(Constants.prefixCommand + "align").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "align bonta oeil 20").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "align bonta oeil 20 oto mustam").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "align -user 145618941615").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "align -user 145618941615 oto mustam").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "align -user 145618941615 bonta oeil 100").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "align -user 145618941615 bonta oeil 0 ilyzaelle").find());
    assertTrue(pattern.matcher(Constants.prefixCommand + "align > 20").find());
}
Also used : Pattern(java.util.regex.Pattern) Command(commands.model.Command)

Aggregations

Command (commands.model.Command)32 Pattern (java.util.regex.Pattern)28 AbstractCommand (commands.model.AbstractCommand)4 Language (enums.Language)4 Matcher (java.util.regex.Matcher)4 ArrayList (java.util.ArrayList)2 AboutCommand (commands.classic.AboutCommand)1 CommandForbidden (data.CommandForbidden)1 Guild (data.Guild)1