Search in sources :

Example 1 with MessageCreateEvent

use of discord4j.core.event.domain.message.MessageCreateEvent in project KaellyBot by Kaysoro.

the class AlignmentCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    String content = m.group(1).trim();
    Optional<discord4j.core.object.entity.Guild> guild = message.getGuild().blockOptional();
    Optional<Member> user = message.getAuthorAsMember().blockOptional();
    // Initialisation du Filtre
    City city = null;
    Order order = null;
    if (guild.isPresent() && user.isPresent()) {
        ServerDofus server = ServerUtils.getDofusServerFrom(Guild.getGuild(guild.get()), message.getChannel().block());
        // Consultation filtré par niveau
        if ((m = Pattern.compile(">\\s+(\\d{1,3})(\\s+.+)?").matcher(content)).matches()) {
            int level = Integer.parseInt(m.group(1));
            if (m.group(2) != null) {
                ServerUtils.ServerQuery serverQuery = ServerUtils.getServerDofusFromName(m.group(2), lg);
                if (serverQuery.hasSucceed())
                    server = serverQuery.getServer();
                else {
                    serverQuery.getExceptions().forEach(e -> e.throwException(message, this, lg, serverQuery.getServersFound()));
                    return;
                }
            } else if (server == null) {
                notFoundServer.throwException(message, this, lg);
                return;
            }
            List<EmbedCreateSpec> embeds = OrderUser.getOrdersFromLevel(guild.get().getMembers().collectList().blockOptional().orElse(Collections.emptyList()), server, level, guild.get(), lg);
            for (EmbedCreateSpec embed : embeds) message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
        } else {
            // L'utilisateur concerné est-il l'auteur de la commande ?
            if (Pattern.compile("^<@[!|&]?\\d+>").matcher(content).find()) {
                content = content.replaceFirst("<@[!|&]?\\d+>", "").trim();
                Optional<Snowflake> memberId = message.getUserMentionIds().stream().findFirst();
                if (!memberId.isPresent()) {
                    BasicDiscordException.USER_NEEDED.throwException(message, this, lg);
                    return;
                }
                user = guild.get().getMemberById(memberId.get()).blockOptional();
            }
            // Consultation des données filtrés par utilisateur
            ServerUtils.ServerQuery serverQuery = ServerUtils.getServerDofusFromName(content, lg);
            if (!serverQuery.getServersFound().isEmpty() && Pattern.compile("(.+)").matcher(content).matches() || content.isEmpty()) {
                if (serverQuery.hasSucceed())
                    server = serverQuery.getServer();
                else if (server == null) {
                    if (!content.isEmpty())
                        serverQuery.getExceptions().forEach(e -> e.throwException(message, this, lg, serverQuery.getServersFound()));
                    else
                        notFoundServer.throwException(message, this, lg);
                    return;
                }
                if (user.isPresent()) {
                    List<EmbedCreateSpec> embeds = OrderUser.getOrdersFromUser(user.get(), server, lg);
                    for (EmbedCreateSpec embed : embeds) message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
                }
            } else // Enregistrement des données
            if ((m = Pattern.compile("(\\p{L}+)\\s+(\\p{L}+)\\s+(\\d{1,3})(\\s+.+)?").matcher(content)).matches()) {
                if (user.isPresent() && message.getAuthor().isPresent() && user.get().getId().equals(message.getAuthor().get().getId())) {
                    // Parsing des données et traitement des divers exceptions
                    List<City> cities = findCity(m.group(1), lg);
                    if (checkData(cities, tooMuchCities, notFoundCity, message, lg))
                        return;
                    city = cities.get(0);
                    List<Order> orders = findOrder(m.group(2), lg);
                    if (checkData(orders, tooMuchOrders, notFoundOrder, message, lg))
                        return;
                    order = orders.get(0);
                    int level = Integer.parseInt(m.group(3));
                    if (m.group(4) != null) {
                        ServerUtils.ServerQuery query = ServerUtils.getServerDofusFromName(m.group(4), lg);
                        if (query.hasSucceed())
                            server = query.getServer();
                        else {
                            query.getExceptions().forEach(e -> e.throwException(message, this, lg, query.getServersFound()));
                            return;
                        }
                    } else if (server == null) {
                        notFoundServer.throwException(message, this, lg);
                        return;
                    }
                    if (OrderUser.containsKeys(user.get().getId().asLong(), server, city, order)) {
                        OrderUser.get(user.get().getId().asLong(), server, city, order).get(0).setLevel(level);
                        if (level != 0)
                            message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "align.update"))).subscribe();
                        else
                            message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "align.reset"))).subscribe();
                    } else {
                        new OrderUser(user.get().getId().asLong(), server, city, order, level).addToDatabase();
                        if (level != 0)
                            message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "align.save"))).subscribe();
                        else
                            message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "align.no_reset"))).subscribe();
                    }
                } else
                    badUse.throwException(message, this, lg);
            } else // Consultation filtré par cité et/ou par ordre
            if ((m = Pattern.compile("(\\p{L}+)(\\s+\\p{L}+)?(\\s+[\\p{L}|\\W]+)?").matcher(content)).matches()) {
                if (m.group(3) != null) {
                    ServerUtils.ServerQuery query = ServerUtils.getServerDofusFromName(m.group(3), lg);
                    if (query.hasSucceed())
                        server = query.getServer();
                    else {
                        query.getExceptions().forEach(e -> e.throwException(message, this, lg, query.getServersFound()));
                        return;
                    }
                }
                // On a précisé à la fois une cité et un ordre
                if (m.group(2) != null) {
                    boolean is2Server = false;
                    if (m.group(3) == null) {
                        ServerUtils.ServerQuery query = ServerUtils.getServerDofusFromName(m.group(2), lg);
                        if (query.hasSucceed()) {
                            server = query.getServer();
                            is2Server = true;
                        } else {
                            query.getExceptions().forEach(e -> e.throwException(message, this, lg, query.getServersFound()));
                            return;
                        }
                    }
                    if (is2Server) {
                        // Est-ce un ordre ? une cité ?
                        String value = m.group(1).trim();
                        List<City> cities = findCity(value, lg);
                        List<Order> orders = findOrder(value, lg);
                        if (cities.isEmpty() && orders.isEmpty()) {
                            notFoundFilter.throwException(message, this, lg);
                            return;
                        }
                        if (cities.size() > 1 || orders.size() > 1) {
                            tooMuchFilters.throwException(message, this, lg);
                            return;
                        }
                        if (cities.size() == 1)
                            city = cities.get(0);
                        if (orders.size() == 1)
                            order = orders.get(0);
                    } else {
                        List<City> cities = findCity(m.group(1).trim(), lg);
                        if (checkData(cities, tooMuchCities, notFoundCity, message, lg))
                            return;
                        city = cities.get(0);
                        List<Order> orders = findOrder(m.group(2).trim(), lg);
                        if (checkData(orders, tooMuchOrders, notFoundOrder, message, lg))
                            return;
                        order = orders.get(0);
                    }
                } else {
                    // Is an order ? a city ?
                    List<City> cities = findCity(m.group(1).trim(), lg);
                    List<Order> orders = findOrder(m.group(1).trim(), lg);
                    if (cities.isEmpty() && orders.isEmpty()) {
                        notFoundFilter.throwException(message, this, lg);
                        return;
                    }
                    if (cities.size() > 1 || orders.size() > 1) {
                        tooMuchFilters.throwException(message, this, lg);
                        return;
                    }
                    if (cities.size() == 1)
                        city = cities.get(0);
                    if (orders.size() == 1)
                        order = orders.get(0);
                }
                if (server == null) {
                    notFoundServer.throwException(message, this, lg);
                    return;
                }
                List<EmbedCreateSpec> embeds = OrderUser.getOrdersFromCityOrOrder(guild.get().getMembers().collectList().blockOptional().orElse(Collections.emptyList()), server, city, order, guild.get(), lg);
                for (EmbedCreateSpec embed : embeds) message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
            } else
                badUse.throwException(message, this, lg);
        }
    }
}
Also used : Order(enums.Order) Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) ServerUtils(util.ServerUtils) OrderUser(data.OrderUser) Snowflake(discord4j.common.util.Snowflake) FetchCommand(commands.model.FetchCommand) Guild(data.Guild) exceptions(exceptions) Normalizer(java.text.Normalizer) ArrayList(java.util.ArrayList) ServerDofus(data.ServerDofus) City(enums.City) Consumer(java.util.function.Consumer) List(java.util.List) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) Member(discord4j.core.object.entity.Member) EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) Order(enums.Order) Optional(java.util.Optional) Translator(util.Translator) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) OrderUser(data.OrderUser) ServerDofus(data.ServerDofus) EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) City(enums.City) ServerUtils(util.ServerUtils) Guild(data.Guild) Snowflake(discord4j.common.util.Snowflake) ArrayList(java.util.ArrayList) List(java.util.List) Member(discord4j.core.object.entity.Member)

