Search in sources :

Example 1 with EmoteRemovedEvent

use of net.dv8tion.jda.api.events.emote.EmoteRemovedEvent 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;
}
Also used : EmoteRemovedEvent(net.dv8tion.jda.api.events.emote.EmoteRemovedEvent) Emote(net.dv8tion.jda.api.entities.Emote) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Role(net.dv8tion.jda.api.entities.Role) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) EmoteImpl(net.dv8tion.jda.internal.entities.EmoteImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) EmoteAddedEvent(net.dv8tion.jda.api.events.emote.EmoteAddedEvent)

Example 2 with EmoteRemovedEvent

use of net.dv8tion.jda.api.events.emote.EmoteRemovedEvent in project Sx4 by sx4-discord-bot.

the class LoggerHandler method onEmoteRemoved.

public void onEmoteRemoved(EmoteRemovedEvent event) {
    Guild guild = event.getGuild();
    Emote emote = event.getEmote();
    LoggerEvent loggerEvent = LoggerEvent.EMOTE_DELETE;
    LoggerContext loggerContext = new LoggerContext().setEmote(emote);
    WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
    embed.setDescription(String.format("The emote `%s` has been deleted", emote.getName()));
    embed.setColor(this.bot.getConfig().getRed());
    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_DELETE).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 deleted by **%s**", emote.getName(), moderator.getAsTag()));
                }
                this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
            });
        } else {
            this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
        }
    });
}
Also used : MessageBulkDeleteEvent(net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent) Document(org.bson.Document) PermissionOverrideCreateEvent(net.dv8tion.jda.api.events.guild.override.PermissionOverrideCreateEvent) RoleCreateEvent(net.dv8tion.jda.api.events.role.RoleCreateEvent) StoreChannelDeleteEvent(net.dv8tion.jda.api.events.channel.store.StoreChannelDeleteEvent) Permission(net.dv8tion.jda.api.Permission) ZonedDateTime(java.time.ZonedDateTime) Aggregates(com.mongodb.client.model.Aggregates) EmoteUpdateRolesEvent(net.dv8tion.jda.api.events.emote.update.EmoteUpdateRolesEvent) AuditLogChange(net.dv8tion.jda.api.audit.AuditLogChange) CategoryCreateEvent(net.dv8tion.jda.api.events.channel.category.CategoryCreateEvent) LoggerManager(com.sx4.bot.managers.LoggerManager) ColourUtility(com.sx4.bot.utility.ColourUtility) TextChannelDeleteEvent(net.dv8tion.jda.api.events.channel.text.TextChannelDeleteEvent) EmoteAddedEvent(net.dv8tion.jda.api.events.emote.EmoteAddedEvent) TextChannelUpdateNameEvent(net.dv8tion.jda.api.events.channel.text.update.TextChannelUpdateNameEvent) TextChannelCreateEvent(net.dv8tion.jda.api.events.channel.text.TextChannelCreateEvent) Duration(java.time.Duration) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) ZoneOffset(java.time.ZoneOffset) LoggerContext(com.sx4.bot.entities.management.LoggerContext) LoggerEvent(com.sx4.bot.entities.management.LoggerEvent) AuditLogKey(net.dv8tion.jda.api.audit.AuditLogKey) PermissionOverrideDeleteEvent(net.dv8tion.jda.api.events.guild.override.PermissionOverrideDeleteEvent) EmbedFooter(club.minnced.discord.webhook.send.WebhookEmbed.EmbedFooter) GuildMessageUpdateEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent) java.util.concurrent(java.util.concurrent) EmoteUpdateNameEvent(net.dv8tion.jda.api.events.emote.update.EmoteUpdateNameEvent) CategoryDeleteEvent(net.dv8tion.jda.api.events.channel.category.CategoryDeleteEvent) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) GuildBanEvent(net.dv8tion.jda.api.events.guild.GuildBanEvent) GuildMemberRemoveEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent) EventListener(net.dv8tion.jda.api.hooks.EventListener) CategoryUpdateNameEvent(net.dv8tion.jda.api.events.channel.category.update.CategoryUpdateNameEvent) GuildMemberUpdateNicknameEvent(net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent) NotNull(org.jetbrains.annotations.NotNull) RoleUpdateColorEvent(net.dv8tion.jda.api.events.role.update.RoleUpdateColorEvent) ActionType(net.dv8tion.jda.api.audit.ActionType) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) VoiceChannelUpdateNameEvent(net.dv8tion.jda.api.events.channel.voice.update.VoiceChannelUpdateNameEvent) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) EmbedAuthor(club.minnced.discord.webhook.send.WebhookEmbed.EmbedAuthor) GuildMemberRoleRemoveEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent) RoleUpdatePermissionsEvent(net.dv8tion.jda.api.events.role.update.RoleUpdatePermissionsEvent) VoiceChannelDeleteEvent(net.dv8tion.jda.api.events.channel.voice.VoiceChannelDeleteEvent) Projections(com.mongodb.client.model.Projections) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) TLongIntHashMap(gnu.trove.map.hash.TLongIntHashMap) EmbedField(club.minnced.discord.webhook.send.WebhookEmbed.EmbedField) AuditLogEntry(net.dv8tion.jda.api.audit.AuditLogEntry) Filters(com.mongodb.client.model.Filters) GuildMessageDeleteEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent) Bson(org.bson.conversions.Bson) Pair(net.dv8tion.jda.internal.utils.tuple.Pair) WebhookEmbedBuilder(club.minnced.discord.webhook.send.WebhookEmbedBuilder) Sx4(com.sx4.bot.core.Sx4) GuildMemberRoleAddEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) EmoteRemovedEvent(net.dv8tion.jda.api.events.emote.EmoteRemovedEvent) Operators(com.sx4.bot.database.mongo.model.Operators) Accumulators(com.mongodb.client.model.Accumulators) TLongIntMap(gnu.trove.map.TLongIntMap) VoiceChannelCreateEvent(net.dv8tion.jda.api.events.channel.voice.VoiceChannelCreateEvent) LoggerUtility(com.sx4.bot.utility.LoggerUtility) RoleUpdateNameEvent(net.dv8tion.jda.api.events.role.update.RoleUpdateNameEvent) StoreChannelUpdateNameEvent(net.dv8tion.jda.api.events.channel.store.update.StoreChannelUpdateNameEvent) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) OkHttpClient(okhttp3.OkHttpClient) PermissionOverrideUpdateEvent(net.dv8tion.jda.api.events.guild.override.PermissionOverrideUpdateEvent) net.dv8tion.jda.api.events.guild.voice(net.dv8tion.jda.api.events.guild.voice) StoreChannelCreateEvent(net.dv8tion.jda.api.events.channel.store.StoreChannelCreateEvent) GuildMessage(com.sx4.bot.entities.cache.GuildMessage) GuildUnbanEvent(net.dv8tion.jda.api.events.guild.GuildUnbanEvent) TLongObjectMap(gnu.trove.map.TLongObjectMap) RoleDeleteEvent(net.dv8tion.jda.api.events.role.RoleDeleteEvent) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) StringUtility(com.sx4.bot.utility.StringUtility) GuildMemberJoinEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent) EmbedFooter(club.minnced.discord.webhook.send.WebhookEmbed.EmbedFooter) EmbedAuthor(club.minnced.discord.webhook.send.WebhookEmbed.EmbedAuthor) WebhookEmbedBuilder(club.minnced.discord.webhook.send.WebhookEmbedBuilder) LoggerEvent(com.sx4.bot.entities.management.LoggerEvent) Document(org.bson.Document) LoggerContext(com.sx4.bot.entities.management.LoggerContext)

