Search in sources :

Example 6 with EmbedCreateSpec

use of discord4j.core.spec.EmbedCreateSpec 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 7 with EmbedCreateSpec

use of discord4j.core.spec.EmbedCreateSpec in project S-argo by Expugn.

the class Profile method bannerInfoMenu.

/**
 * Displays if the user is missing characters or weapons and if they do happen
 * to have the characters or weapons it will tell them that they obtained them
 * and for weapons it will tell them how much they have obtained of that weapon.
 *
 * @param bannerIDString  The banner ID in string form.
 */
private void bannerInfoMenu(String bannerIDString) {
    int bannerID = Integer.parseInt(bannerIDString) - 1;
    Consumer<EmbedCreateSpec> ecsTemplate;
    // OPEN BANNERS FILE
    List<Banner> banners = BannerParser.getBanners();
    if (bannerID < banners.size() && bannerID >= 0) {
        Banner requestedBanner = banners.get(bannerID);
        boolean characterFound = false;
        int characterCounter = 0;
        List<Character> obtainedCharacters = new ArrayList<>();
        List<Character> unobtainedCharacters = new ArrayList<>();
        for (Character c : requestedBanner.getCharacters()) {
            characterCounter++;
            // TRY AND FIND CHARACTER IN USER BOX
            for (Character oC : user.getCharacterBox()) {
                if (c.getPrefix().equals(oC.getPrefix()) && c.getName().equals(oC.getName()) && (c.getRarity() == oC.getRarity())) {
                    // CHARACTER IS SAME
                    characterFound = true;
                    break;
                }
            }
            // ADD CHARACTER TO LIST
            if (!characterFound) {
                unobtainedCharacters.add(c);
            } else {
                obtainedCharacters.add(c);
            }
            characterFound = false;
        }
        String obtainedList = "";
        int obtainedCounter = 0;
        if (obtainedCharacters.size() > 0) {
            for (Character c : obtainedCharacters) {
                obtainedList += c.toString() + "\n";
                obtainedCounter++;
            }
        }
        String noHaveList = "";
        int noHaveCounter = 0;
        if (unobtainedCharacters.size() > 0) {
            for (Character c : unobtainedCharacters) {
                noHaveList += c.toString() + "\n";
                noHaveCounter++;
            }
        }
        /* WEAPON STATS */
        boolean weaponFound = false;
        List<Weapon> obtainedWeapons = new ArrayList<>();
        List<Weapon> unobtainedWeapons = new ArrayList<>();
        for (Weapon w : requestedBanner.getWeapons()) {
            /* TRY AND FIND WEAPON IN USER BOX */
            for (Weapon oW : user.getWeaponBox()) {
                if (w.getName().equals(oW.getName())) {
                    /* WEAPON IS SAME */
                    w.setCount(oW.getCount());
                    weaponFound = true;
                    break;
                }
            }
            /* ADD WEAPON TO LIST */
            if (!weaponFound) {
                unobtainedWeapons.add(w);
            } else {
                obtainedWeapons.add(w);
            }
            weaponFound = false;
        }
        String obtainedList_wep = "";
        int obtainedCounter_wep = 0;
        if (obtainedWeapons.size() > 0) {
            for (Weapon w : obtainedWeapons) {
                obtainedList_wep += w.toStringWithCount() + "\n";
                obtainedCounter_wep++;
            }
        }
        String noHaveList_wep = "";
        int noHaveCounter_wep = 0;
        if (unobtainedWeapons.size() > 0) {
            for (Weapon w : unobtainedWeapons) {
                noHaveList_wep += w.toString() + "\n";
                noHaveCounter_wep++;
            }
        }
        int characterCounter_final = characterCounter;
        int obtainedCounter_final = obtainedCounter;
        String obtainedList_final = obtainedList;
        int noHaveCounter_final = noHaveCounter;
        String noHaveList_final = noHaveList;
        int obtainedCounter_wep_final = obtainedCounter_wep;
        String obtainedList_wep_final = obtainedList_wep;
        int noHaveCounter_wep_final = noHaveCounter_wep;
        String noHaveList_wep_final = noHaveList_wep;
        ecsTemplate = s -> {
            s.setAuthor(userName + "'s Profile", "", member.getAvatarUrl());
            s.setColor(new Color(255, 86, 91));
            s.setThumbnail(new GitHubImage("images/System/Profile_Icon.png").getURL());
            s.addField(requestedBanner.getBannerName(), characterCounter_final + " Characters Available", false);
            if (obtainedCharacters.size() > 0)
                s.addField(obtainedCounter_final + " Characters Obtained", obtainedList_final, false);
            if (unobtainedCharacters.size() > 0)
                s.addField(noHaveCounter_final + " Characters Missing", noHaveList_final, false);
            if (obtainedWeapons.size() > 0)
                s.addField(obtainedCounter_wep_final + " Weapons Obtained", obtainedList_wep_final, false);
            if (unobtainedWeapons.size() > 0)
                s.addField(noHaveCounter_wep_final + " Weapons Missing", noHaveList_wep_final, false);
        };
    } else {
        Sargo.replyToMessage_Warning(CHANNEL, "UNKNOWN BANNER ID", "Use '" + CommandManager.getCommandPrefix() + "**scout**' for a list of banners.");
        return;
    }
    Sargo.sendEmbed(CHANNEL, ecsTemplate);
}
Also used : Character(io.github.spugn.Sargo.Objects.Character) EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) GitHubImage(io.github.spugn.Sargo.Utilities.GitHubImage)

