Search in sources :

Example 1 with AuditLogChange

use of net.dv8tion.jda.core.audit.AuditLogChange in project FlareBot by FlareBot.

the class ModlogEvents method onGenericRoleUpdate.

private void onGenericRoleUpdate(GenericRoleUpdateEvent event, @Nonnull GuildWrapper wrapper) {
    if (cannotHandle(wrapper, ModlogEvent.ROLE_EDIT))
        return;
    if (event instanceof RoleUpdatePositionEvent) {
        return;
    }
    if (event.getResponseNumber() == roleResponseNumber) {
        return;
    }
    roleResponseNumber = event.getResponseNumber();
    event.getGuild().getAuditLogs().limit(1).queue(auditLogs -> {
        AuditLogEntry entry = auditLogs.get(0);
        Map<String, AuditLogChange> changes = entry.getChanges();
        EmbedBuilder permissionsBuilder = new EmbedBuilder();
        permissionsBuilder.addField("Role", event.getRole().getName() + " (" + event.getRole().getId() + ")", true);
        if (changes.containsKey("permissions")) {
            AuditLogChange change = changes.get("permissions");
            Map<Boolean, List<Permission>> permChanges = GeneralUtils.getChanged(Permission.getPermissions(((Integer) change.getOldValue()).longValue()), Permission.getPermissions(((Integer) change.getNewValue()).longValue()));
            if (permChanges.get(true).size() > 0) {
                StringBuilder added = new StringBuilder();
                for (Permission addedPerm : permChanges.get(true)) {
                    added.append(addedPerm.getName()).append("\n");
                }
                permissionsBuilder.addField("Added Perms", "```\n" + added.toString() + "```", false);
            }
            if (permChanges.get(false).size() > 0) {
                StringBuilder removed = new StringBuilder();
                for (Permission removedPerm : permChanges.get(false)) {
                    removed.append(removedPerm.getName()).append("\n");
                }
                permissionsBuilder.addField("Removed Perms", "```\n" + removed.toString() + "```", false);
            }
        }
        if (changes.containsKey("name")) {
            AuditLogChange change = changes.get("name");
            permissionsBuilder.addField("Name Change", "`" + change.getOldValue() + "` -> `" + change.getNewValue() + "`", true);
        }
        if (changes.containsKey("mentionable")) {
            AuditLogChange change = changes.get("mentionable");
            permissionsBuilder.addField("Mentionable", "`" + change.getNewValue() + "`", true);
        }
        if (changes.containsKey("hoist")) {
            AuditLogChange change = changes.get("hoist");
            permissionsBuilder.addField("Displayed Separately", "`" + change.getNewValue() + "`", true);
        }
        if (changes.containsKey("color")) {
            AuditLogChange change = changes.get("color");
            permissionsBuilder.addField("Color Change", "`#" + Integer.toHexString(change.getOldValue()) + "` -> `#" + Integer.toHexString(change.getNewValue()) + "`", true);
        }
        ModlogHandler.getInstance().postToModlog(wrapper, ModlogEvent.ROLE_EDIT, entry.getUser(), permissionsBuilder);
    });
}
Also used : RoleUpdatePositionEvent(net.dv8tion.jda.core.events.role.update.RoleUpdatePositionEvent) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) AuditLogEntry(net.dv8tion.jda.core.audit.AuditLogEntry) AuditLogChange(net.dv8tion.jda.core.audit.AuditLogChange) Permission(net.dv8tion.jda.core.Permission) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with AuditLogChange

use of net.dv8tion.jda.core.audit.AuditLogChange in project FlareBot by FlareBot.

the class ModlogEvents method onGenericGuildUpdate.

