use of net.dv8tion.jda.api.events.emote.EmoteAddedEvent in project JDA by DV8FromTheWorld.
the class GuildEmojisUpdateHandler method handleInternally.
@Override
protected Long handleInternally(DataObject content) {
if (!getJDA().isCacheFlagSet(CacheFlag.EMOTE))
return null;
final long guildId = content.getLong("guild_id");
if (getJDA().getGuildSetupController().isLocked(guildId))
return guildId;
GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
if (guild == null) {
getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
return null;
}
DataArray array = content.getArray("emojis");
List<Emote> oldEmotes, newEmotes;
SnowflakeCacheViewImpl<Emote> emoteView = guild.getEmotesView();
try (UnlockHook hook = emoteView.writeLock()) {
TLongObjectMap<Emote> emoteMap = emoteView.getMap();
// snapshot of emote cache
oldEmotes = new ArrayList<>(emoteMap.valueCollection());
newEmotes = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
DataObject current = array.getObject(i);
final long emoteId = current.getLong("id");
EmoteImpl emote = (EmoteImpl) emoteMap.get(emoteId);
EmoteImpl oldEmote = null;
if (emote == null) {
emote = new EmoteImpl(emoteId, guild);
newEmotes.add(emote);
} else {
// emote is in our cache which is why we don't want to remove it in cleanup later
oldEmotes.remove(emote);
oldEmote = emote.clone();
}
emote.setName(current.getString("name")).setAnimated(current.getBoolean("animated")).setManaged(current.getBoolean("managed"));
// update roles
DataArray roles = current.getArray("roles");
Set<Role> newRoles = emote.getRoleSet();
// snapshot of cached roles
Set<Role> oldRoles = new HashSet<>(newRoles);
for (int j = 0; j < roles.length(); j++) {
Role role = guild.getRoleById(roles.getString(j));
if (role != null) {
newRoles.add(role);
oldRoles.remove(role);
}
}
// cleanup old cached roles that were not found in the JSONArray
for (Role r : oldRoles) {
// newRoles directly writes to the set contained in the emote
newRoles.remove(r);
}
// finally, update the emote
emoteMap.put(emote.getIdLong(), emote);
// check for updated fields and fire events
handleReplace(oldEmote, emote);
}
for (Emote e : oldEmotes) emoteMap.remove(e.getIdLong());
}
// cleanup old emotes that don't exist anymore
for (Emote e : oldEmotes) {
getJDA().handleEvent(new EmoteRemovedEvent(getJDA(), responseNumber, e));
}
for (Emote e : newEmotes) {
getJDA().handleEvent(new EmoteAddedEvent(getJDA(), responseNumber, e));
}
return null;
}
use of net.dv8tion.jda.api.events.emote.EmoteAddedEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onEmoteAdded.
public void onEmoteAdded(EmoteAddedEvent event) {
Guild guild = event.getGuild();
Emote emote = event.getEmote();
LoggerEvent loggerEvent = LoggerEvent.EMOTE_CREATE;
LoggerContext loggerContext = new LoggerContext().setEmote(emote);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The emote %s has been created", emote.getAsMention()));
embed.setColor(this.bot.getConfig().getGreen());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
embed.setFooter(new EmbedFooter(String.format("Emote ID: %s", emote.getId()), null));
this.bot.getMongo().aggregateLoggers(this.getPipeline(guild.getIdLong())).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (documents.isEmpty()) {
return;
}
Document data = documents.get(0);
List<Document> loggers = LoggerUtility.getValidLoggers(data.getList("loggers", Document.class), loggerEvent, loggerContext);
if (loggers.isEmpty()) {
return;
}
if (!emote.isManaged() && guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.EMOTE_CREATE).whenComplete((logs, auditException) -> {
User moderator = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).filter(e -> e.getTargetIdLong() == emote.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("The emote %s has been created by **%s**", emote.getAsMention(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
Aggregations