net.dv8tion.jda.api.audit.AuditLogEntry Java Examples

The following examples show how to use net.dv8tion.jda.api.audit.AuditLogEntry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PunishmentListener.java    From Arraybot with Apache License 2.0 6 votes vote down vote up
/**
 * Whether or not the latest audit log shows that it is a punishment.
 * @param entry The audit log entry.
 * @param user The user.
 * @param type The punishment type.
 * @param roles The roles involved in the event, should be null unless it is a mute.
 * @return True if it is, false otherwise.
 */
private boolean isPunishment(AuditLogEntry entry, long user, PunishmentType type, Role[] roles) {
    if(entry.getTargetIdLong() != user) {
        return false;
    }
    if(entry.getType() != type.getAuditType()
            && entry.getType() != type.getUndoAuditType()) {
        return false;
    }
    if(type == PunishmentType.MUTE) {
        String mutedRole = UPunishment.getMutedRole(entry.getGuild());
        for(Role role : roles) {
            if(role.getId().equals(mutedRole)) {
                return true;
            }
        }
        return false;
    }
    return true;
}
 
Example #2
Source File: PunishmentListener.java    From Arraybot with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the punishment or punishment revocation.
 * @param event The event.
 * @param userObject The user object.
 * @param type The type of punishment.
 * @param roles The roles involved in the event, should be null unless it is a mute or unmute.
 * @param revoke True: revocation, false: punishment creation.
 */
private void handlePunishment(GenericGuildEvent event, User userObject, PunishmentType type, Role[] roles, boolean revoke) {
    Guild guild = event.getGuild();
    if(!event.getGuild().getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
        return;
    }
    long user = userObject.getIdLong();
    guild.retrieveAuditLogs().queue(entries -> {
        if(entries.isEmpty()) {
            return;
        }
        AuditLogEntry entry = entries.get(0);
        if(!isPunishment(entry, user, type, roles)
                || entry.getUser() == null
                || entry.getUser().getIdLong() == guild.getSelfMember().getUser().getIdLong()) {
            return;
        }
        long staff = entry.getUser().getIdLong();
        if(!revoke) {
            punishmentManager.punish(guild, user, type, staff, -1, true, entry.getReason());
        } else {
            PunishmentObject punishmentObject = ULambda.INSTANCE.getSpecificGeneralizedPunishment(guild, user, type);
            if(punishmentObject == null) {
                return;
            }
            if(!punishmentManager.revoke(guild, punishmentObject, staff)) {
                Arraybot.INSTANCE.getLogger().error("Failed the revocation of the punishment number {} in the guild {}.", punishmentObject.getId(), guild.getIdLong());
            }
        }
    });
}
 
Example #3
Source File: EntityBuilder.java    From JDA with Apache License 2.0 5 votes vote down vote up
public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson, DataObject userJson, DataObject webhookJson)
{
    final long targetId = entryJson.getLong("target_id", 0);
    final long id = entryJson.getLong("id");
    final int typeKey = entryJson.getInt("action_type");
    final DataArray changes = entryJson.isNull("changes") ? null : entryJson.getArray("changes");
    final DataObject options = entryJson.isNull("options") ? null : entryJson.getObject("options");
    final String reason = entryJson.getString("reason", null);

    final UserImpl user = userJson == null ? null : createFakeUser(userJson);
    final WebhookImpl webhook = webhookJson == null ? null : createWebhook(webhookJson);
    final Set<AuditLogChange> changesList;
    final ActionType type = ActionType.from(typeKey);

    if (changes != null)
    {
        changesList = new HashSet<>(changes.length());
        for (int i = 0; i < changes.length(); i++)
        {
            final DataObject object = changes.getObject(i);
            AuditLogChange change = createAuditLogChange(object);
            changesList.add(change);
        }
    }
    else
    {
        changesList = Collections.emptySet();
    }

    CaseInsensitiveMap<String, AuditLogChange> changeMap = new CaseInsensitiveMap<>(changeToMap(changesList));
    CaseInsensitiveMap<String, Object> optionMap = options != null
            ? new CaseInsensitiveMap<>(options.toMap()) : null;

    return new AuditLogEntry(type, typeKey, id, targetId, guild, user, webhook, reason, changeMap, optionMap);
}
 
Example #4
Source File: GuildListener.java    From SkyBot with GNU Affero General Public License v3.0 4 votes vote down vote up
private void modLogBanUnban(ActionType type, User user, Guild guild) {
    if (!guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
        return;
    }

    final DunctebotGuild dbg = new DunctebotGuild(guild, variables);
    final GuildSettings settings = dbg.getSettings();

    if (settings.getLogChannel() < 1) {
        return;
    }

    // If unban and unban logging is disabled
    if (type == ActionType.UNBAN && !settings.isUnbanLogging()) {
        return;
    }

    // If ban and ban logging is disabled
    if (type == ActionType.BAN && !settings.isBanLogging()) {
        return;
    }

    guild.retrieveAuditLogs()
        .cache(false)
        .type(type)
        .limit(5)
        .queue((actions) -> {
            for (final AuditLogEntry action : actions) {
                if (action.getUser() != null && action.getUser().getIdLong() == guild.getSelfMember().getIdLong()) {
                    continue;
                }

                if (action.getTargetIdLong() == user.getIdLong()) {
                    ModerationUtils.modLog(
                        action.getUser(),
                        user,
                        type == ActionType.BAN ? "banned" : "unbanned",
                        action.getReason(),
                        dbg
                    );

                    break;
                }
            }
        });
}
 
Example #5
Source File: AuditLogPaginationActionImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Override
protected long getKey(AuditLogEntry it)
{
    return it.getIdLong();
}