Search in sources :

Example 1 with ActionType

use of sx.blah.discord.handle.audit.ActionType in project Discord4J by Discord4J.

the class DiscordUtils method getAuditLogEntryFromJSON.

/**
 * Converts a json {@link AuditLogEntry} to a {@link AuditLogEntry}.
 *
 * @param guild The guild the entry belongs to.
 * @param users The users of the parent audit log.
 * @param webhooks The webhooks of the parent audit log.
 * @param json The converted audit log entry object.
 * @return The converted audit log entry.
 */
public static AuditLogEntry getAuditLogEntryFromJSON(IGuild guild, LongMap<IUser> users, LongMap<IWebhook> webhooks, AuditLogEntryObject json) {
    long targetID = json.target_id == null ? 0 : Long.parseUnsignedLong(json.target_id);
    long id = Long.parseUnsignedLong(json.id);
    IUser user = users.get(Long.parseUnsignedLong(json.user_id));
    ChangeMap changes = json.changes == null ? new ChangeMap() : Arrays.stream(json.changes).collect(ChangeMap.Collector.toChangeMap());
    OptionMap options = new OptionMap(json.options);
    ActionType actionType = ActionType.fromRaw(json.action_type);
    switch(actionType) {
        case GUILD_UPDATE:
            return new DiscordObjectEntry<>(guild, id, user, changes, json.reason, actionType, options);
        case CHANNEL_CREATE:
        case CHANNEL_UPDATE:
        case CHANNEL_OVERWRITE_CREATE:
        case CHANNEL_OVERWRITE_UPDATE:
        case CHANNEL_OVERWRITE_DELETE:
            IChannel channel = guild.getChannelByID(targetID);
            if (channel == null)
                channel = guild.getVoiceChannelByID(targetID);
            if (channel == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(channel, id, user, changes, json.reason, actionType, options);
        case MEMBER_KICK:
        case MEMBER_BAN_ADD:
        case MEMBER_BAN_REMOVE:
        case MEMBER_UPDATE:
        case MEMBER_ROLE_UPDATE:
        case // message delete target is the author of the message
        MESSAGE_DELETE:
            IUser target = users.get(targetID);
            if (target == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(target, id, user, changes, json.reason, actionType, options);
        case ROLE_CREATE:
        case ROLE_UPDATE:
            IRole role = guild.getRoleByID(targetID);
            if (role == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(role, id, user, changes, json.reason, actionType, options);
        case WEBHOOK_CREATE:
        case WEBHOOK_UPDATE:
            IWebhook webhook = webhooks.get(targetID);
            if (webhook == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(webhook, id, user, changes, json.reason, actionType, options);
        case EMOJI_CREATE:
        case EMOJI_UPDATE:
            IEmoji emoji = guild.getEmojiByID(targetID);
            if (emoji == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(emoji, id, user, changes, json.reason, actionType, options);
        case CHANNEL_DELETE:
        case ROLE_DELETE:
        case WEBHOOK_DELETE:
        case EMOJI_DELETE:
            return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
        case INVITE_CREATE:
        case INVITE_DELETE:
        case INVITE_UPDATE:
        case MEMBER_PRUNE:
            return new AuditLogEntry(id, user, changes, json.reason, actionType, options);
    }
    return null;
}
Also used : ChangeMap(sx.blah.discord.handle.audit.entry.change.ChangeMap) ActionType(sx.blah.discord.handle.audit.ActionType) AuditLogEntry(sx.blah.discord.handle.audit.entry.AuditLogEntry) TargetedEntry(sx.blah.discord.handle.audit.entry.TargetedEntry) OptionMap(sx.blah.discord.handle.audit.entry.option.OptionMap) DiscordObjectEntry(sx.blah.discord.handle.audit.entry.DiscordObjectEntry)

Aggregations

ActionType (sx.blah.discord.handle.audit.ActionType)1 AuditLogEntry (sx.blah.discord.handle.audit.entry.AuditLogEntry)1 DiscordObjectEntry (sx.blah.discord.handle.audit.entry.DiscordObjectEntry)1 TargetedEntry (sx.blah.discord.handle.audit.entry.TargetedEntry)1 ChangeMap (sx.blah.discord.handle.audit.entry.change.ChangeMap)1 OptionMap (sx.blah.discord.handle.audit.entry.option.OptionMap)1