Search in sources :

Example 6 with XmlElement

use of net.robinfriedli.jxp.api.XmlElement in project aiode by robinfriedli.

the class InitialiseCommandContributionsTask method validateReferencedArguments.

private void validateReferencedArguments(CommandHierarchyNode<?> commandHierarchyNode, ArgumentContribution argumentContribution, Collection<XmlElement> arguments) {
    for (XmlElement argument : arguments) {
        String argumentIdentifier = argument.getAttribute("argument").getValue();
        ArgumentContribution referencedArgument = commandHierarchyNode.getArgument(argumentIdentifier).unwrapArgumentContribution();
        if (referencedArgument == null) {
            throw new IllegalStateException(String.format("Invalid command configuration for node '%s'. Cannot find referenced argument identifier '%s' required for element %s on argument %s.", commandHierarchyNode.getId(), argumentIdentifier, argument, argumentContribution));
        }
    }
}
Also used : XmlElement(net.robinfriedli.jxp.api.XmlElement) ArgumentContribution(net.robinfriedli.aiode.entities.xml.ArgumentContribution)

Example 7 with XmlElement

use of net.robinfriedli.jxp.api.XmlElement in project aiode by robinfriedli.

the class MigrateGuildSpecificationsTask method migrateSpecifications.

private void migrateSpecifications(JDA shard, File file, Session session) throws IOException {
    try (Context context = jxpBackend.getContext(file)) {
        List<XmlElement> guildSpecifications = context.query(tagName("guildSpecification")).collect();
        for (XmlElement guildSpecification : guildSpecifications) {
            migrateSpecification(shard, guildSpecification, session);
        }
    }
    File directory = new File("./resources/archive");
    if (!directory.exists()) {
        directory.mkdir();
    }
    Objects.requireNonNull(file);
    Files.move(file, new File(directory.getPath() + "/" + file.getName()));
}
Also used : Context(net.robinfriedli.jxp.persist.Context) XmlElement(net.robinfriedli.jxp.api.XmlElement) File(java.io.File)

Example 8 with XmlElement

use of net.robinfriedli.jxp.api.XmlElement in project aiode by robinfriedli.

the class MigrateGuildSpecificationsTask method migrateSpecification.

private void migrateSpecification(JDA shard, XmlElement specification, Session session) {
    String guildId = specification.getAttribute("guildId").getValue();
    Guild guild = shard.getGuildById(guildId);
    if (guild != null) {
        Optional<GuildSpecification> existingGuildSpecification = session.createQuery("from " + GuildSpecification.class.getName() + " where guildId = '" + guild.getId() + "'", GuildSpecification.class).uniqueResultOptional();
        if (existingGuildSpecification.isPresent()) {
            return;
        }
        String botifyName = specification.getAttribute("botifyName").getValue();
        String guildName = specification.getAttribute("guildName").getValue();
        String prefix;
        if (specification.hasAttribute("prefix")) {
            prefix = specification.getAttribute("prefix").getValue();
        } else {
            prefix = null;
        }
        GuildSpecification guildSpecification = new GuildSpecification();
        guildSpecification.setGuildName(guildName);
        guildSpecification.setGuildId(guildId);
        guildSpecification.setBotName(botifyName);
        guildSpecification.setPrefix(prefix);
        List<XmlElement> accessConfigurations = specification.query(tagName("accessConfiguration")).collect();
        for (XmlElement accessConfiguration : accessConfigurations) {
            String commandIdentifier = accessConfiguration.getAttribute("commandIdentifier").getValue();
            AccessConfiguration ac = new AccessConfiguration(commandIdentifier, session);
            String roleIdString = accessConfiguration.getAttribute("roleIds").getValue();
            Iterable<String> roleIds = splitter.split(roleIdString);
            for (String roleId : roleIds) {
                Role role = guild.getRoleById(roleId);
                if (role != null) {
                    GrantedRole grantedRole = new GrantedRole(role);
                    session.persist(grantedRole);
                    ac.addRole(grantedRole);
                }
            }
            guildSpecification.addAccessConfiguration(ac);
            session.persist(ac);
        }
        session.persist(guildSpecification);
    }
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) XmlElement(net.robinfriedli.jxp.api.XmlElement) Guild(net.dv8tion.jda.api.entities.Guild) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Example 9 with XmlElement

