/** * Sets the avatar for the currently logged in account * * <p><b>Client-Accounts ({@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT}) require the * current password to be updated. See {@link #setAvatar(net.dv8tion.jda.core.entities.Icon, String) #setAvatar(Icon, String)}</b> * * @param avatar * An {@link net.dv8tion.jda.core.entities.Icon Icon} instance representing * the new Avatar for the current account, {@code null} to reset the avatar to the default avatar. * * @return AccountManager for chaining convenience */ @CheckReturnValue public AccountManager setAvatar(Icon avatar) { return setAvatar(avatar, null); }
/** * Sets the username for the currently logged in account * * <p><b>Client-Accounts ({@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT}) require the * current password to be updated. See {@link #setName(String, String)}</b> * * @param name * The new username * * @throws IllegalArgumentException * If the provided name is: * <ul> * <li>Equal to {@code null}</li> * <li>Less than {@code 2} or more than {@code 32} characters in length</li> * </ul> * * @return AccountManager for chaining convenience */ @CheckReturnValue public AccountManager setName(String name) { return setName(name, null); }
@Override public AccountManager getManager() { AccountManager mng = manager; if (mng == null) { mng = MiscUtil.locked(mngLock, () -> { if (manager == null) manager = new AccountManager(this); return manager; }); } return mng; }
@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 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 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."); } } } }
@Override protected void execute(CommandEvent event) { try { String oldname = event.getSelfUser().getName(); event.getSelfUser().getManager().setName(event.getArgs()).complete(false); event.reply(event.getClient().getSuccess()+" Name changed from `"+oldname+"` to `"+event.getArgs()+"`"); } catch(RateLimitedException e) { event.reply(event.getClient().getError()+" Name can only be changed twice per hour!"); } catch(Exception e) { event.reply(event.getClient().getError()+" That name is not valid!"); } } }
@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"; } }