Example 8 with EmbedCreateSpec

use of discord4j.core.spec.EmbedCreateSpec in project S-argo by Expugn.

the class Sargo method sendEmbed.

public static Message sendEmbed(TextChannel tc, Consumer<EmbedCreateSpec> ecsTemplate, File image) {
    try {
        InputStream is = new FileInputStream(image);
        Message sentMessage = tc.createMessage(s -> s.setEmbed(ecsTemplate.andThen(es -> es.setImage("attachment://" + image.getName()))).addFile(image.getName(), is)).block();
        is.close();
        return sentMessage;
    } catch (FileNotFoundException e) {
        return tc.createMessage(ms -> ms.setEmbed(ecsTemplate.andThen(es -> es.addField("MISSING SCOUT RESULT", "Scout result image is missing.", true)))).block();
    } catch (IOException e) {
    // IGNORED
    }
    return null;
}
Also used : Reload(io.github.spugn.Sargo.Functions.Reload) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) Logger(org.slf4j.Logger) CommandSettingsParser(io.github.spugn.Sargo.XMLParsers.CommandSettingsParser) ReadyEvent(discord4j.core.event.domain.lifecycle.ReadyEvent) Update(io.github.spugn.Sargo.Functions.Update) DiscordClientBuilder(discord4j.core.DiscordClientBuilder) LoggerFactory(org.slf4j.LoggerFactory) DiscordClient(discord4j.core.DiscordClient) java.awt(java.awt) Consumer(java.util.function.Consumer) MessageListener(io.github.spugn.Sargo.Listeners.MessageListener) java.io(java.io) Message(discord4j.core.object.entity.Message) ScoutMasterParser(io.github.spugn.Sargo.XMLParsers.ScoutMasterParser) Activity(discord4j.core.object.presence.Activity) EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) SystemData(io.github.spugn.Sargo.System.SystemData) LoginSettingsParser(io.github.spugn.Sargo.XMLParsers.LoginSettingsParser) GitHubImage(io.github.spugn.Sargo.Utilities.GitHubImage) TextChannel(discord4j.core.object.entity.TextChannel) Presence(discord4j.core.object.presence.Presence) Message(discord4j.core.object.entity.Message)

Example 9 with EmbedCreateSpec

use of discord4j.core.spec.EmbedCreateSpec in project KaellyBot by Kaysoro.

the class Almanax method decorateGroupedObject.

public static EmbedCreateSpec decorateGroupedObject(Language lg, Date day, int occurrence) throws IOException {
    Date firstDate = DateUtils.addDays(day, 1);
    Date lastDate = DateUtils.addDays(day, occurrence);
    String title = Translator.getLabel(lg, "almanax.embed.title.1") + " " + discordToBot.format(firstDate) + (occurrence > 1 ? " " + Translator.getLabel(lg, "almanax.embed.title.2") + " " + discordToBot.format(lastDate) : "");
    EmbedCreateSpec.Builder builder = EmbedCreateSpec.builder().title(title);
    for (int i = 1; i <= occurrence; i++) {
        firstDate = DateUtils.addDays(new Date(), i);
        Almanax almanax = Almanax.get(lg, firstDate);
        builder.addField(discordToBot.format(firstDate), Translator.getLabel(lg, "almanax.embed.bonus") + " " + almanax.getBonus() + "\n" + Translator.getLabel(lg, "almanax.embed.offrande") + " " + almanax.getOffrande(), true);
    }
    return builder.build();
}
Also used : EmbedCreateSpec(discord4j.core.spec.EmbedCreateSpec) Date(java.util.Date)

Aggregations

EmbedCreateSpec (discord4j.core.spec.EmbedCreateSpec)9 MessageCreateEvent (discord4j.core.event.domain.message.MessageCreateEvent)5 Message (discord4j.core.object.entity.Message)5 Language (enums.Language)4 Matcher (java.util.regex.Matcher)4 AbstractCommand (commands.model.AbstractCommand)3 ServerDofus (data.ServerDofus)3 exceptions (exceptions)3 GitHubImage (io.github.spugn.Sargo.Utilities.GitHubImage)3 List (java.util.List)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3 Translator (util.Translator)3 Guild (data.Guild)2 Snowflake (discord4j.common.util.Snowflake)2 Member (discord4j.core.object.entity.Member)2 Character (io.github.spugn.Sargo.Objects.Character)2 IOException (java.io.IOException)2 Exception (java.lang.Exception)2 Normalizer (java.text.Normalizer)2