use of net.robinfriedli.jxp.api.XmlElement in project aiode by robinfriedli.

the class SetRedirectedSpotifyTrackNameTask method migrate.

private void migrate(Context context, SpotifyApi spotifyApi) throws IOException, SpotifyWebApiException, ParseException {
    for (XmlElement playlist : context.query(tagName("playlist")).collect()) {
        Map<String, XmlElement> itemsToMigrate = new HashMap<>();
        for (XmlElement playlistItem : playlist.getSubElements()) {
            if (playlistItem.hasAttribute("redirectedSpotifyId") && !playlistItem.hasAttribute("spotifyTrackName")) {
                String trackId = playlistItem.getAttribute("redirectedSpotifyId").getValue();
                itemsToMigrate.put(trackId, playlistItem);
            }
        }
        List<List<String>> batches = Lists.partition(Lists.newArrayList(itemsToMigrate.keySet()), 50);
        for (List<String> batch : batches) {
            Track[] tracks = spotifyApi.getSeveralTracks(batch.toArray(new String[0])).build().execute();
            for (Track track : tracks) {
                XmlElement playlistItem = itemsToMigrate.get(track.getId());
                playlistItem.setAttribute("spotifyTrackName", track.getName());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) XmlElement(net.robinfriedli.jxp.api.XmlElement) List(java.util.List) Track(se.michaelthelin.spotify.model_objects.specification.Track)

Example 10 with XmlElement

use of net.robinfriedli.jxp.api.XmlElement in project aiode by robinfriedli.

the class EmbedDocumentContribution method buildEmbed.

public EmbedBuilder buildEmbed() {
    Aiode aiode = Aiode.get();
    groovyShell.setProperty("aiode", aiode);
    groovyShell.setProperty("properties", aiode.getSpringPropertiesConfig());
    String title = getAttribute("title").getValue();
    String description = getAttribute("description").getValue();
    EmbedBuilder embedBuilder = new EmbedBuilder();
    embedBuilder.setTitle(title);
    embedBuilder.setDescription(description);
    for (XmlElement field : getSubElements()) {
        if (field.hasAttribute("displayPredicate")) {
            Object displayPredicateResult = groovyShell.evaluate(field.getAttribute("displayPredicate").getValue());
            if (!(displayPredicateResult instanceof Boolean)) {
                throw new IllegalStateException("displayPredicate did not return a boolean");
            }
            if (!((boolean) displayPredicateResult)) {
                continue;
            }
        }
        String fieldTitle = field.getAttribute("title").getValue();
        boolean inline = field.getAttribute("inline").getBool();
        embedBuilder.addField(fieldTitle, field.getTextContent(), inline);
    }
    return embedBuilder;
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) XmlElement(net.robinfriedli.jxp.api.XmlElement) AbstractXmlElement(net.robinfriedli.jxp.api.AbstractXmlElement) Aiode(net.robinfriedli.aiode.Aiode)

Aggregations

XmlElement (net.robinfriedli.jxp.api.XmlElement)10 Context (net.robinfriedli.jxp.persist.Context)4 HashMap (java.util.HashMap)3 List (java.util.List)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 Guild (net.dv8tion.jda.api.entities.Guild)3 Role (net.dv8tion.jda.api.entities.Role)2 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)2 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)2 ArgumentContribution (net.robinfriedli.aiode.entities.xml.ArgumentContribution)2 Conditions (net.robinfriedli.jxp.queries.Conditions)2 Sets (com.google.api.client.util.Sets)1 File (java.io.File)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Set (java.util.Set)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1