Search in sources :

Example 1 with Alliance

use of data.Alliance in project KaellyBot by Kaysoro.

the class AllianceCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    String pseudo = m.group(1).trim().toLowerCase();
    String serverName = null;
    if (Pattern.compile("\\s+-serv\\s+").matcher(pseudo).find()) {
        String[] split = pseudo.split("\\s+-serv\\s+");
        pseudo = split[0];
        serverName = split[1];
    }
    StringBuilder url;
    try {
        url = new StringBuilder(Translator.getLabel(lg, "game.url")).append(Translator.getLabel(lg, "alliance.url")).append("?").append(forPseudo).append(URLEncoder.encode(pseudo, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        ExceptionManager.manageException(e, message, this, lg);
        return;
    }
    if (serverName != null) {
        ServerUtils.ServerQuery serverQuery = ServerUtils.getServerDofusFromName(serverName, lg);
        if (serverQuery.hasSucceed()) {
            url.append("&").append(forServer).append(serverQuery.getServer());
        } else {
            serverQuery.getExceptions().stream().forEach(e -> e.throwException(message, this, lg, serverQuery.getServersFound()));
            return;
        }
    }
    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 la bonne alliance (ie la 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(2).text());
            }
            if (result.size() == 1) {
                Alliance alliancePage = Alliance.getAlliance(Translator.getLabel(lg, "game.url") + result.get(0), lg);
                message.getChannel().flatMap(chan -> chan.createEmbed(alliancePage.decorateEmbedObject(lg))).subscribe();
            } else if (result.size() > 1)
                tooMuchAlliances.throwException(message, this, lg, servers);
            else
                notFoundAlliance.throwException(message, this, lg);
        } else
            notFoundAlliance.throwException(message, this, lg);
    } catch (IOException e) {
        ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.ALLIANCEPAGE_INACCESSIBLE);
    } catch (Exception e) {
        ExceptionManager.manageException(e, message, this, lg);
    }
}
Also used : Language(enums.Language) JSoupManager(util.JSoupManager) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) AbstractCommand(commands.model.AbstractCommand) ServerUtils(util.ServerUtils) IOException(java.io.IOException) exceptions(exceptions) ArrayList(java.util.ArrayList) URLEncoder(java.net.URLEncoder) List(java.util.List) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) Alliance(data.Alliance) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) Translator(util.Translator) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServerUtils(util.ServerUtils) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Alliance(data.Alliance)

Aggregations

AbstractCommand (commands.model.AbstractCommand)1 Alliance (data.Alliance)1 MessageCreateEvent (discord4j.core.event.domain.message.MessageCreateEvent)1 Message (discord4j.core.object.entity.Message)1 Language (enums.Language)1 exceptions (exceptions)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLEncoder (java.net.URLEncoder)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1 JSoupManager (util.JSoupManager)1 ServerUtils (util.ServerUtils)1 Translator (util.Translator)1