@Override public void action(String head, String[] args, MessageReceivedEvent event) { AccountManagerUpdatable manager = Main.getShardManager().getJDA().getSelfUser().getManagerUpdatable(); if(event.getMessage().getAttachments().isEmpty()) return; Message.Attachment attachment = event.getMessage().getAttachments().get(0); try { File file = new File(attachment.getFileName()); attachment.download(file); Icon icon = Icon.from(file); manager.getAvatarField().setValue(icon).update().complete(); MessageUtilities.sendPrivateMsg("Updated bot avatar!", event.getAuthor(), null); file.delete(); } catch (IOException e) { Logging.exception(this.getClass(), e); MessageUtilities.sendPrivateMsg("Failed to update bot avatar!", event.getAuthor(), null); } }
@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."); } } }
public static void changeBotAvatar(File image) { try { Bot.getInstance().getBots().get(0).getSelfUser().getManager().setAvatar(Icon.from(image)).queue(); } catch (Exception e) { LoggerFactory.getLogger(State.class).error("Error changing avatar"); } }
@Override protected void doCommand(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) { respond(event, event.getClient().getError() + " " + Locale.getCommandsMessage("setavatar.invalid").finish()); } else { try { event.getSelfUser().getManager().setAvatar(Icon.from(s)).queue( v -> respond(event, event.getClient().getSuccess() + " " + Locale.getCommandsMessage("setavatar.changed").finish()), t -> respond(event,event.getClient().getError() + " " + Locale.getCommandsMessage("setavatar.failed").finish())); } catch (IOException e) { respond(event, event.getClient().getError() + " " + Locale.getCommandsMessage("setavatar.couldNotLoad").finish()); } } }
@Override protected void execute(CommandEvent event) { final List<Emote> currentEmotes = event.getGuild().getEmotes(); final Set<String> emoteNamesToInstall = new HashSet<>(Arrays.asList("mystic", "valor", "instinct")); boolean emotesAlreadyInstalled = false; for (Emote emote : currentEmotes) { final String emoteToInstall = emote.getName().toLowerCase(); if (emoteNamesToInstall.contains(emoteToInstall)) { event.reply(localeService.getMessageFor(LocaleService.EMOTE_INSTALLED_ALREADY, localeService.getLocaleForUser(event.getAuthor()), emoteToInstall)); emotesAlreadyInstalled = true; } } if (emotesAlreadyInstalled) { return; } final InputStream mysticPngResource = InstallEmotesCommand.class.getResourceAsStream("/static/img/mystic.png"); final InputStream valorPngResource = InstallEmotesCommand.class.getResourceAsStream("/static/img/valor.png"); final InputStream instinctPngResource = InstallEmotesCommand.class.getResourceAsStream("/static/img/instinct.png"); try { event.reply("Installing icons for Pokemon go teams..."); createEmote("mystic", event, Icon.from(mysticPngResource)); createEmote("valor", event, Icon.from(valorPngResource)); createEmote("instinct", event, Icon.from(instinctPngResource)); event.reply("Emotes installed. Try them out: :mystic: :valor: :instinct:"); } catch (Throwable t) { event.reply(t.getMessage()); } }
private void createEmote(String iconName, CommandEvent commandEvent, Icon icon, Role... roles) { JSONObject body = new JSONObject(); body.put("name", iconName); 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())); } // todo: check bot permissions, must be able to handle emojis GuildImpl guild = (GuildImpl) commandEvent.getGuild(); JDA jda = commandEvent.getJDA(); Route.CompiledRoute route = Route.Emotes.CREATE_EMOTE.compile(guild.getId()); AuditableRestAction<Emote> action = new AuditableRestAction<Emote>(jda, route, body) { @Override protected void handleResponse(Response response, Request<Emote> request) { if (response.isOk()) { JSONObject obj = response.getObject(); final long id = obj.getLong("id"); String name = obj.getString("name"); EmoteImpl emote = new EmoteImpl(id, guild).setName(name); // managed is false by default, should always be false for emotes created by client accounts. JSONArray rolesArr = obj.getJSONArray("roles"); Set<Role> roleSet = emote.getRoleSet(); for (int i = 0; i < rolesArr.length(); i++) { roleSet.add(guild.getRoleById(rolesArr.getString(i))); } // put emote into cache guild.getEmoteMap().put(id, emote); request.onSuccess(emote); } else { request.onFailure(response); throw new RuntimeException("Couldn't install emojis. " + "Make sure that pokeraidbot has access to manage emojis."); } } }; action.queue(); }
@Override public void onCommand(MessageReceivedEvent e, String[] args) { // set <> <> if (args.length == 3) { // set avatar <> if (args[1].equalsIgnoreCase("avatar")) { try (InputStream is = new URL(args[2]).openStream()) { e.getJDA().getSelfUser().getManager().setAvatar(Icon.from(is)); SendMessage.sendMessage(e, "Successfully set avatar."); } catch (IOException ex) { SendMessage.sendMessage(e, "Error: Failed to set avatar. " + ex.getMessage()); } } else if (args[1].equalsIgnoreCase("nickname")) { Guild guild = e.getGuild(); guild.getManager().setName(args[2]); SendMessage.sendMessage(e, "Successfully set nickname."); } } // !set <> <> <> else if (args.length >= 4) { // !set config <> <> if (args[1].equalsIgnoreCase("config")) { Config config = ConfigManager.getInstance().getConfig(); // !set config name <newName> if (args[2].equalsIgnoreCase("name") || args[2].equalsIgnoreCase("username")) { String newName = remainingArgs(args, 3); config.setBotName(newName); e.getJDA().getSelfUser().getManager().setName(newName); } // !set config game <newGame> else if (args[2].equalsIgnoreCase("game")) { String newGame = remainingArgs(args, 3); config.setBotGame(newGame); e.getJDA().getPresence().setGame(Game.of(newGame)); } ConfigManager.getInstance().saveConfig(); } else if (args[1].equalsIgnoreCase("setting") || args[1].equalsIgnoreCase("settings")) { if (args[2].equalsIgnoreCase("prefix")) { // Prefix requires 5 arguments if (args.length != 5) { SendMessage.sendMessage(e, "Error: Incorrect usage."); return; } Settings settings; if (args[3].equalsIgnoreCase("null") || args[3].equalsIgnoreCase("-")) { settings = SettingsManager.getInstance(null).getSettings(); } else if (NumberUtils.isDigits(args[3])) { if (e.getJDA().getGuildById(args[3]) != null) { settings = SettingsManager.getInstance(args[3]).getSettings(); } else { SendMessage.sendMessage(e, "Error: Could not find guild with ID " + args[3] + "."); return; } } else { SendMessage.sendMessage(e, "Error: Unknown argument " + args[3]); return; } settings.setPrefix(args[4]); SendMessage.sendMessage(e, "Set prefix for " + (args[3] == null ? "global" : args[3]) + " to \"" + args[4] + "\"."); } } } }
protected void setupFields() { name = new AccountField<String>(this, selfUser::getName) { @Override public void checkValue(String value) { Checks.notNull(value, "account name"); if (value.length() < 2 || value.length() > 32) throw new IllegalArgumentException("Provided name must be 2 to 32 characters in length"); } }; avatar = new AccountField<Icon>(this, null) { @Override public void checkValue(Icon value) { } @Override public Icon getOriginalValue() { throw new UnsupportedOperationException("Cannot easily provide the original Avatar. Use User#getIconUrl() and download it yourself."); } @Override public boolean shouldUpdate() { return isSet(); } }; if (isType(AccountType.CLIENT)) { email = new AccountField<String>(this, selfUser::getEmail) { @Override public void checkValue(String value) { Checks.notNull(value, "account email"); if (!EMAIL_PATTERN.matcher(value).find()) throw new IllegalArgumentException("Provided email is in invalid format. Provided value: " + value); } }; password = new AccountField<String>(this, null) { @Override public void checkValue(String value) { Checks.notNull(value, "account password"); if (value.length() < 6 || value.length() > 128) throw new IllegalArgumentException("Provided password must ben 6 to 128 characters in length"); } @Override public String getOriginalValue() { throw new UnsupportedOperationException("Cannot get the original password. We are not given this information."); } @Override public boolean shouldUpdate() { return isSet(); } }; } }
/** * An {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField} * for the {@link net.dv8tion.jda.core.entities.Icon Icon} of the selected {@link net.dv8tion.jda.core.entities.Guild Guild}. * <br>To reset the Icon of a Guild provide {@code null} to {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Icon)}. * * <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Icon)} * on the returned {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField} instance. * * @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException * If the Guild is temporarily not {@link net.dv8tion.jda.core.entities.Guild#isAvailable() available} * * @return {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField} - Type: {@link net.dv8tion.jda.core.entities.Icon Icon} */ public GuildField<Icon> getIconField() { checkAvailable(); return icon; }
/** * An {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField} * for the <b><u>splash {@link net.dv8tion.jda.core.entities.Icon Icon}</u></b> of the selected {@link net.dv8tion.jda.core.entities.Guild Guild}. * <br>To reset the splash of a Guild provide {@code null} to {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Icon)}. * * <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Icon)} * on the returned {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField} instance. * * @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException * If the Guild is temporarily not {@link net.dv8tion.jda.core.entities.Guild#isAvailable() available} * * @return {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField} - Type: {@link net.dv8tion.jda.core.entities.Icon Icon} */ public GuildField<Icon> getSplashField() { checkAvailable(); return splash; }
/** * Sets the {@link net.dv8tion.jda.core.entities.Icon Icon} * for the resulting {@link net.dv8tion.jda.core.entities.Guild Guild} * * @param icon * The {@link net.dv8tion.jda.core.entities.Icon Icon} to use * * @return The current GuildAction for chaining convenience */ @CheckReturnValue public GuildAction setIcon(Icon icon) { this.icon = icon; return this; }
/** * Sets the <b>Avatar</b> for the custom Webhook User * * @param icon * An {@link net.dv8tion.jda.core.entities.Icon Icon} for the new avatar. * Or null to use default avatar. * * @return The current WebhookAction for chaining convenience. */ @CheckReturnValue public WebhookAction setAvatar(Icon icon) { this.avatar = icon; return this; }
/** * Sets the <b><u>icon</u></b> of the selected {@link net.dv8tion.jda.client.entities.Application Application}. * <br>Wraps {@link net.dv8tion.jda.client.managers.ApplicationManagerUpdatable#getIconField() ApplicationManagerUpdatable#getIconField()}. * * @param icon * The new {@link net.dv8tion.jda.core.entities.Icon Icon} * for the selected {@link net.dv8tion.jda.client.entities.Application Application} * * @return {@link net.dv8tion.jda.core.requests.RestAction} * <br>Update RestAction from {@link ApplicationManagerUpdatable#update() #update()} * * @see net.dv8tion.jda.client.managers.ApplicationManagerUpdatable#getIconField() * @see net.dv8tion.jda.client.managers.ApplicationManagerUpdatable#update() */ @CheckReturnValue public RestAction<Void> setIcon(final Icon icon) { return this.updatable.getIconField().setValue(icon).update(); }
/** * An {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} * for the {@link net.dv8tion.jda.core.entities.Icon Icon} of the selected * {@link net.dv8tion.jda.client.entities.Application Application}. * * <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Icon)} * on the returned {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} instance. * * @return {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} * - Type: {@link net.dv8tion.jda.core.entities.Icon Icon} */ public final ApplicationField<Icon> getIconField() { return this.icon; }
/** * Sets the {@link net.dv8tion.jda.core.entities.Icon Icon} of the selected {@link net.dv8tion.jda.client.entities.Application Application}. * * @param icon * The {@link net.dv8tion.jda.core.entities.Icon Icon} for new {@link net.dv8tion.jda.client.entities.Application Application} * * @return The current ApplicationAction for chaining */ public ApplicationAction setIcon(final Icon icon) { this.icon = icon; return this; }
/** * Sets the <b><u>default avatar</u></b> of the selected {@link net.dv8tion.jda.core.entities.Webhook Webhook}. * <br>Wraps {@link net.dv8tion.jda.core.managers.WebhookManagerUpdatable#getAvatarField()} * * @param icon * The new default avatar {@link net.dv8tion.jda.core.entities.Icon Icon} * for the selected {@link net.dv8tion.jda.core.entities.Webhook Webhook} * * @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException * If the currently logged in account does not have the Permission {@link net.dv8tion.jda.core.Permission#MANAGE_WEBHOOKS MANAGE_WEBHOOKS} * * @return {@link net.dv8tion.jda.core.requests.restaction.AuditableRestAction AuditableRestAction} * <br>Update RestAction from {@link WebhookManagerUpdatable#update() #update()} * * @see net.dv8tion.jda.core.managers.WebhookManagerUpdatable#getAvatarField() * @see net.dv8tion.jda.core.managers.WebhookManagerUpdatable#update() */ @CheckReturnValue public AuditableRestAction<Void> setAvatar(Icon icon) { return manager.getAvatarField().setValue(icon).update(); }
/** * Sets the {@link net.dv8tion.jda.core.entities.Icon Icon} of this {@link net.dv8tion.jda.core.entities.Guild Guild}. * More information can be found {@link GuildManagerUpdatable#getIconField() here}! * * <p>For information on possible {@link net.dv8tion.jda.core.requests.ErrorResponse ErrorResponses} * by the returned {@link net.dv8tion.jda.core.requests.RestAction RestAction} see {@link GuildManagerUpdatable#update() #update()} * * @param icon * The new icon for this {@link net.dv8tion.jda.core.entities.Guild Guild} * * @return {@link net.dv8tion.jda.core.requests.restaction.AuditableRestAction AuditableRestAction} * <br>Update RestAction from {@link GuildManagerUpdatable#update() #update()} * * @see net.dv8tion.jda.core.managers.GuildManagerUpdatable#getIconField() * @see net.dv8tion.jda.core.managers.GuildManagerUpdatable#update() */ @CheckReturnValue public AuditableRestAction<Void> setIcon(Icon icon) { return updatable.getIconField().setValue(icon).update(); }
/** * Sets the Splash {@link net.dv8tion.jda.core.entities.Icon Icon} of this {@link net.dv8tion.jda.core.entities.Guild Guild}. * More information can be found {@link GuildManagerUpdatable#getSplashField() here}! * * <p>For information on possible {@link net.dv8tion.jda.core.requests.ErrorResponse ErrorResponses} * by the returned {@link net.dv8tion.jda.core.requests.RestAction RestAction} see {@link GuildManagerUpdatable#update() #update()} * * @param splash * The new splash for this {@link net.dv8tion.jda.core.entities.Guild Guild} * * @return {@link net.dv8tion.jda.core.requests.restaction.AuditableRestAction AuditableRestAction} * <br>Update RestAction from {@link GuildManagerUpdatable#update() #update()} * * @see net.dv8tion.jda.core.managers.GuildManagerUpdatable#getSplashField() * @see net.dv8tion.jda.core.managers.GuildManagerUpdatable#update() */ @CheckReturnValue public AuditableRestAction<Void> setSplash(Icon splash) { return updatable.getSplashField().setValue(splash).update(); }
/** * An {@link net.dv8tion.jda.core.managers.fields.AccountField AccountField} * for the {@code avatar} of the currently logged in account. * * <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Icon)} * on the returned {@link net.dv8tion.jda.core.managers.fields.AccountField AccountField} instance. * <br>An {@link net.dv8tion.jda.core.entities.Icon Icon} can be retrieved through one of the static {@code Icon.from(...)} methods * * <p>Providing {@code null} as value will cause the {@link net.dv8tion.jda.core.entities.SelfUser#getDefaultAvatarId() default avatar} for this account to be used. * <br>Otherwise {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) Field.setValue(...)} will * throw an {@link IllegalArgumentException IllegalArgumentException}. * * @return {@link net.dv8tion.jda.core.managers.fields.AccountField AccountField} - Type: {@link net.dv8tion.jda.core.entities.Icon Icon} */ public AccountField<Icon> getAvatarField() { return avatar; }
/** * Sets the avatar for the currently logged in account * <br>More information can be found {@link AccountManagerUpdatable#getAvatarField() here}! * * <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 {@link net.dv8tion.jda.core.requests.RestAction} * The update RestAction that will set the provided icon as the new avatar or reset the avatar. * <br>See {@link net.dv8tion.jda.core.managers.AccountManagerUpdatable#update(String) #update()} for more information */ @CheckReturnValue public RestAction<Void> setAvatar(Icon avatar) { return setAvatar(avatar, null); }
/** * Sets the avatar for the currently logged in account * <br>More information can be found {@link AccountManagerUpdatable#getAvatarField() here}! * * @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. * @param currentPassword * The current password for the represented account, * this is only required for {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT} * * @throws IllegalArgumentException * If the provided {@code currentPassword} is {@code null} or empty and the currently * logged in account is from {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT} * * @return {@link net.dv8tion.jda.core.requests.RestAction} * The update RestAction that will set the provided icon as the new avatar or reset the avatar. * <br>See {@link net.dv8tion.jda.core.managers.AccountManagerUpdatable#update(String) #update()} for more information */ @CheckReturnValue public RestAction<Void> setAvatar(Icon avatar, String currentPassword) { return updatable.getAvatarField().setValue(avatar).update(currentPassword); }