use of com.ivkos.wallhaven4j.util.exceptions.ConnectionException in project Shadbot by Shadorc.
the class WallpaperCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
LoadingMessage loadingMsg = new LoadingMessage("Loading wallpaper...", context.getChannel());
loadingMsg.send();
if (wallhaven == null) {
wallhaven = new Wallhaven(APIKeys.get(APIKey.WALLHAVEN_LOGIN), APIKeys.get(APIKey.WALLHAVEN_PASSWORD));
}
Options options = new Options();
options.addOption("p", PURITY, true, FormatUtils.format(Purity.values(), purity -> purity.toString().toLowerCase(), ", "));
options.addOption("c", CATEGORY, true, FormatUtils.format(Category.values(), cat -> cat.toString().toLowerCase(), ", "));
Option ratioOpt = new Option("rat", RATIO, true, "image ratio");
ratioOpt.setValueSeparator('x');
options.addOption(ratioOpt);
Option resOpt = new Option("res", RESOLUTION, true, "image resolution");
resOpt.setValueSeparator('x');
options.addOption(resOpt);
Option keyOpt = new Option("k", KEYWORD, true, KEYWORD);
keyOpt.setValueSeparator(',');
options.addOption(keyOpt);
CommandLine cmdLine;
try {
List<String> args = StringUtils.split(context.getArg());
cmdLine = new DefaultParser().parse(options, args.toArray(new String[args.size()]));
} catch (UnrecognizedOptionException | org.apache.commons.cli.MissingArgumentException err) {
loadingMsg.delete();
throw new IllegalCmdArgumentException(String.format("%s. Use `%shelp %s` for more information.", err.getMessage(), context.getPrefix(), this.getName()));
} catch (ParseException err) {
loadingMsg.delete();
Utils.handle("getting a wallpaper", context, err);
return;
}
Purity purity = this.parseEnum(loadingMsg, context, Purity.class, PURITY, cmdLine.getOptionValue(PURITY, Purity.SFW.toString()));
if ((purity.equals(Purity.NSFW) || purity.equals(Purity.SKETCHY)) && !context.getChannel().isNSFW()) {
loadingMsg.edit(TextUtils.mustBeNSFW(context.getPrefix()));
return;
}
SearchQueryBuilder queryBuilder = new SearchQueryBuilder();
queryBuilder.purity(purity);
if (cmdLine.hasOption(CATEGORY)) {
queryBuilder.categories(this.parseEnum(loadingMsg, context, Category.class, CATEGORY, cmdLine.getOptionValue(CATEGORY)));
}
if (cmdLine.hasOption(RATIO)) {
Dimension dim = this.parseDim(loadingMsg, context, RATIO, cmdLine.getOptionValues(RATIO));
queryBuilder.ratios(new Ratio((int) dim.getWidth(), (int) dim.getHeight()));
}
if (cmdLine.hasOption(RESOLUTION)) {
Dimension dim = this.parseDim(loadingMsg, context, RESOLUTION, cmdLine.getOptionValues(RESOLUTION));
queryBuilder.resolutions(new Resolution((int) dim.getWidth(), (int) dim.getHeight()));
}
if (cmdLine.hasOption(KEYWORD)) {
queryBuilder.keywords(cmdLine.getOptionValues(KEYWORD));
}
try {
List<Wallpaper> wallpapers = wallhaven.search(queryBuilder.pages(1).build());
if (wallpapers.isEmpty()) {
loadingMsg.edit(TextUtils.noResult(context.getMessage().getContent()));
return;
}
Wallpaper wallpaper = wallpapers.get(ThreadLocalRandom.current().nextInt(wallpapers.size()));
String tags = FormatUtils.format(wallpaper.getTags(), tag -> String.format("`%s`", StringUtils.remove(tag.toString(), "#")), " ");
EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Wallpaper").withAuthorUrl(wallpaper.getUrl()).withImage(wallpaper.getImageUrl()).appendField("Resolution", wallpaper.getResolution().toString(), false).appendField("Tags", tags, false);
loadingMsg.edit(embed.build());
} catch (ConnectionException err) {
loadingMsg.delete();
Utils.handle("getting a wallpaper", context, err.getCause());
}
}
Aggregations