/** * Creates an {@link net.dv8tion.jda.core.entities.Icon Icon} instance for * this attachment if {@link #isImage() isImage()} is {@code true}! * <br>This is a convenience method that can be used to retrieve an Icon from an attachment image link which * requires a set user-agent to be loaded. * * <p>When a global proxy was specified via {@link net.dv8tion.jda.core.JDABuilder JDABuilder} this will use the * specified proxy to create an {@link java.io.InputStream InputStream} otherwise it will use a normal {@link java.net.URLConnection URLConnection} * with the User-Agent for the currently logged in account. * * @throws IOException * If an IOError occurs while reading the image * @throws java.lang.IllegalStateException * If this is not an image attachment * * @return {@link net.dv8tion.jda.core.entities.Icon Icon} for this image attachment * * @since 3.4.0 */ public Icon getAsIcon() throws IOException { if (!isImage()) throw new IllegalStateException("Cannot create an Icon out of this attachment. This is not an image."); AtomicReference<Icon> icon = new AtomicReference<>(); withInputStream((in) -> icon.set(Icon.from(in))); return icon.get(); }
@Override protected void execute(CommandEvent event) { String url; if(event.getArgs().isEmpty()) if(!event.getMessage().getAttachments().isEmpty() && event.getMessage().getAttachments().get(0).isImage()) url = event.getMessage().getAttachments().get(0).getUrl(); else url = null; else url = event.getArgs(); InputStream s = OtherUtil.imageFromUrl(url); if(s==null) { event.reply(event.getClient().getError()+" Invalid or missing URL"); } else { try { event.getSelfUser().getManager().setAvatar(Icon.from(s)).queue( v -> event.reply(event.getClient().getSuccess()+" Successfully changed avatar."), t -> event.reply(event.getClient().getError()+" Failed to set avatar.")); } catch(IOException e) { event.reply(event.getClient().getError()+" Could not load from provided URL."); } } } }