Search in sources :

Example 1 with EmbedTable

use of net.robinfriedli.aiode.util.EmbedTable in project aiode by robinfriedli.

the class SearchCommand method listLocalList.

private void listLocalList() {
    if (getCommandInput().isBlank()) {
        Session session = getContext().getSession();
        List<Playlist> playlists = getQueryBuilderFactory().find(Playlist.class).build(session).getResultList();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        if (playlists.isEmpty()) {
            embedBuilder.setDescription("No playlists");
        } else {
            EmbedTable table = new EmbedTable(embedBuilder);
            table.addColumn("Playlist", playlists, Playlist::getName);
            table.addColumn("Duration", playlists, playlist -> Util.normalizeMillis(playlist.getDuration()));
            table.addColumn("Items", playlists, playlist -> String.valueOf(playlist.getSize()));
            table.build();
        }
        sendMessage(embedBuilder);
    } else {
        Playlist playlist = SearchEngine.searchLocalList(getContext().getSession(), getCommandInput());
        if (playlist == null) {
            throw new NoResultsFoundException(String.format("No local list found for '%s'", getCommandInput()));
        }
        String createdUserId = playlist.getCreatedUserId();
        String createdUser;
        if (createdUserId.equals("system")) {
            createdUser = playlist.getCreatedUser();
        } else {
            ShardManager shardManager = Aiode.get().getShardManager();
            User userById;
            try {
                userById = shardManager.retrieveUserById(createdUserId).complete();
            } catch (ErrorResponseException e) {
                if (e.getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
                    userById = null;
                } else {
                    throw e;
                }
            }
            createdUser = userById != null ? userById.getName() : playlist.getCreatedUser();
        }
        SpringPropertiesConfig springPropertiesConfig = Aiode.get().getSpringPropertiesConfig();
        String baseUri = springPropertiesConfig.requireApplicationProperty("aiode.server.base_uri");
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.addField("Name", playlist.getName(), true);
        embedBuilder.addField("Duration", Util.normalizeMillis(playlist.getDuration()), true);
        embedBuilder.addField("Created by", createdUser, true);
        embedBuilder.addField("Tracks", String.valueOf(playlist.getSize()), true);
        embedBuilder.addBlankField(false);
        String url = baseUri + String.format("/list?name=%s&guildId=%s", URLEncoder.encode(playlist.getName(), StandardCharsets.UTF_8), playlist.getGuildId());
        embedBuilder.addField("First tracks:", "[Full list](" + url + ")", false);
        List<PlaylistItem> items = playlist.getItemsSorted();
        Util.appendEmbedList(embedBuilder, items.size() > 5 ? items.subList(0, 5) : items, item -> item.display() + " - " + Util.normalizeMillis(item.getDuration()), "Track - Duration");
        sendWithLogo(embedBuilder);
    }
}
Also used : User(net.dv8tion.jda.api.entities.User) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) Playlist(net.robinfriedli.aiode.entities.Playlist) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 2 with EmbedTable

use of net.robinfriedli.aiode.util.EmbedTable in project aiode by robinfriedli.

the class PresetCommand method doRun.

@Override
public void doRun() {
    Session session = getContext().getSession();
    if (argumentSet("delete")) {
        Preset preset = SearchEngine.searchPreset(session, getCommandInput());
        if (preset == null) {
            throw new InvalidCommandException(String.format("No preset found for '%s'", getCommandInput()));
        }
        invoke(() -> session.delete(preset));
    } else if (getCommandInput().isBlank()) {
        List<Preset> presets = getQueryBuilderFactory().find(Preset.class).build(session).getResultList();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        if (presets.isEmpty()) {
            embedBuilder.setDescription("No presets saved");
        } else {
            EmbedTable table = new EmbedTable(embedBuilder);
            table.addColumn("Name", presets, Preset::getName);
            table.addColumn("Preset", presets, Preset::getPreset);
            table.build();
        }
        sendMessage(embedBuilder);
    } else {
        String presetString = getCommandInput();
        String name = getArgumentValue("as");
        Preset existingPreset = SearchEngine.searchPreset(getContext().getSession(), name);
        if (existingPreset != null) {
            throw new InvalidCommandException("Preset " + name + " already exists");
        }
        Preset preset = new Preset(name, presetString, getContext().getGuild(), getContext().getUser());
        String testString = presetString.contains("%s") ? name + " test" : name;
        AbstractCommand abstractCommand = preset.instantiateCommand(getManager(), getContext(), testString);
        CommandParser commandParser = new CommandParser(abstractCommand, ArgumentPrefixProperty.getForCurrentContext());
        commandParser.parse();
        abstractCommand.verify();
        invoke(() -> session.persist(preset));
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Preset(net.robinfriedli.aiode.entities.Preset) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) List(java.util.List) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) Session(org.hibernate.Session) CommandParser(net.robinfriedli.aiode.command.parser.CommandParser)

Example 3 with EmbedTable

use of net.robinfriedli.aiode.util.EmbedTable in project aiode by robinfriedli.

the class PropertyCommand method listProperties.

