Search in sources :

Example 71 with Language

use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.

the class RandomCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    Random r = new Random();
    Matcher tmp;
    if (m.group(1) == null || m.group(1).trim().isEmpty()) {
        boolean value = r.nextBoolean();
        message.getChannel().flatMap(chan -> chan.createMessage(value ? Translator.getLabel(lg, "random.request.1") : Translator.getLabel(lg, "random.request.2") + " !")).subscribe();
    } else if (m.group(1).matches("\\s+-?\\d+")) {
        try {
            int limit = Integer.parseInt(m.group(1).trim());
            if (limit > 0)
                message.getChannel().flatMap(chan -> chan.createMessage(r.nextInt(limit) + " !")).subscribe();
            else
                message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "random.request.3"))).subscribe();
        } catch (NumberFormatException e) {
            message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "random.request.4") + " " + Integer.MAX_VALUE + ").")).subscribe();
        }
    } else if ((tmp = Pattern.compile("\\s+-dj(\\s+\\d{1,3})?(\\s+\\d{1,3})?").matcher(m.group(1))).matches()) {
        int level = DEFAULT_LEVEL;
        int tolerance = DEFAULT_TOLERANCE;
        if (tmp.group(1) != null) {
            level = Integer.parseInt(tmp.group(1).trim());
            level = level - level % 10;
            if (level > DEFAULT_LEVEL)
                level = DEFAULT_LEVEL;
            if (level < MIN_LEVEL)
                level = MIN_LEVEL;
        }
        if (tmp.group(2) != null)
            tolerance = Integer.parseInt(tmp.group(2).trim());
        else if (tmp.group(1) == null)
            tolerance = MAX_TOLERANCE;
        final int finalLevel = level;
        final int finalTolerance = tolerance;
        List<Dungeon> djs = Arrays.stream(Dungeon.values()).filter(dj -> dj.filterByLevel(finalLevel, finalTolerance)).collect(Collectors.toList());
        Dungeon selected = djs.get(r.nextInt(djs.size()));
        if (selected.isEvent() && djs.size() > 1) {
            Dungeon selected2 = selected;
            while (selected == selected2 || selected2.isEvent()) selected2 = djs.get(r.nextInt(djs.size()));
            final Dungeon SELECTED2 = selected2;
            message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "random.request.6").replace("{dj}", selected.getLabel(lg)).replace("{dj2}", SELECTED2.getLabel(lg)).replace("{size}", String.valueOf(djs.size())))).subscribe();
        } else
            message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "random.request.5").replace("{dj}", selected.getLabel(lg)).replace("{size}", String.valueOf(djs.size())))).subscribe();
    } else if (m.group(1).matches("\\s+-dj(\\s+\\d+)?(\\s+\\d+)?"))
        badUse.throwException(message, this, lg);
    else {
        String value = m.group(1).trim();
        String[] values = value.split("\\s+");
        message.getChannel().flatMap(chan -> chan.createMessage(values[r.nextInt(values.length)] + " !")).subscribe();
    }
}
Also used : Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) Arrays(java.util.Arrays) List(java.util.List) AbstractCommand(commands.model.AbstractCommand) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) Random(java.util.Random) Translator(util.Translator) Pattern(java.util.regex.Pattern) Dungeon(enums.Dungeon) Collectors(java.util.stream.Collectors) Random(java.util.Random) Matcher(java.util.regex.Matcher) Dungeon(enums.Dungeon)

Example 72 with Language

use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.