private void onGenericGuildUpdate(GenericGuildUpdateEvent event, @Nonnull GuildWrapper wrapper) {
    if (event instanceof GuildUpdateExplicitContentLevelEvent) {
        return;
    }
    if (cannotHandle(wrapper, ModlogEvent.GUILD_UPDATE))
        return;
    if (event.getResponseNumber() == guildResponseNumber) {
        return;
    }
    guildResponseNumber = event.getResponseNumber();
    event.getGuild().getAuditLogs().limit(1).queue(auditLogs -> {
        AuditLogEntry entry = auditLogs.get(0);
        Map<String, AuditLogChange> changes = entry.getChanges();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        if (changes.containsKey("region")) {
            embedBuilder.addField("Region change", "`" + changes.get("region").getOldValue() + "` -> `" + changes.get("region").getNewValue() + "`", true);
        }
        if (changes.containsKey("name")) {
            embedBuilder.addField("Name", "`" + changes.get("name").getOldValue() + "` -> `" + changes.get("name").getNewValue() + "`", true);
        }
        if (changes.containsKey("afk_channel_id")) {
            AuditLogChange change = changes.get("afk_channel_id");
            String oldChannel;
            String newChannel;
            if (change.getOldValue() == null) {
                oldChannel = "none";
            } else {
                oldChannel = event.getGuild().getVoiceChannelById(change.getOldValue()).getName();
            }
            if (change.getNewValue() == null) {
                newChannel = "none";
            } else {
                newChannel = event.getGuild().getVoiceChannelById(change.getNewValue()).getName();
            }
            embedBuilder.addField("AFK channel", "`" + oldChannel + "` -> `" + newChannel + "`", true);
        }
        if (changes.containsKey("afk_timeout")) {
            embedBuilder.addField("AFK timeout (minutes)", "`" + ((int) changes.get("afk_timeout").getOldValue() / 60) + "` -> `" + ((int) changes.get("afk_timeout").getNewValue() / 60) + "`", true);
        }
        if (changes.containsKey("system_channel_id")) {
            AuditLogChange change = changes.get("system_channel_id");
            String oldChannel;
            String newChannel;
            if (change.getOldValue() == null) {
                oldChannel = "none";
            } else {
                oldChannel = event.getGuild().getTextChannelById(change.getOldValue()).getName();
            }
            if (change.getNewValue() == null) {
                newChannel = "none";
            } else {
                newChannel = event.getGuild().getTextChannelById(change.getNewValue()).getName();
            }
            embedBuilder.addField("Welcome channel", "`" + oldChannel + "` -> `" + newChannel + "`", true);
        }
        if (changes.containsKey("default_message_notifications")) {
            String oldValue;
            String newValue;
            if ((int) changes.get("default_message_notifications").getOldValue() == 0) {
                oldValue = "All messages";
                newValue = "Only mentions";
            } else {
                oldValue = "Only mentions";
                newValue = "All messages";
            }
            embedBuilder.addField("Notification", "`" + oldValue + "` -> `" + newValue + "`", true);
        }
        if (changes.containsKey("verification_level")) {
            embedBuilder.addField("Verification level", "`" + GeneralUtils.getVerificationString(Guild.VerificationLevel.fromKey(changes.get("verification_level").getOldValue())) + " ` -> `" + GeneralUtils.getVerificationString(Guild.VerificationLevel.fromKey(changes.get("verification_level").getNewValue())) + "`", true);
        }
        if (changes.containsKey("mfa_level")) {
            boolean tfa = false;
            if ((int) changes.get("mfa_level").getOldValue() == 0) {
                tfa = true;
            }
            embedBuilder.addField("Two Factor authorization required", tfa ? "Yes" : "No", true);
        }
        ModlogHandler.getInstance().postToModlog(wrapper, ModlogEvent.GUILD_UPDATE, entry.getUser(), embedBuilder);
    });
}
Also used : GuildUpdateExplicitContentLevelEvent(net.dv8tion.jda.core.events.guild.update.GuildUpdateExplicitContentLevelEvent) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) AuditLogEntry(net.dv8tion.jda.core.audit.AuditLogEntry) AuditLogChange(net.dv8tion.jda.core.audit.AuditLogChange)

Example 3 with AuditLogChange

use of net.dv8tion.jda.core.audit.AuditLogChange in project FlareBot by FlareBot.

the class ModlogEvents method onGuildUpdateExplicitContentLevel.

