Search in sources :

Example 16 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class Message method sendEmbed.

public static void sendEmbed(IChannel channel, EmbedObject content) {
    Language lg = Translator.getLanguageFrom(channel);
    RequestBuffer.request(() -> {
        try {
            new MessageBuilder(ClientConfig.DISCORD()).withChannel(channel).withEmbed(content).build();
        } catch (RateLimitException e) {
            LoggerFactory.getLogger(Message.class).warn(e.getMessage(), e);
            throw e;
        } catch (DiscordException e) {
            Reporter.report(e, channel.isPrivate() ? null : channel.getGuild(), channel);
            LoggerFactory.getLogger(Message.class).error(e.getMessage(), e);
        } catch (MissingPermissionsException e) {
            LoggerFactory.getLogger(Message.class).warn(Constants.name + " n'a pas les permissions pour appliquer cette requête.");
            new MissingPermissionDiscordException().throwException(channel, lg, e);
        } catch (Exception e) {
            Reporter.report(e, channel.isPrivate() ? null : channel.getGuild(), channel);
            LoggerFactory.getLogger(Message.class).error(e.getMessage(), e);
        }
        return null;
    });
}
Also used : MissingPermissionDiscordException(exceptions.MissingPermissionDiscordException) Language(enums.Language) MissingPermissionDiscordException(exceptions.MissingPermissionDiscordException) MissingPermissionDiscordException(exceptions.MissingPermissionDiscordException)

Example 17 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class SetCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Matcher m = getMatcher(message);
        Language lg = Translator.getLanguageFrom(message.getChannel());
        m.find();
        if (message.getChannel().getModifiedPermissions(ClientConfig.DISCORD().getOurUser()).contains(Permissions.USE_EXTERNAL_EMOJIS) && ClientConfig.DISCORD().getOurUser().getPermissionsForGuild(message.getGuild()).contains(Permissions.USE_EXTERNAL_EMOJIS)) {
            String normalName = Normalizer.normalize(m.group(2).trim(), Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
            String editedName = removeUselessWords(lg, normalName);
            BestMatcher matcher = new BestMatcher(normalName);
            try {
                matcher.evaluateAll(getListRequestableFrom(getSearchURL(lg, editedName), message, notFoundSet));
                if (matcher.isUnique()) {
                    // We have found it !
                    Embedded set = Set.getSet(lg, Translator.getLabel(lg, "game.url") + matcher.getBest().getUrl());
                    if (m.group(1) != null)
                        Message.sendEmbed(message.getChannel(), set.getMoreEmbedObject(lg));
                    else
                        Message.sendEmbed(message.getChannel(), set.getEmbedObject(lg));
                } else if (// Too much sets
                !matcher.isEmpty())
                    tooMuchSets.throwException(message, this, lg, matcher.getBests());
                else
                    // empty
                    notFoundSet.throwException(message, this, lg);
            } catch (IOException e) {
                ExceptionManager.manageIOException(e, message, this, lg, notFoundSet);
            }
            return true;
        } else
            noExternalEmoji.throwException(message, this, lg);
    }
    return false;
}
Also used : Language(enums.Language) Matcher(java.util.regex.Matcher) IOException(java.io.IOException)

Example 18 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class WhoisCommand 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 pseudo = m.group(1).trim().toLowerCase();
        StringBuilder url;
        try {
            url = new StringBuilder(Translator.getLabel(lg, "game.url")).append(Translator.getLabel(lg, "character.url")).append("?").append(forPseudo).append(URLEncoder.encode(pseudo, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            ExceptionManager.manageException(e, message, this, lg);
            return false;
        }
        if (m.group(2) != null) {
            String serverName = m.group(2).trim().toLowerCase();
            List<ServerDofus> result = new ArrayList<>();
            for (ServerDofus server : ServerDofus.getServersDofus()) if (server.getName().toLowerCase().startsWith(serverName))
                result.add(server);
            if (result.size() == 1)
                url.append("&").append(forServer).append(result.get(0).getId());
            else {
                if (!result.isEmpty())
                    tooMuchServers.throwException(message, this, lg);
                else
                    notFoundServer.throwException(message, this, lg);
                return false;
            }
        }
        try {
            Document doc = JSoupManager.getDocument(url.toString());
            Elements elems = doc.getElementsByClass("ak-bg-odd");
            elems.addAll(doc.getElementsByClass("ak-bg-even"));
            if (!elems.isEmpty()) {
                // on boucle jusqu'à temps de trouver le bon personnage (ie le plus proche du nom donnée)
                List<String> result = new ArrayList<>();
                List<String> servers = new ArrayList<>();
                for (Element element : elems) if (pseudo.equals(element.child(1).text().trim().toLowerCase())) {
                    result.add(element.child(1).select("a").attr("href"));
                    servers.add(element.child(element.children().size() - 2).text());
                }
                if (result.size() == 1) {
                    Connection.Response response = JSoupManager.getResponse(Translator.getLabel(lg, "game.url") + result.get(0));
                    if (!response.url().getPath().endsWith(Translator.getLabel(lg, "whois.request"))) {
                        Character characPage = Character.getCharacter(Translator.getLabel(lg, "game.url") + result.get(0), lg);
                        Message.sendEmbed(message.getChannel(), characPage.getEmbedObject(lg));
                    } else
                        characterTooOld.throwException(message, this, lg);
                } else if (result.size() > 1)
                    tooMuchCharacters.throwException(message, this, lg, servers);
                else
                    notFoundCharacter.throwException(message, this, lg);
            } else
                notFoundCharacter.throwException(message, this, lg);
        } catch (IOException e) {
            ExceptionManager.manageIOException(e, message, this, lg, characterPageInaccessible);
        } catch (Exception e) {
            ExceptionManager.manageException(e, message, this, lg);
        }
    }
    return false;
}
Also used : Character(data.Character) Matcher(java.util.regex.Matcher) ServerDofus(data.ServerDofus) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Connection(org.jsoup.Connection) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IOException(java.io.IOException) Exception(java.lang.Exception) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Language(enums.Language)