the class WhoisCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    String pseudo = m.group(2).trim().toLowerCase();
    ServerDofus server = null;
    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;
    }
    if (m.group(3) != null) {
        String serverName = m.group(3).trim().toLowerCase();
        ServerUtils.ServerQuery serverQuery = ServerUtils.getServerDofusFromName(serverName, lg);
        if (serverQuery.hasSucceed()) {
            url.append("&").append(forServer).append(serverQuery.getServer().getId());
            server = serverQuery.getServer();
        } else {
            serverQuery.getExceptions().forEach(e -> e.throwException(message, this, lg, serverQuery.getServersFound()));
            return;
        }
    }
    try {
        Document doc = JSoupManager.getDocument(url.toString());
        Elements elements = doc.getElementsByClass("ak-bg-odd");
        elements.addAll(doc.getElementsByClass("ak-bg-even"));
        if (!elements.isEmpty()) {
            // loop until finding the nearest character
            List<CharacterQuery> result = elements.stream().filter(e -> pseudo.equals(e.child(1).text().trim().toLowerCase())).map(e -> new CharacterQuery().withUrl(e.child(1).select("a").attr("href")).withServer(e.child(e.children().size() - 2).text())).collect(Collectors.toList());
            // Ankama bug workaround
            if (server != null && result.size() > 1) {
                List<CharacterQuery> filteredResult = new ArrayList<>();
                for (CharacterQuery query : result) try {
                    JSoupManager.getResponse(Translator.getLabel(lg, "game.url") + query.getUrl());
                    filteredResult.add(query);
                } catch (IOException e) {
                    LOG.warn("Not distinct character for same server: " + query.getUrl());
                }
                result = filteredResult;
                if (result.isEmpty()) {
                    BasicDiscordException.CHARACTERPAGE_INACCESSIBLE.throwException(message, this, lg);
                    return;
                }
            }
            if (result.size() == 1) {
                Connection.Response response = JSoupManager.getResponse(Translator.getLabel(lg, "game.url") + result.get(0).getUrl());
                final List<CharacterQuery> RESULT = result;
                if (!response.url().getPath().endsWith(Translator.getLabel(lg, "whois.request"))) {
                    if (m.group(1) == null) {
                        try {
                            EmbedCreateSpec embed = Character.getCharacter(Translator.getLabel(lg, "game.url") + RESULT.get(0).getUrl(), lg).decorateEmbedObject(lg);
                            message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
                        } catch (IOException e) {
                            ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.CHARACTERPAGE_INACCESSIBLE);
                        }
                    } else {
                        try {
                            EmbedCreateSpec embed = Character.getCharacterStuff(Translator.getLabel(lg, "game.url") + RESULT.get(0).getUrl() + Translator.getLabel(lg, "character.stuff.url"), lg).decorateMoreEmbedObject(lg);
                            message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
                        } catch (IOException e) {
                            ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.CHARACTERPAGE_INACCESSIBLE);
                        }
                    }
                } else
                    BasicDiscordException.CHARACTER_TOO_OLD.throwException(message, this, lg);
            } else if (result.size() > 1)
                tooMuchCharacters.throwException(message, this, lg, result.stream().map(CharacterQuery::getServer).distinct().collect(Collectors.toList()));
            else
                notFoundCharacter.throwException(message, this, lg);
        } else
            notFoundCharacter.throwException(message, this, lg);
    } catch (IOException e) {
        ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.CHARACTERPAGE_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) Logger(org.slf4j.Logger) ServerUtils(util.ServerUtils) LoggerFactory(org.slf4j.LoggerFactory) Character(data.Character) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) exceptions(exceptions) Exception(java.lang.Exception) ArrayList(java.util.ArrayList) ServerDofus(data.ServerDofus) URLEncoder(java.net.URLEncoder) List(java.util.List) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) Document(org.jsoup.nodes.Document) EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) Connection(org.jsoup.Connection) Elements(org.jsoup.select.Elements) Translator(util.Translator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServerDofus(data.ServerDofus) EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) ArrayList(java.util.ArrayList) Connection(org.jsoup.Connection) 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) Exception(java.lang.Exception) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 73 with Language

use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.

the class SendNudeCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    if (message.getChannel().blockOptional().map(chan -> chan instanceof PrivateChannel).orElse(false) || message.getChannel().blockOptional().map(chan -> ((TextChannel) chan).isNsfw()).orElse(false)) {
        int position = RANDOM.nextInt(Nude.values().length);
        Nude nude = Nude.values()[position];
        message.getChannel().flatMap(chan -> chan.createEmbed(spec -> spec.setTitle(Translator.getLabel(lg, "sendnude.title")).setDescription(Translator.getLabel(lg, "sendnude.author").replace("{author}", nude.getAuthor()).replace("{url}", nude.getUrl())).setColor(discord4j.rest.util.Color.PINK).setFooter(Translator.getLabel(lg, "sendnude.footer").replace("{position}", String.valueOf(position + 1)).replace("{number}", String.valueOf(Nude.values().length)), null).setImage(nude.getImage()))).subscribe();
    } else
        // Exception NSFW
        BasicDiscordException.NO_NSFW_CHANNEL.throwException(message, this, lg);
}
Also used : PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) Language(enums.Language) java.awt(java.awt) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) AbstractCommand(commands.model.AbstractCommand) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) TextChannel(discord4j.core.object.entity.channel.TextChannel) PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) Nude(enums.Nude) BasicDiscordException(exceptions.BasicDiscordException) Random(java.util.Random) Translator(util.Translator) Nude(enums.Nude)

Example 74 with Language

use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.

