Search in sources :

Example 1 with RoleUpdatePositionEvent

use of net.dv8tion.jda.core.events.role.update.RoleUpdatePositionEvent 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)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 Permission (net.dv8tion.jda.core.Permission)1 AuditLogChange (net.dv8tion.jda.core.audit.AuditLogChange)1 AuditLogEntry (net.dv8tion.jda.core.audit.AuditLogEntry)1 RoleUpdatePositionEvent (net.dv8tion.jda.core.events.role.update.RoleUpdatePositionEvent)1