private void listProperties() {
    GuildPropertyManager guildPropertyManager = Aiode.get().getGuildPropertyManager();
    List<AbstractGuildProperty> properties = guildPropertyManager.getProperties();
    GuildSpecification specification = getContext().getGuildContext().getSpecification();
    EmbedBuilder embedBuilder = new EmbedBuilder();
    EmbedTable table = new EmbedTable(embedBuilder);
    table.addColumn("Name", properties, AbstractGuildProperty::getName);
    table.addColumn("Default Value", properties, AbstractGuildProperty::getDefaultValue);
    table.addColumn("Set Value", properties, property -> property.display(specification));
    table.build();
    sendMessage(embedBuilder);
}
Also used : AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) GuildPropertyManager(net.robinfriedli.aiode.discord.property.GuildPropertyManager) EmbedTable(net.robinfriedli.aiode.util.EmbedTable)

Example 4 with EmbedTable

use of net.robinfriedli.aiode.util.EmbedTable in project aiode by robinfriedli.

the class AbstractScriptCommand method listAllScripts.

private void listAllScripts(QueryBuilderFactory queryBuilderFactory, Session session, CommandContext context) {
    List<StoredScript> storedScripts = queryBuilderFactory.find(StoredScript.class).where((cb, root, subQueryFactory) -> cb.equal(root.get("scriptUsage"), subQueryFactory.createUncorrelatedSubQuery(StoredScript.ScriptUsage.class, "pk").where((cb1, root1) -> cb1.equal(root1.get("uniqueId"), scriptUsageId)).build(session))).build(session).getResultList();
    EmbedBuilder embedBuilder = new EmbedBuilder();
    if (storedScripts.isEmpty()) {
        embedBuilder.setDescription(String.format("No %ss saved", scriptUsageId));
        getMessageService().sendTemporary(embedBuilder, context.getChannel());
    } else {
        embedBuilder.setDescription(String.format("Show a specific %s by entering its identifier", scriptUsageId));
        EmbedTable table = new EmbedTable(embedBuilder);
        table.addColumn("Identifier", storedScripts, StoredScript::getIdentifier);
        table.addColumn("Active", storedScripts, script -> String.valueOf(script.isActive()));
        table.build();
        sendMessage(embedBuilder);
    }
}
Also used : MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) CommandManager(net.robinfriedli.aiode.command.CommandManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Session(org.hibernate.Session) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GroovyShell(groovy.lang.GroovyShell) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) Aiode(net.robinfriedli.aiode.Aiode) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) List(java.util.List) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) CommandContext(net.robinfriedli.aiode.command.CommandContext) StoredScript(net.robinfriedli.aiode.entities.StoredScript) Optional(java.util.Optional) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) ExceptionUtils(net.robinfriedli.aiode.exceptions.ExceptionUtils) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) StoredScript(net.robinfriedli.aiode.entities.StoredScript) EmbedTable(net.robinfriedli.aiode.util.EmbedTable)

Example 5 with EmbedTable

use of net.robinfriedli.aiode.util.EmbedTable in project aiode by robinfriedli.

the class AbstractPaginationWidget method prepareEmbedBuilderForPage.

private EmbedBuilder prepareEmbedBuilderForPage() {
    int pageCount = pages.size();
    if (currentPage >= pageCount && !pages.isEmpty()) {
        throw new IllegalStateException(String.format("Current page is out of bounds. Current index: %d; page count: %d", currentPage, pageCount));
    }
    EmbedBuilder embedBuilder = new EmbedBuilder();
    embedBuilder.setTitle(getTitle());
    String description = getDescription();
    if (!Strings.isNullOrEmpty(description)) {
        embedBuilder.setDescription(description);
    }
    Aiode aiode = Aiode.get();
    SpringPropertiesConfig springPropertiesConfig = aiode.getSpringPropertiesConfig();
    String baseUri = springPropertiesConfig.requireApplicationProperty("aiode.server.base_uri");
    String logoUrl = baseUri + "/resources-public/img/aiode-logo-small.png";
    embedBuilder.setFooter(String.format("Page %d of %d", currentPage + 1, Math.max(pageCount, 1)), logoUrl);
    EmbedTable embedTable = new EmbedTable(embedBuilder);
    List<E> page = pages.isEmpty() ? Collections.emptyList() : pages.get(currentPage);
    for (Column<E> column : getColumns()) {
        Function<E, EmbedTable.Group> groupFunction = column.getGroupFunction();
        if (groupFunction != null) {
            embedTable.addColumn(column.getTitle(), page, column.getDisplayFunc(), groupFunction);
        } else {
            embedTable.addColumn(column.getTitle(), page, column.getDisplayFunc());
        }
    }
    embedTable.build();
    return embedBuilder;
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) Aiode(net.robinfriedli.aiode.Aiode)

Aggregations

EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)5 EmbedTable (net.robinfriedli.aiode.util.EmbedTable)5 Session (org.hibernate.Session)3 List (java.util.List)2 Aiode (net.robinfriedli.aiode.Aiode)2 SpringPropertiesConfig (net.robinfriedli.aiode.boot.SpringPropertiesConfig)2 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)2 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)2 GroovyShell (groovy.lang.GroovyShell)1 Optional (java.util.Optional)1 User (net.dv8tion.jda.api.entities.User)1 ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)1 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)1 YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)1 CommandContext (net.robinfriedli.aiode.command.CommandContext)1 CommandManager (net.robinfriedli.aiode.command.CommandManager)1 CommandParser (net.robinfriedli.aiode.command.parser.CommandParser)1 AbstractGuildProperty (net.robinfriedli.aiode.discord.property.AbstractGuildProperty)1 GuildPropertyManager (net.robinfriedli.aiode.discord.property.GuildPropertyManager)1 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)1