private void onGuildUpdateExplicitContentLevel(GuildUpdateExplicitContentLevelEvent e, @Nonnull GuildWrapper wrapper) {
    if (cannotHandle(wrapper, ModlogEvent.GUILD_EXPLICIT_FILTER_CHANGE))
        return;
    AuditLogEntry entry = e.getGuild().getAuditLogs().complete().get(0);
    AuditLogChange levelChange = entry.getChanges().get("explicit_content_filter");
    ModlogHandler.getInstance().postToModlog(wrapper, ModlogEvent.GUILD_EXPLICIT_FILTER_CHANGE, entry.getUser(), new MessageEmbed.Field("Old level", Guild.ExplicitContentLevel.fromKey(levelChange.getOldValue()).getDescription(), true), new MessageEmbed.Field("New level", Guild.ExplicitContentLevel.fromKey(levelChange.getNewValue()).getDescription(), true));
}
Also used : MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) AuditLogEntry(net.dv8tion.jda.core.audit.AuditLogEntry) AuditLogChange(net.dv8tion.jda.core.audit.AuditLogChange)

Example 4 with AuditLogChange

use of net.dv8tion.jda.core.audit.AuditLogChange in project FlareBot by FlareBot.

the class ModlogEvents method onGuildMemberRoleAdd.

private void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event, @Nonnull GuildWrapper wrapper) {
    if (cannotHandle(wrapper, ModlogEvent.MEMBER_ROLE_GIVE))
        return;
    event.getGuild().getAuditLogs().queue(auditLogEntries -> {
        if (auditLogEntries.isEmpty())
            return;
        AuditLogEntry entry = auditLogEntries.get(0);
        Map<String, AuditLogChange> changes = entry.getChanges();
        AuditLogChange change = changes.get("$add");
        @SuppressWarnings("unchecked") HashMap<String, String> role = ((ArrayList<HashMap<String, String>>) change.getNewValue()).get(0);
        if (wrapper.getAutoAssignRoles().contains(role.get("id")) && ((System.currentTimeMillis() / 1000) - event.getMember().getJoinDate().toEpochSecond()) < 10) {
            return;
        }
        ModlogHandler.getInstance().postToModlog(wrapper, ModlogEvent.MEMBER_ROLE_GIVE, event.getUser(), entry.getUser(), null, new MessageEmbed.Field("Role", role.get("name") + " (" + role.get("id") + ")", true));
    });
}
Also used : MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) AuditLogEntry(net.dv8tion.jda.core.audit.AuditLogEntry) AuditLogChange(net.dv8tion.jda.core.audit.AuditLogChange) ArrayList(java.util.ArrayList)

Example 5 with AuditLogChange

use of net.dv8tion.jda.core.audit.AuditLogChange in project FlareBot by FlareBot.

the class ModlogEvents method onGuildMemberRoleRemove.

private void onGuildMemberRoleRemove(GuildMemberRoleRemoveEvent event, @Nonnull GuildWrapper wrapper) {
    if (cannotHandle(wrapper, ModlogEvent.MEMBER_ROLE_REMOVE))
        return;
    event.getGuild().getAuditLogs().queue(auditLogEntries -> {
        if (auditLogEntries.isEmpty())
            return;
        AuditLogEntry entry = auditLogEntries.get(0);
        Map<String, AuditLogChange> changes = entry.getChanges();
        AuditLogChange change = changes.get("$remove");
        @SuppressWarnings("unchecked") HashMap<String, String> role = ((ArrayList<HashMap<String, String>>) change.getNewValue()).get(0);
        ModlogHandler.getInstance().postToModlog(wrapper, ModlogEvent.MEMBER_ROLE_REMOVE, event.getUser(), entry.getUser(), null, new MessageEmbed.Field("Role", role.get("name") + " (" + role.get("id") + ")", true));
    });
}
Also used : MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) AuditLogEntry(net.dv8tion.jda.core.audit.AuditLogEntry) AuditLogChange(net.dv8tion.jda.core.audit.AuditLogChange) ArrayList(java.util.ArrayList)

Aggregations

AuditLogChange (net.dv8tion.jda.core.audit.AuditLogChange)5 AuditLogEntry (net.dv8tion.jda.core.audit.AuditLogEntry)5 ArrayList (java.util.ArrayList)3 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)3 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 List (java.util.List)1 Permission (net.dv8tion.jda.core.Permission)1 GuildUpdateExplicitContentLevelEvent (net.dv8tion.jda.core.events.guild.update.GuildUpdateExplicitContentLevelEvent)1 RoleUpdatePositionEvent (net.dv8tion.jda.core.events.role.update.RoleUpdatePositionEvent)1