/** * Creates a new EmoteManagerUpdatable instance * * @param emote * The target {@link net.dv8tion.jda.core.entities.impl.EmoteImpl EmoteImpl} to modify * * @throws java.lang.IllegalStateException * If the specified Emote is {@link net.dv8tion.jda.core.entities.Emote#isFake() fake} or {@link net.dv8tion.jda.core.entities.Emote#isManaged() managed}. */ public EmoteManagerUpdatable(EmoteImpl emote) { if (emote.isFake()) throw new IllegalStateException("The emote you are trying to update is not an actual emote we have access to (it is fake)!"); if (emote.isManaged()) throw new IllegalStateException("You cannot modify a managed emote!"); this.emote = emote; checkPermission(Permission.MANAGE_EMOTES); setupFields(); }
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 protected Long handleInternally(JSONObject content) { JSONObject emoji = content.getJSONObject("emoji"); final long userId = content.getLong("user_id"); final long messageId = content.getLong("message_id"); final long channelId = content.getLong("channel_id"); final Long emojiId = emoji.isNull("id") ? null : emoji.getLong("id"); String emojiName = emoji.optString("name", null); final boolean emojiAnimated = emoji.optBoolean("animated"); if (emojiId == null && emojiName == null) { WebSocketClient.LOG.debug("Received a reaction {} with no name nor id. json: {}", JDALogger.getLazyString(() -> add ? "add" : "remove"), content); return null; } User user = api.getUserById(userId); if (user == null) user = api.getFakeUserMap().get(userId); if (user == null) { api.getEventCache().cache(EventCache.Type.USER, userId, () -> handle(responseNumber, allContent)); EventCache.LOG.debug("Received a reaction for a user that JDA does not currently have cached"); return null; } MessageChannel channel = api.getTextChannelById(channelId); if (channel == null) { channel = api.getPrivateChannelById(channelId); } if (channel == null) { channel = api.getFakePrivateChannelMap().get(channelId); } if (channel == null && api.getAccountType() == AccountType.CLIENT) { channel = api.asClient().getGroupById(channelId); } if (channel == null) { api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent)); EventCache.LOG.debug("Received a reaction for a channel that JDA does not currently have cached"); return null; } MessageReaction.ReactionEmote rEmote; if (emojiId != null) { Emote emote = api.getEmoteById(emojiId); if (emote == null) { if (emojiName != null) { emote = new EmoteImpl(emojiId, api).setAnimated(emojiAnimated).setName(emojiName); } else { WebSocketClient.LOG.debug("Received a reaction {} with a null name. json: {}", JDALogger.getLazyString(() -> add ? "add" : "remove"), content); return null; } } rEmote = new MessageReaction.ReactionEmote(emote); } else { rEmote = new MessageReaction.ReactionEmote(emojiName, null, api); } MessageReaction reaction = new MessageReaction(channel, rEmote, messageId, user.equals(api.getSelfUser()), -1); if (add) onAdd(reaction, user); else onRemove(reaction, user); return null; }
/** * Creates a new EmoteManager instance * * @param emote * The target {@link net.dv8tion.jda.core.entities.impl.EmoteImpl EmoteImpl} to modify * * @throws net.dv8tion.jda.core.exceptions.AccountTypeException * If the currently logged in account is not from {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT} * @throws java.lang.IllegalStateException * If the specified Emote is {@link net.dv8tion.jda.core.entities.Emote#isFake() fake} or {@link net.dv8tion.jda.core.entities.Emote#isManaged() managed}. */ public EmoteManager(EmoteImpl emote) { this.updatable = new EmoteManagerUpdatable(emote); }