use of org.javacord.core.entity.emoji.KnownCustomEmojiImpl in project Javacord by BtoBastian.
the class GuildEmojisUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
long id = packet.get("guild_id").asLong();
api.getPossiblyUnreadyServerById(id).map(server -> (ServerImpl) server).ifPresent(server -> {
HashMap<Long, JsonNode> emojis = new HashMap<>();
for (JsonNode emojiJson : packet.get("emojis")) {
emojis.put(emojiJson.get("id").asLong(), emojiJson);
}
emojis.forEach((key, value) -> {
Optional<KnownCustomEmoji> optionalEmoji = server.getCustomEmojiById(key);
if (optionalEmoji.isPresent()) {
KnownCustomEmoji emoji = optionalEmoji.get();
String oldName = emoji.getName();
String newName = value.get("name").asText();
if (!Objects.deepEquals(oldName, newName)) {
KnownCustomEmojiChangeNameEvent event = new KnownCustomEmojiChangeNameEventImpl(emoji, newName, oldName);
((KnownCustomEmojiImpl) emoji).setName(newName);
api.getEventDispatcher().dispatchKnownCustomEmojiChangeNameEvent(server, emoji, server, event);
}
Collection<Role> oldWhitelist = emoji.getWhitelistedRoles().orElse(Collections.emptySet());
JsonNode newWhitelistJson = value.get("roles");
Collection<Role> newWhitelist = new ArrayList<>();
if (newWhitelistJson != null && !newWhitelistJson.isNull()) {
for (JsonNode role : newWhitelistJson) {
server.getRoleById(role.asLong()).ifPresent(newWhitelist::add);
}
}
if (!newWhitelist.containsAll(oldWhitelist) || !oldWhitelist.containsAll(newWhitelist)) {
KnownCustomEmojiChangeWhitelistedRolesEvent event = new KnownCustomEmojiChangeWhitelistedRolesEventImpl(emoji, newWhitelist, oldWhitelist);
((KnownCustomEmojiImpl) emoji).setWhitelist(newWhitelist);
api.getEventDispatcher().dispatchKnownCustomEmojiChangeWhitelistedRolesEvent(server, emoji, server, event);
}
} else {
KnownCustomEmoji emoji = api.getOrCreateKnownCustomEmoji(server, value);
server.addCustomEmoji(emoji);
KnownCustomEmojiCreateEvent event = new KnownCustomEmojiCreateEventImpl(emoji);
api.getEventDispatcher().dispatchKnownCustomEmojiCreateEvent(server, server, event);
}
});
Set<Long> emojiIds = emojis.keySet();
server.getCustomEmojis().stream().filter(emoji -> !emojiIds.contains(emoji.getId())).forEach(emoji -> {
api.removeCustomEmoji(emoji);
server.removeCustomEmoji(emoji);
KnownCustomEmojiDeleteEvent event = new KnownCustomEmojiDeleteEventImpl(emoji);
api.getEventDispatcher().dispatchKnownCustomEmojiDeleteEvent(server, emoji, server, event);
});
});
}
Aggregations