Aggregations

EmoteAddedEvent (net.dv8tion.jda.api.events.emote.EmoteAddedEvent)2 EmoteRemovedEvent (net.dv8tion.jda.api.events.emote.EmoteRemovedEvent)2 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)1 EmbedAuthor (club.minnced.discord.webhook.send.WebhookEmbed.EmbedAuthor)1 EmbedField (club.minnced.discord.webhook.send.WebhookEmbed.EmbedField)1 EmbedFooter (club.minnced.discord.webhook.send.WebhookEmbed.EmbedFooter)1 WebhookEmbedBuilder (club.minnced.discord.webhook.send.WebhookEmbedBuilder)1 Accumulators (com.mongodb.client.model.Accumulators)1 Aggregates (com.mongodb.client.model.Aggregates)1 Filters (com.mongodb.client.model.Filters)1 Projections (com.mongodb.client.model.Projections)1 Sx4 (com.sx4.bot.core.Sx4)1 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)1 Operators (com.sx4.bot.database.mongo.model.Operators)1 GuildMessage (com.sx4.bot.entities.cache.GuildMessage)1 LoggerContext (com.sx4.bot.entities.management.LoggerContext)1 LoggerEvent (com.sx4.bot.entities.management.LoggerEvent)1 LoggerManager (com.sx4.bot.managers.LoggerManager)1 ColourUtility (com.sx4.bot.utility.ColourUtility)1 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)1