Example 2 with MessageCreateEvent

use of discord4j.core.event.domain.message.MessageCreateEvent in project KaellyBot by Kaysoro.

the class AlmanaxCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    try {
        Date date = new Date();
        if (m.group(1) != null && m.group(1).matches("\\s+\\+\\d")) {
            int number = Integer.parseInt(m.group(1).replaceAll("\\s+\\+", ""));
            try {
                EmbedCreateSpec embed = Almanax.decorateGroupedObject(lg, date, number);
                message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
            } catch (IOException e) {
                ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.ALMANAX);
            }
        } else {
            if (m.group(1) != null && m.group(1).matches("\\s+\\d{2}/\\d{2}/\\d{4}"))
                date = Almanax.discordToBot.parse(m.group(1));
            Almanax almanax = Almanax.get(lg, date);
            message.getChannel().flatMap(chan -> chan.createEmbed(almanax.decorateMoreEmbedObject(lg))).subscribe();
        }
    } catch (ParseException e) {
        BasicDiscordException.INCORRECT_DATE_FORMAT.throwException(message, this, lg);
    } catch (IOException e) {
        ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.ALMANAX);
    } catch (Exception e) {
        ExceptionManager.manageException(e, message, this, lg);
    }
}
Also used : Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) AbstractCommand(commands.model.AbstractCommand) Date(java.util.Date) Almanax(data.Almanax) IOException(java.io.IOException) exceptions(exceptions) Exception(java.lang.Exception) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) Translator(util.Translator) ParseException(java.text.ParseException) EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) Almanax(data.Almanax) IOException(java.io.IOException) ParseException(java.text.ParseException) Date(java.util.Date) IOException(java.io.IOException) Exception(java.lang.Exception) ParseException(java.text.ParseException)