Example 19 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class LanguageCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Language lg = Translator.getLanguageFrom(message.getChannel());
        Matcher m = getMatcher(message);
        m.find();
        if (m.group(2) != null) {
            // Ajouts
            if (isUserHasEnoughRights(message)) {
                List<Language> langs = new ArrayList<>();
                for (Language lang : Language.values()) if (m.group(2).trim().toUpperCase().equals(lang.getAbrev()))
                    langs.add(lang);
                if (langs.size() == 1) {
                    if (m.group(1) == null) {
                        Guild.getGuild(message.getGuild()).setLanguage(langs.get(0));
                        lg = langs.get(0);
                        Message.sendText(message.getChannel(), message.getGuild().getName() + " " + Translator.getLabel(lg, "lang.request.1") + " " + langs.get(0));
                    } else {
                        ChannelLanguage chan = ChannelLanguage.getChannelLanguages().get(message.getChannel().getLongID());
                        if (chan != null) {
                            if (chan.getLang().equals(langs.get(0))) {
                                chan.removeToDatabase();
                                lg = Translator.getLanguageFrom(message.getChannel());
                                Message.sendText(message.getChannel(), message.getChannel().getName() + " " + Translator.getLabel(lg, "lang.request.2") + " " + Guild.getGuild(message.getGuild()).getLanguage());
                            } else {
                                chan.setLanguage(langs.get(0));
                                lg = langs.get(0);
                                Message.sendText(message.getChannel(), message.getChannel().getName() + " " + Translator.getLabel(lg, "lang.request.1") + " " + chan.getLang());
                            }
                        } else {
                            chan = new ChannelLanguage(langs.get(0), message.getChannel().getLongID());
                            chan.addToDatabase();
                            lg = langs.get(0);
                            Message.sendText(message.getChannel(), message.getChannel().getName() + " " + Translator.getLabel(lg, "lang.request.1") + " " + chan.getLang());
                        }
                    }
                } else if (langs.isEmpty())
                    notFoundLang.throwException(message, this, lg);
                else
                    tooMuchLangs.throwException(message, this, lg);
            } else {
                noEnoughRights.throwException(message, this, lg);
                return false;
            }
        } else {
            // Consultation
            String text = "**" + message.getGuild().getName() + "** " + Translator.getLabel(lg, "lang.request.3") + " " + Guild.getGuild(message.getGuild()).getLanguage() + ".";
            ChannelLanguage chanLang = ChannelLanguage.getChannelLanguages().get(message.getChannel().getLongID());
            if (chanLang != null)
                text += "\nLe salon *" + message.getChannel().getName() + "* " + Translator.getLabel(lg, "lang.request.3") + " " + chanLang.getLang() + ".";
            Message.sendText(message.getChannel(), text);
        }
    }
    return false;
}
Also used : ChannelLanguage(data.ChannelLanguage) Language(enums.Language) ChannelLanguage(data.ChannelLanguage) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 20 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class LanguageCommand method help.

@Override
public String help(Language lg, String prefixe) {
    StringBuilder st = new StringBuilder(" (");
    for (Language lang : Language.values()) st.append(lang.getAbrev()).append(", ");
    st.setLength(st.length() - 2);
    st.append(").");
    return "**" + prefixe + name + "** " + Translator.getLabel(lg, "lang.help") + st.toString();
}
Also used : Language(enums.Language) ChannelLanguage(data.ChannelLanguage)

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