the class AboutCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    Optional<ApplicationInfo> appInfo = message.getClient().getApplicationInfo().blockOptional();
    if (appInfo.isPresent()) {
        message.getChannel().flatMap(chan -> chan.createEmbed(spec -> {
            spec.setTitle(Translator.getLabel(lg, "about.title").replace("{name}", Constants.name).replace("{version}", Constants.version)).setImage(Constants.changelog).setThumbnail(appInfo.get().getIconUrl(Image.Format.PNG).orElse(null)).addField(Translator.getLabel(lg, "about.invite.title"), Translator.getLabel(lg, "about.invite.desc").replace("{name}", Constants.name).replace("{invite}", Constants.invite), false).addField(Translator.getLabel(lg, "about.support.title"), Translator.getLabel(lg, "about.support.desc").replace("{name}", Constants.name).replace("{discordInvite}", Constants.discordInvite), false).addField(Translator.getLabel(lg, "about.twitter.title"), Translator.getLabel(lg, "about.twitter.desc").replace("{name}", Constants.name).replace("{twitter}", Constants.twitterAccount), false).addField(Translator.getLabel(lg, "about.opensource.title"), Translator.getLabel(lg, "about.opensource.desc").replace("{git}", Constants.git), false).addField(Translator.getLabel(lg, "about.free.title"), Translator.getLabel(lg, "about.free.desc").replace("{paypal}", Constants.paypal), false).addField(Translator.getLabel(lg, "about.privacy.title"), Translator.getLabel(lg, "about.privacy.desc").replace("{paypal}", Constants.paypal), false).addField(Translator.getLabel(lg, "about.graphist.title"), Translator.getLabel(lg, "about.graphist.desc").replace("{graphist}", Graphist.ELYCANN.toMarkdown()), false);
            StringBuilder st = new StringBuilder();
            for (Donator donator : Donator.values()) st.append(donator.getName()).append(", ");
            st.setLength(st.length() - 2);
            spec.addField(Translator.getLabel(lg, "about.donators.title"), st.toString() + ".", false);
        })).subscribe();
    }
}
Also used : User(discord4j.core.object.entity.User) Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) AbstractCommand(commands.model.AbstractCommand) Constants.authorName(data.Constants.authorName) Constants(data.Constants) ApplicationInfo(discord4j.core.object.entity.ApplicationInfo) Donator(enums.Donator) Image(discord4j.rest.util.Image) Matcher(java.util.regex.Matcher) Constants.authorAvatar(data.Constants.authorAvatar) Message(discord4j.core.object.entity.Message) Optional(java.util.Optional) Graphist(enums.Graphist) Translator(util.Translator) Donator(enums.Donator) ApplicationInfo(discord4j.core.object.entity.ApplicationInfo)

Example 75 with Language

use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.

the class TooMuchDiscordException method throwException.

@Override
public void throwException(Message message, Command command, Language lg, Object... arguments) {
    AnkamaBug bug = null;
    String gender = Translator.getLabel(lg, "exception.object." + objectKey + ".gender");
    StringBuilder st = new StringBuilder(Translator.getLabel(lg, "exception.toomuch.toomuch." + gender)).append(" ").append(Translator.getLabel(lg, "exception.object." + objectKey + ".plural")).append(" ").append(Translator.getLabel(lg, "exception.toomuch.found." + gender));
    if (arguments.length > 0) {
        List<Object> objects = (List<Object>) arguments[0];
        long similarOcc = objects.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting())).values().stream().max(Long::compareTo).orElse(0L);
        if (similarOcc > 1) {
            if (command instanceof WhoisCommand)
                bug = AnkamaBug.GHOST_CHARACTER;
            else if (command instanceof GuildCommand)
                bug = AnkamaBug.GHOST_GUILD;
            else if (command instanceof AllianceCommand)
                bug = AnkamaBug.GHOST_ALLY;
        }
        if (objects.size() <= ITEM_LIMIT) {
            st.append(": ");
            for (Object object : objects) if (isTranslatable)
                st.append(Translator.getLabel(lg, object.toString())).append(", ");
            else
                st.append(object.toString()).append(", ");
            st.delete(st.length() - 2, st.length()).append(".");
        } else
            st.append(". ").append(Translator.getLabel(lg, "exception.toomuch.items"));
    } else {
        if (st.substring(st.length() - 1, st.length()).matches("\\s+"))
            st.delete(st.length() - 1, st.length());
        st.append(".");
    }
    if (bug != null) {
        final AnkamaBug BUG = bug;
        message.getChannel().flatMap(channel -> channel.createEmbed(BUG.decorateEmbed(st.toString(), lg))).subscribe();
    } else
        message.getChannel().flatMap(channel -> channel.createMessage(st.toString())).subscribe();
}
Also used : GuildCommand(commands.classic.GuildCommand) AnkamaBug(enums.AnkamaBug) Language(enums.Language) WhoisCommand(commands.classic.WhoisCommand) List(java.util.List) Message(discord4j.core.object.entity.Message) Command(commands.model.Command) Translator(util.Translator) Collectors(java.util.stream.Collectors) AllianceCommand(commands.classic.AllianceCommand) AnkamaBug(enums.AnkamaBug) WhoisCommand(commands.classic.WhoisCommand) GuildCommand(commands.classic.GuildCommand) AllianceCommand(commands.classic.AllianceCommand) List(java.util.List)

Aggregations

Language (enums.Language)69 Matcher (java.util.regex.Matcher)43 ArrayList (java.util.ArrayList)29 AbstractCommand (commands.model.AbstractCommand)19 IOException (java.io.IOException)19 Translator (util.Translator)19 Message (discord4j.core.object.entity.Message)18 MessageCreateEvent (discord4j.core.event.domain.message.MessageCreateEvent)16 List (java.util.List)15 Language (ai.elimu.model.v2.enums.Language)12 Guild (data.Guild)12 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)10 Word (ai.elimu.model.content.Word)10 ChannelLanguage (data.ChannelLanguage)10 Command (commands.model.Command)9 Snowflake (discord4j.common.util.Snowflake)8 BasicDiscordException (exceptions.BasicDiscordException)8 ServerDofus (data.ServerDofus)7 exceptions (exceptions)7 Document (org.jsoup.nodes.Document)7