Example 3 with MessageCreateEvent

use of discord4j.core.event.domain.message.MessageCreateEvent 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)

Example 4 with MessageCreateEvent

use of discord4j.core.event.domain.message.MessageCreateEvent in project KaellyBot by Kaysoro.

the class DistanceCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    Coordinate coordinate = new Coordinate(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)));
    StringBuilder st = new StringBuilder();
    Transport zaap = null;
    Transport transportLimited = null;
    if (!coordinate.isNull()) {
        double minDist = Double.MAX_VALUE;
        double minDistLimited = Double.MAX_VALUE;
        for (Transport transport : Transport.values()) {
            double tmp = transport.getCoordinate().getDistance(coordinate);
            if (transport.isFreeAccess() && (zaap == null || minDist > tmp)) {
                zaap = transport;
                minDist = tmp;
            }
            if (!transport.isFreeAccess() && (transportLimited == null || minDistLimited > tmp)) {
                transportLimited = transport;
                minDistLimited = tmp;
            }
        }
        st.append(Translator.getLabel(lg, "distance.request.1")).append(" ").append(zaap.toDiscordString(lg));
        if (minDist > minDistLimited)
            st.append("\n").append(Translator.getLabel(lg, "distance.request.2")).append(" ").append(transportLimited.toDiscordString(lg));
    } else
        st.append(Translator.getLabel(lg, "distance.request.3"));
    message.getChannel().flatMap(chan -> chan.createMessage(st.toString())).subscribe();
}
Also used : Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) AbstractCommand(commands.model.AbstractCommand) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) Transport(enums.Transport) Translator(util.Translator) Coordinate(data.Coordinate) Coordinate(data.Coordinate) Transport(enums.Transport)

Example 5 with MessageCreateEvent

use of discord4j.core.event.domain.message.MessageCreateEvent in project KaellyBot by Kaysoro.

the class GuildCommand 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, "guild.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 guilde (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(3).text());
            }
            if (result.size() == 1) {
                DofusGuild guildPage = DofusGuild.getDofusGuild(Translator.getLabel(lg, "game.url") + result.get(0), lg);
                message.getChannel().flatMap(chan -> chan.createEmbed(guildPage.decorateEmbedObject(lg))).subscribe();
            } else if (result.size() > 1)
                tooMuchGuilds.throwException(message, this, lg, servers);
            else
                notFoundGuild.throwException(message, this, lg);
        } else
            notFoundGuild.throwException(message, this, lg);
    } catch (IOException e) {
        ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.GUILDPAGE_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) 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) DofusGuild(data.DofusGuild) DofusGuild(data.DofusGuild) 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)

Aggregations

MessageCreateEvent (discord4j.core.event.domain.message.MessageCreateEvent)16 Language (enums.Language)16 Matcher (java.util.regex.Matcher)16 Translator (util.Translator)16 Message (discord4j.core.object.entity.Message)15 AbstractCommand (commands.model.AbstractCommand)14 exceptions (exceptions)8 List (java.util.List)8 Pattern (java.util.regex.Pattern)6 Guild (data.Guild)5 Snowflake (discord4j.common.util.Snowflake)5 ArrayList (java.util.ArrayList)5 ServerUtils (util.ServerUtils)5 Constants (data.Constants)4 MessageChannel (discord4j.core.object.entity.channel.MessageChannel)4 EmbedCreateSpec (discord4j.core.spec.EmbedCreateSpec)4 BasicDiscordException (exceptions.BasicDiscordException)4 IOException (java.io.IOException)4 Collectors (java.util.stream.Collectors)4 ServerDofus (data.ServerDofus)3