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);
}
}
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));
}
}
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);
}
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);
}
}
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;
}
Aggregations