/** * 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(); }
/** * Creates an {@link Icon Icon} with the specified image data. * * @param data * not-null image data bytes. * * @throws IllegalArgumentException * if the provided data is null * * @return An Icon instance representing the specified image data */ public static Icon from(byte[] data) { Checks.notNull(data, "Provided byte[]"); try { return new Icon(new String(Base64.getEncoder().encode(data), "UTF-8")); } catch (UnsupportedEncodingException e) { throw new AssertionError(e); // thanks JDK 1.4 } } }
@Override public RequestBody finalizeData() { JSONObject object = new JSONObject(); object.put("name", name); object.put("avatar", avatar != null ? avatar.getEncoding() : JSONObject.NULL); return getRequestBody(object); }
/** * Creates an {@link Icon Icon} with the specified {@link java.io.InputStream InputStream}. * <br>We here read the specified InputStream and forward the retrieved byte data to {@link #from(byte[])}. * * @param stream * A not-null InputStream. * * @throws IllegalArgumentException * if the provided stream is null * @throws IOException * If the first byte cannot be read for any reason other than the end of the file, * if the input stream has been closed, or if some other I/O error occurs. * * @return An Icon instance representing the specified InputStream * * @see net.dv8tion.jda.core.utils.IOUtil#readFully(InputStream) */ public static Icon from(InputStream stream) throws IOException { Checks.notNull(stream, "InputStream"); return from(IOUtil.readFully(stream)); }
@Override protected RequestBody finalizeData() { final JSONObject object = new JSONObject(); object.put("name", this.name); // required if (this.description != null && !this.description.isEmpty()) object.put("description", this.description); if (this.icon != null) object.put("icon", this.icon.getEncoding()); return getRequestBody(object); }
/** * Creates an {@link Icon Icon} with the specified {@link java.io.File File}. * <br>We here read the specified File and forward the retrieved byte data to {@link #from(byte[])}. * * @param file * An existing, not-null file. * * @throws IllegalArgumentException * if the provided file is either null or does not exist * @throws IOException * if there is a problem while reading the file. * * @return An Icon instance representing the specified File * * @see net.dv8tion.jda.core.utils.IOUtil#readFully(File) */ public static Icon from(File file) throws IOException { Checks.notNull(file, "Provided File"); Checks.check(file.exists(), "Provided file does not exist!"); return from(IOUtil.readFully(file)); }
@Override protected RequestBody finalizeData() { JSONObject data = new JSONObject(); if (shouldUpdate(NAME)) data.put("name", name); if (shouldUpdate(CHANNEL)) data.put("channel_id", channel); if (shouldUpdate(AVATAR)) data.put("avatar", avatar == null ? JSONObject.NULL : avatar.getEncoding()); return getRequestBody(data); }
@Override public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) { if (args.length == 0) { if (!message.getAttachments().isEmpty()) { Message.Attachment attachment = message.getAttachments().get(0); try { sender.getJDA().getSelfUser().getManager().setAvatar(Icon.from( new URL(attachment.getUrl()).openStream() )).complete(); } catch (IOException e) { channel.sendMessage("Failed to update avatar!! " + e).queue(); } channel.sendMessage("Success!").queue(); } else { channel.sendMessage("You must either attach an image or link to one!").queue(); } } else { try { sender.getJDA().getSelfUser().getManager().setAvatar(Icon.from( new URL(args[0]).openStream() )).complete(); } catch (IOException e) { channel.sendMessage("Failed to update avatar!! " + e).queue(); } channel.sendMessage("Success!").queue(); } }
@Override protected RequestBody finalizeData() { final JSONObject object = new JSONObject(); object.put("name", name); object.put("roles", new JSONArray(roles)); if (!channels.isEmpty()) object.put("channels", new JSONArray(channels)); if (icon != null) object.put("icon", icon.getEncoding()); if (verificationLevel != null) object.put("verification_level", verificationLevel.getKey()); if (notificationLevel != null) object.put("default_message_notifications", notificationLevel.getKey()); if (explicitContentLevel != null) object.put("explicit_content_filter", explicitContentLevel.getKey()); if (region != null) object.put("region", region.getKey()); return getRequestBody(object); }
@Override public String execute(DiscordBot bot, String[] args, MessageChannel channel, User author, Message inputMessage) { SimpleRank rank = bot.security.getSimpleRank(author); if (!rank.isAtLeast(SimpleRank.CREATOR)) { return Templates.no_permission.formatGuild(channel); } if (args.length <= 1) { try { Icon icon = Icon.from(Unirest.get(args[0]).asBinary().getBody()); bot.queue.add(channel.getJDA().getSelfUser().getManager().setAvatar(icon)); } catch (IOException | UnirestException e) { return "Error: " + e.getMessage(); } return ":+1:"; } return ":face_palm: I expected you to know how to use it"; } }
@Override protected RequestBody finalizeData() { JSONObject body = new JSONObject().put("name", getGuild().getName()); if (shouldUpdate(NAME)) body.put("name", name); if (shouldUpdate(REGION)) body.put("region", region); if (shouldUpdate(AFK_TIMEOUT)) body.put("afk_timeout", afkTimeout); if (shouldUpdate(ICON)) body.put("icon", icon == null ? JSONObject.NULL : icon.getEncoding()); if (shouldUpdate(SPLASH)) body.put("splash", splash == null ? JSONObject.NULL : splash.getEncoding()); if (shouldUpdate(AFK_CHANNEL)) body.put("afk_channel_id", opt(afkChannel)); if (shouldUpdate(SYSTEM_CHANNEL)) body.put("system_channel_id", opt(systemChannel)); if (shouldUpdate(VERIFICATION_LEVEL)) body.put("verification_level", verificationLevel); if (shouldUpdate(NOTIFICATION_LEVEL)) body.put("default_message_notifications", notificationLevel); if (shouldUpdate(MFA_LEVEL)) body.put("mfa_level", mfaLevel); if (shouldUpdate(EXPLICIT_CONTENT_LEVEL)) body.put("explicit_content_filter", explicitContentLevel); reset(); //now that we've built our JSON object, reset the manager back to the non-modified state return getRequestBody(body); }
@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."); } } } }
body.put("image", icon.getEncoding()); if (roles.length > 0) // making sure none of the provided roles are null before mapping them to the snowflake id body.put("roles", Stream.of(roles).filter(Objects::nonNull).map(ISnowflake::getId).collect(Collectors.toSet()));
@Override protected RequestBody finalizeData() { boolean isClient = getJDA().getAccountType() == AccountType.CLIENT; Checks.check(!isClient || (currentPassword != null && !currentPassword.isEmpty()), "Provided client account password to be used in auth is null or empty!"); JSONObject body = new JSONObject(); //Required fields. Populate with current values.. body.put("username", getSelfUser().getName()); body.put("avatar", opt(getSelfUser().getAvatarId())); if (shouldUpdate(NAME)) body.put("username", name); if (shouldUpdate(AVATAR)) body.put("avatar", avatar == null ? JSONObject.NULL : avatar.getEncoding()); if (isClient) { //Required fields. Populate with current values. body.put("password", currentPassword); body.put("email", email); if (shouldUpdate(EMAIL)) body.put("email", email); if (shouldUpdate(PASSWORD)) body.put("new_password", password); } reset(); return getRequestBody(body); }
@Override protected RequestBody finalizeData() { JSONObject body = new JSONObject(); body.put("description", shouldUpdate(DESCRIPTION) ? opt(description) : application.getDescription()); body.put("bot_require_code_grant", shouldUpdate(CODE_GRANT) ? isCodeGrant : application.doesBotRequireCodeGrant()); body.put("icon", shouldUpdate(ICON) ? (icon == null ? JSONObject.NULL : icon.getEncoding()) : application.getIconUrl()); body.put("bot_public", shouldUpdate(PUBLIC) ? isPublic : application.isBotPublic()); body.put("name", shouldUpdate(NAME) ? name : application.getName()); withLock(this.redirectUris, (list) -> body.put("redirect_uris", shouldUpdate(REDIRECT_URI) ? list : application.getRedirectUris())); reset(); return getRequestBody(body); } }