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

The following examples show how to use net.dv8tion.jda.api.audit.ActionType. 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: 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 #2
Source File: GuildListener.java    From SkyBot with GNU Affero General Public License v3.0 4 votes vote down vote up
private void onGuildUnban(GuildUnbanEvent event) {
    modLogBanUnban(ActionType.UNBAN, event.getUser(), event.getGuild());
}
 
Example #3
Source File: GuildListener.java    From SkyBot with GNU Affero General Public License v3.0 4 votes vote down vote up
private void onGuildBan(GuildBanEvent event) {
    modLogBanUnban(ActionType.BAN, event.getUser(), event.getGuild());
}
 
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;
                }
            }
        });
}