Search in sources :

Example 31 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class TutorialCommand method getListTutoFrom.

private List<Requestable> getListTutoFrom(String url, IMessage message) {
    List<Requestable> result = new ArrayList<>();
    Language lg = Translator.getLanguageFrom(message.getChannel());
    try {
        Document doc = JSoupManager.getDocument(url);
        Elements elems = doc.getElementById("wsite-search-list").getElementsByTag("a");
        for (Element element : elems) result.add(new Requestable(element.child(0).text(), element.attr("href")));
    } catch (IOException e) {
        ExceptionManager.manageIOException(e, message, this, lg, notFoundTuto);
        return new ArrayList<>();
    } catch (Exception e) {
        ExceptionManager.manageException(e, message, this, lg);
        return new ArrayList<>();
    }
    return result;
}
Also used : Language(enums.Language) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) NotFoundDiscordException(exceptions.NotFoundDiscordException) IOException(java.io.IOException) DiscordException(exceptions.DiscordException) TooMuchDiscordException(exceptions.TooMuchDiscordException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 32 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class TutorialCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Matcher m = getMatcher(message);
        m.find();
        Language lg = Translator.getLanguageFrom(message.getChannel());
        String normalName = Normalizer.normalize(m.group(1).trim(), Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
        String editedName = removeUselessWords(normalName);
        BestMatcher matcher = new BestMatcher(normalName);
        try {
            matcher.evaluateAll(getListTutoFrom(getSearchURL(editedName), message));
            if (// We have found it !
            matcher.isUnique())
                Message.sendText(message.getChannel(), Translator.getLabel(lg, "tutorial.request") + " " + Constants.dofusPourLesNoobURL + matcher.getBest().getUrl());
            else if (// Too much tutos
            !matcher.isEmpty())
                tooMuchTutos.throwException(message, this, lg, matcher.getBests());
            else
                // empty
                notFoundTuto.throwException(message, this, lg);
        } catch (IOException e) {
            ExceptionManager.manageIOException(e, message, this, lg, notFoundTuto);
        }
        return true;
    }
    return false;
}
Also used : Language(enums.Language) Matcher(java.util.regex.Matcher) IOException(java.io.IOException)

Example 33 with Language

use of enums.Language 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 34 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class PrefixCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Language lg = Translator.getLanguageFrom(message.getChannel());
        if (isUserHasEnoughRights(message)) {
            Matcher m = getMatcher(message);
            m.find();
            String newPrefix = m.group(1).trim();
            if (newPrefix.length() >= 1 && newPrefix.length() <= Constants.prefixeLimit) {
                Guild.getGuild(message.getGuild()).setPrefixe(newPrefix);
                Message.sendText(message.getChannel(), Translator.getLabel(lg, "prefix.request.1").replace("{prefix}", getPrefixMdEscaped(message)));
                try {
                    Message.sendText(message.getGuild().getOwner().getOrCreatePMChannel(), Translator.getLabel(lg, "prefix.request.2").replace("{prefix}", getPrefixMdEscaped(message)).replace("{guild.name}", message.getGuild().getName()));
                } catch (DiscordException e) {
                    LOG.warn("request", "Impossible de contacter l'administrateur de la guilde [" + message.getGuild().getName() + "].");
                }
                return true;
            } else
                prefixeOutOfBounds.throwException(message, this, lg);
        } else
            noEnoughRights.throwException(message, this, lg);
    }
    return false;
}
Also used : Language(enums.Language) Matcher(java.util.regex.Matcher) AdvancedDiscordException(exceptions.AdvancedDiscordException) BasicDiscordException(exceptions.BasicDiscordException) DiscordException(sx.blah.discord.util.DiscordException)

Example 35 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class ServerCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Guild guild = Guild.getGuild(message.getGuild());
        Language lg = Translator.getLanguageFrom(message.getChannel());
        Matcher m = getMatcher(message);
        m.find();
        if (m.group(1) != null)
            if (isUserHasEnoughRights(message)) {
                String serverName = m.group(1).toLowerCase().trim();
                if (!serverName.equals("-reset")) {
                    serverName = Normalizer.normalize(serverName, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").replaceAll("\\W+", "").trim();
                    List<ServerDofus> result = new ArrayList<>();
                    for (ServerDofus server : ServerDofus.getServersDofus()) if (Normalizer.normalize(server.getName(), Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").replaceAll("\\W+", "").toLowerCase().trim().startsWith(serverName))
                        result.add(server);
                    if (result.size() == 1) {
                        if (guild.getServerDofus() != null && guild.getServerDofus() != result.get(0))
                            guild.resetPortals();
                        guild.setServer(result.get(0));
                        guild.mergePortals(result.get(0).getSweetPortals());
                        Message.sendText(message.getChannel(), Translator.getLabel(lg, "server.request.1").replace("{game}", Constants.game) + " " + guild.getName() + " " + Translator.getLabel(lg, "server.request.2") + " " + result.get(0).getName() + ".");
                    } else if (result.isEmpty())
                        notFoundServer.throwException(message, this, lg);
                    else
                        tooMuchServers.throwException(message, this, lg, result);
                } else {
                    guild.setServer(null);
                    Message.sendText(message.getChannel(), guild.getName() + " " + Translator.getLabel(lg, "server.request.3").replace("{game}", Constants.game));
                }
            } else
                noEnoughRights.throwException(message, this, lg);
        else {
            if (guild.getServerDofus() != null)
                Message.sendText(message.getChannel(), guild.getName() + " " + Translator.getLabel(lg, "server.request.4") + " " + guild.getServerDofus().getName() + ".");
            else
                Message.sendText(message.getChannel(), guild.getName() + " " + Translator.getLabel(lg, "server.request.5").replace("{game}", Constants.game));
        }
    }
    return false;
}
Also used : Language(enums.Language) Matcher(java.util.regex.Matcher) ServerDofus(data.ServerDofus) ArrayList(java.util.ArrayList) List(java.util.List) Guild(data.Guild)

Aggregations

Language (enums.Language)43 Matcher (java.util.regex.Matcher)27 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 ChannelLanguage (data.ChannelLanguage)6 AbstractCommand (commands.model.AbstractCommand)4 Command (commands.model.Command)4 Guild (data.Guild)4 ServerDofus (data.ServerDofus)4 BasicDiscordException (exceptions.BasicDiscordException)3 DiscordException (exceptions.DiscordException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SQLException (java.sql.SQLException)3 List (java.util.List)3 Document (org.jsoup.nodes.Document)3 Element (org.jsoup.nodes.Element)3 Elements (org.jsoup.select.Elements)3 Almanax (data.Almanax)2 Embedded (data.Embedded)2 BadUseCommandDiscordException (exceptions.BadUseCommandDiscordException)2