From 3819254393ae7c0addf07faac8ccda7a64bfc900 Mon Sep 17 00:00:00 2001 From: Almighty-Satan <43804777+Almighty-Satan@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:06:30 +0200 Subject: [PATCH 1/2] Add additional methods to get the target of an audit-log entry --- .../dv8tion/jda/api/audit/AuditLogEntry.java | 69 +++++++++++++++++-- .../jda/internal/entities/EntityBuilder.java | 37 +++++++++- .../GuildAuditLogEntryCreateHandler.java | 2 +- .../AuditLogPaginationActionImpl.java | 31 ++++----- 4 files changed, 112 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java b/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java index ddaa4aeb80..457f513036 100644 --- a/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java +++ b/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java @@ -19,12 +19,16 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.ISnowflake; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.ScheduledEvent; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.Webhook; +import net.dv8tion.jda.api.entities.automod.AutoModRule; +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent; import net.dv8tion.jda.internal.entities.GuildImpl; import net.dv8tion.jda.internal.entities.UserImpl; -import net.dv8tion.jda.internal.entities.WebhookImpl; import net.dv8tion.jda.internal.utils.Checks; import net.dv8tion.jda.internal.utils.EntityString; @@ -47,7 +51,7 @@ public class AuditLogEntry implements ISnowflake protected final long userId; protected final GuildImpl guild; protected final UserImpl user; - protected final WebhookImpl webhook; + protected final ISnowflake target; protected final String reason; protected final Map changes; @@ -55,7 +59,7 @@ public class AuditLogEntry implements ISnowflake protected final ActionType type; protected final int rawType; - public AuditLogEntry(ActionType type, int rawType, long id, long userId, long targetId, GuildImpl guild, UserImpl user, WebhookImpl webhook, + public AuditLogEntry(ActionType type, int rawType, long id, long userId, long targetId, GuildImpl guild, UserImpl user, ISnowflake target, String reason, Map changes, Map options) { this.type = type; @@ -65,7 +69,7 @@ public AuditLogEntry(ActionType type, int rawType, long id, long userId, long ta this.targetId = targetId; this.guild = guild; this.user = user; - this.webhook = webhook; + this.target = target; this.reason = reason; this.changes = changes != null && !changes.isEmpty() ? Collections.unmodifiableMap(changes) @@ -105,6 +109,61 @@ public String getTargetId() { return Long.toUnsignedString(targetId); } + + /** + * The {@link AutoModRule} that the target id of this audit-log entry refers to + * + * @return Possibly-null AutoModRule instance + */ + @Nullable + public AutoModRule getAutoModRule() + { + return type.getTargetType() != TargetType.AUTO_MODERATION_RULE ? null : (AutoModRule) target; + } + + /** + * The {@link GuildChannel} that the target id of this audit-log entry refers to + * + * @return Possibly-null GuildChannel instance + */ + @Nullable + public GuildChannel getGuildChannel() + { + return type.getTargetType() != TargetType.CHANNEL ? null : (GuildChannel) target; + } + + /** + * The {@link Member} that the target id of this audit-log entry refers to + * + * @return Possibly-null Member instance + */ + @Nullable + public Member getMember() + { + return type.getTargetType() != TargetType.MEMBER ? null : (Member) target; + } + + /** + * The {@link ScheduledEvent} that the target id of this audit-log entry refers to + * + * @return Possibly-null ScheduledEvent instance + */ + @Nullable + public ScheduledEvent getScheduledEvent() + { + return type.getTargetType() != TargetType.SCHEDULED_EVENT ? null : (ScheduledEvent) target; + } + + /** + * The {@link ThreadChannel} that the target id of this audit-log entry refers to + * + * @return Possibly-null ThreadChannel instance + */ + @Nullable + public ThreadChannel getThread() + { + return type.getTargetType() != TargetType.THREAD ? null : (ThreadChannel) target; + } /** * The {@link net.dv8tion.jda.api.entities.Webhook Webhook} that the target id of this audit-log entry refers to @@ -114,7 +173,7 @@ public String getTargetId() @Nullable public Webhook getWebhook() { - return webhook; + return type.getTargetType() != TargetType.WEBHOOK ? null : (Webhook) target; } /** diff --git a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java index 09b06f2ad8..6e0b6c419c 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java @@ -57,6 +57,7 @@ import net.dv8tion.jda.api.utils.data.DataArray; import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.JDAImpl; +import net.dv8tion.jda.internal.entities.automod.AutoModRuleImpl; import net.dv8tion.jda.internal.entities.channel.concrete.*; import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IPermissionContainerMixin; import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IPostContainerMixin; @@ -2513,7 +2514,8 @@ public ApplicationTeam createApplicationTeam(DataObject object) return new ApplicationTeamImpl(iconId, members, id, ownerId); } - public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson, DataObject userJson, DataObject webhookJson) + public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson, TLongObjectMap userMap, TLongObjectMap webhookMap, + TLongObjectMap threadMap, TLongObjectMap scheduledEventMap, TLongObjectMap autoModerationRulesMap) { final long targetId = entryJson.getLong("target_id", 0); final long userId = entryJson.getLong("user_id", 0); @@ -2523,11 +2525,40 @@ public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson, final DataObject options = entryJson.isNull("options") ? null : entryJson.getObject("options"); final String reason = entryJson.getString("reason", null); + final DataObject userJson = userMap == null ? null : userMap.get(userId); final UserImpl user = userJson == null ? null : createUser(userJson); - final WebhookImpl webhook = webhookJson == null ? null : createWebhook(webhookJson); final Set changesList; final ActionType type = ActionType.from(typeKey); + ISnowflake target; + DataObject targetJson; + switch (type.getTargetType()) { + case AUTO_MODERATION_RULE: + targetJson = autoModerationRulesMap == null ? null : autoModerationRulesMap.get(targetId); + target = targetJson == null ? null : AutoModRuleImpl.fromData(guild, targetJson); + break; + case CHANNEL: + target = guild.getGuildChannelById(targetId); + break; + case MEMBER: + target = guild.getMemberById(targetId); + break; + case SCHEDULED_EVENT: + targetJson = scheduledEventMap == null ? null : scheduledEventMap.get(targetId); + target = targetJson == null ? null : createScheduledEvent(guild, targetJson); + break; + case THREAD: + targetJson = threadMap == null ? null : threadMap.get(targetId); + target = targetJson == null ? null : createThreadChannel(guild, targetJson, guild.getIdLong()); + break; + case WEBHOOK: + targetJson = webhookMap == null ? null : webhookMap.get(targetId); + target = targetJson == null ? null : createWebhook(targetJson); + break; + default: + target = null; + } + if (changes != null) { changesList = new HashSet<>(changes.length()); @@ -2547,7 +2578,7 @@ public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson, CaseInsensitiveMap optionMap = options != null ? new CaseInsensitiveMap<>(options.toMap()) : null; - return new AuditLogEntry(type, typeKey, id, userId, targetId, guild, user, webhook, reason, changeMap, optionMap); + return new AuditLogEntry(type, typeKey, id, userId, targetId, guild, user, target, reason, changeMap, optionMap); } public AuditLogChange createAuditLogChange(DataObject change) diff --git a/src/main/java/net/dv8tion/jda/internal/handle/GuildAuditLogEntryCreateHandler.java b/src/main/java/net/dv8tion/jda/internal/handle/GuildAuditLogEntryCreateHandler.java index 035721ddb0..d32209bbcd 100644 --- a/src/main/java/net/dv8tion/jda/internal/handle/GuildAuditLogEntryCreateHandler.java +++ b/src/main/java/net/dv8tion/jda/internal/handle/GuildAuditLogEntryCreateHandler.java @@ -44,7 +44,7 @@ protected Long handleInternally(DataObject content) return null; } - AuditLogEntry entry = api.getEntityBuilder().createAuditLogEntry(guild, content, null, null); + AuditLogEntry entry = api.getEntityBuilder().createAuditLogEntry(guild, content, null, null, null, null, null); api.handleEvent( new GuildAuditLogEntryCreateEvent( diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/pagination/AuditLogPaginationActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/pagination/AuditLogPaginationActionImpl.java index a48fa65d3d..a1e4993d96 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/pagination/AuditLogPaginationActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/pagination/AuditLogPaginationActionImpl.java @@ -17,7 +17,6 @@ package net.dv8tion.jda.internal.requests.restaction.pagination; import gnu.trove.map.TLongObjectMap; -import gnu.trove.map.hash.TLongObjectHashMap; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.audit.ActionType; import net.dv8tion.jda.api.audit.AuditLogEntry; @@ -33,11 +32,13 @@ import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.entities.EntityBuilder; import net.dv8tion.jda.internal.entities.GuildImpl; +import net.dv8tion.jda.internal.utils.Helpers; import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; +import java.util.function.ToLongFunction; public class AuditLogPaginationActionImpl extends PaginationActionImpl @@ -105,35 +106,29 @@ protected Route.CompiledRoute finalizeRoute() protected void handleSuccess(Response response, Request> request) { DataObject obj = response.getObject(); + DataArray entries = obj.getArray("audit_log_entries"); DataArray users = obj.getArray("users"); DataArray webhooks = obj.getArray("webhooks"); - DataArray entries = obj.getArray("audit_log_entries"); + DataArray threads = obj.getArray("threads"); + DataArray events = obj.getArray("guild_scheduled_events"); + DataArray automodRules = obj.getArray("auto_moderation_rules"); List list = new ArrayList<>(entries.length()); EntityBuilder builder = api.getEntityBuilder(); - TLongObjectMap userMap = new TLongObjectHashMap<>(); - for (int i = 0; i < users.length(); i++) - { - DataObject user = users.getObject(i); - userMap.put(user.getLong("id"), user); - } - - TLongObjectMap webhookMap = new TLongObjectHashMap<>(); - for (int i = 0; i < webhooks.length(); i++) - { - DataObject webhook = webhooks.getObject(i); - webhookMap.put(webhook.getLong("id"), webhook); - } + ToLongFunction getIdFunction = dataObject -> dataObject.getLong("id"); + TLongObjectMap userMap = Helpers.convertToMap(getIdFunction, users); + TLongObjectMap webhookMap = Helpers.convertToMap(getIdFunction, webhooks); + TLongObjectMap threadMap = Helpers.convertToMap(getIdFunction, threads); + TLongObjectMap eventMap = Helpers.convertToMap(getIdFunction, events); + TLongObjectMap automodRuleMap = Helpers.convertToMap(getIdFunction, automodRules); for (int i = 0; i < entries.length(); i++) { try { DataObject entry = entries.getObject(i); - DataObject user = userMap.get(entry.getLong("user_id", 0)); - DataObject webhook = webhookMap.get(entry.getLong("target_id", 0)); - AuditLogEntry result = builder.createAuditLogEntry((GuildImpl) guild, entry, user, webhook); + AuditLogEntry result = builder.createAuditLogEntry((GuildImpl) guild, entry, userMap, webhookMap, threadMap, eventMap, automodRuleMap); list.add(result); } catch (ParsingException | NullPointerException e) From 0a8b66ef2d123c6f5cf3a275859d614e0e7de098 Mon Sep 17 00:00:00 2001 From: Almighty-Satan <43804777+Almighty-Satan@users.noreply.github.com> Date: Thu, 26 Oct 2023 10:25:07 +0200 Subject: [PATCH 2/2] Fix Javadoc --- .../net/dv8tion/jda/api/audit/AuditLogEntry.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java b/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java index 457f513036..db6c7e7a78 100644 --- a/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java +++ b/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java @@ -111,7 +111,7 @@ public String getTargetId() } /** - * The {@link AutoModRule} that the target id of this audit-log entry refers to + * The {@link AutoModRule} that the target id of this audit-log entry refers to. * * @return Possibly-null AutoModRule instance */ @@ -122,7 +122,7 @@ public AutoModRule getAutoModRule() } /** - * The {@link GuildChannel} that the target id of this audit-log entry refers to + * The {@link GuildChannel} that the target id of this audit-log entry refers to. * * @return Possibly-null GuildChannel instance */ @@ -133,7 +133,7 @@ public GuildChannel getGuildChannel() } /** - * The {@link Member} that the target id of this audit-log entry refers to + * The {@link Member} that the target id of this audit-log entry refers to. * * @return Possibly-null Member instance */ @@ -144,7 +144,7 @@ public Member getMember() } /** - * The {@link ScheduledEvent} that the target id of this audit-log entry refers to + * The {@link ScheduledEvent} that the target id of this audit-log entry refers to. * * @return Possibly-null ScheduledEvent instance */ @@ -155,7 +155,7 @@ public ScheduledEvent getScheduledEvent() } /** - * The {@link ThreadChannel} that the target id of this audit-log entry refers to + * The {@link ThreadChannel} that the target id of this audit-log entry refers to. * * @return Possibly-null ThreadChannel instance */ @@ -166,7 +166,7 @@ public ThreadChannel getThread() } /** - * The {@link net.dv8tion.jda.api.entities.Webhook Webhook} that the target id of this audit-log entry refers to + * The {@link net.dv8tion.jda.api.entities.Webhook Webhook} that the target id of this audit-log entry refers to. * * @return Possibly-null Webhook instance */