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 d7dd917f81..ddaa4aeb80 100644 --- a/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java +++ b/src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java @@ -21,6 +21,7 @@ import net.dv8tion.jda.api.entities.ISnowflake; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.Webhook; +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; @@ -43,6 +44,7 @@ public class AuditLogEntry implements ISnowflake { protected final long id; protected final long targetId; + protected final long userId; protected final GuildImpl guild; protected final UserImpl user; protected final WebhookImpl webhook; @@ -53,12 +55,13 @@ public class AuditLogEntry implements ISnowflake protected final ActionType type; protected final int rawType; - public AuditLogEntry(ActionType type, int rawType, long id, long targetId, GuildImpl guild, UserImpl user, WebhookImpl webhook, + public AuditLogEntry(ActionType type, int rawType, long id, long userId, long targetId, GuildImpl guild, UserImpl user, WebhookImpl webhook, String reason, Map changes, Map options) { - this.rawType = rawType; this.type = type; + this.rawType = rawType; this.id = id; + this.userId = userId; this.targetId = targetId; this.guild = guild; this.user = user; @@ -126,8 +129,30 @@ public Guild getGuild() } /** - * The {@link net.dv8tion.jda.api.entities.User User} responsible - * for this action. + * The id for the user that executed the action. + * + * @return The user id + */ + public long getUserIdLong() + { + return userId; + } + + /** + * The id for the user that executed the action. + * + * @return The user id + */ + @Nonnull + public String getUserId() + { + return Long.toUnsignedString(userId); + } + + /** + * The {@link User} responsible for this action. + * + *

This will not be available for {@link GuildAuditLogEntryCreateEvent}, you can use {@link #getUserIdLong()} instead. * * @return Possibly-null User instance */ diff --git a/src/main/java/net/dv8tion/jda/api/events/guild/GuildAuditLogEntryCreateEvent.java b/src/main/java/net/dv8tion/jda/api/events/guild/GuildAuditLogEntryCreateEvent.java new file mode 100644 index 0000000000..6ba66bc9db --- /dev/null +++ b/src/main/java/net/dv8tion/jda/api/events/guild/GuildAuditLogEntryCreateEvent.java @@ -0,0 +1,55 @@ +/* + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dv8tion.jda.api.events.guild; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.audit.AuditLogEntry; +import net.dv8tion.jda.api.entities.Guild; + +import javax.annotation.Nonnull; + +/** + * Indicates that an {@link AuditLogEntry} was added to a {@link Guild}. + * + *

This never provides a {@link AuditLogEntry#getUser() responsible user} instance. + * You can use {@link AuditLogEntry#getUserIdLong()} instead. + * + *

Requirements
+ * + *

This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_MODERATION GUILD_MODERATION} intent to be enabled. + */ +public class GuildAuditLogEntryCreateEvent extends GenericGuildEvent +{ + private final AuditLogEntry entry; + + public GuildAuditLogEntryCreateEvent(@Nonnull JDA api, long responseNumber, @Nonnull AuditLogEntry entry) + { + super(api, responseNumber, entry.getGuild()); + this.entry = entry; + } + + /** + * The {@link AuditLogEntry} that was added to the {@link Guild} + * + * @return The added entry + */ + @Nonnull + public AuditLogEntry getEntry() + { + return entry; + } +} diff --git a/src/main/java/net/dv8tion/jda/api/events/guild/GuildBanEvent.java b/src/main/java/net/dv8tion/jda/api/events/guild/GuildBanEvent.java index 5bb8875d49..1912a0fa0f 100644 --- a/src/main/java/net/dv8tion/jda/api/events/guild/GuildBanEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/guild/GuildBanEvent.java @@ -31,7 +31,7 @@ * *

Requirements
* - *

This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_BANS GUILD_BANS} intent to be enabled. + *

This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_MODERATION GUILD_MODERATION} intent to be enabled. */ public class GuildBanEvent extends GenericGuildEvent { diff --git a/src/main/java/net/dv8tion/jda/api/events/guild/GuildUnbanEvent.java b/src/main/java/net/dv8tion/jda/api/events/guild/GuildUnbanEvent.java index 2043bd4e56..18a151d313 100644 --- a/src/main/java/net/dv8tion/jda/api/events/guild/GuildUnbanEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/guild/GuildUnbanEvent.java @@ -28,7 +28,7 @@ * *

Requirements
* - *

This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_BANS GUILD_BANS} intent to be enabled. + *

This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_MODERATION GUILD_MODERATION} intent to be enabled. */ public class GuildUnbanEvent extends GenericGuildEvent { diff --git a/src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java b/src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java index 86519063b5..6cc4b0bea0 100644 --- a/src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java +++ b/src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java @@ -242,6 +242,7 @@ public void onUnavailableGuildJoined(@Nonnull UnavailableGuildJoinedEvent event) public void onUnavailableGuildLeave(@Nonnull UnavailableGuildLeaveEvent event) {} public void onGuildBan(@Nonnull GuildBanEvent event) {} public void onGuildUnban(@Nonnull GuildUnbanEvent event) {} + public void onGuildAuditLogEntryCreate(@Nonnull GuildAuditLogEntryCreateEvent event) {} public void onGuildMemberRemove(@Nonnull GuildMemberRemoveEvent event) {} //Guild Update Events diff --git a/src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java b/src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java index 7d2084a3cc..51db8d7722 100644 --- a/src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java +++ b/src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java @@ -16,10 +16,14 @@ package net.dv8tion.jda.api.requests; +import net.dv8tion.jda.annotations.DeprecatedSince; +import net.dv8tion.jda.annotations.ForRemoval; +import net.dv8tion.jda.annotations.ReplaceWith; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.GenericEvent; import net.dv8tion.jda.api.events.emoji.GenericEmojiEvent; +import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent; import net.dv8tion.jda.api.events.guild.GuildBanEvent; import net.dv8tion.jda.api.events.guild.GuildUnbanEvent; import net.dv8tion.jda.api.events.guild.invite.GenericGuildInviteEvent; @@ -51,7 +55,7 @@ * *

    *
  1. GUILD_MEMBERS - This is a privileged gateway intent that is used to update user information and join/leaves (including kicks). This is required to cache all members of a guild (including chunking)
  2. - *
  3. GUILD_BANS - This will only track guild bans and unbans
  4. + *
  5. GUILD_MODERATION - This will only track guild moderation events, such as bans, unbans, and audit-logs.
  6. *
  7. GUILD_EMOJIS - This will only track custom emoji create/modify/delete. Most bots don't need this since they just use the emoji id anyway.
  8. *
  9. GUILD_WEBHOOKS - This will only track guild webhook create/update/delete. Most bots don't need this since related events don't contain any useful information about webhook changes.
  10. *
  11. GUILD_INVITES - This will only track invite create/delete. Most bots don't make use of invites since they are added through OAuth2 authorization by administrators.
  12. @@ -90,7 +94,15 @@ public enum GatewayIntent /** * Ban events. */ + @Deprecated + @ForRemoval + @DeprecatedSince("5.0.0-beta.4") + @ReplaceWith("GUILD_MODERATION") GUILD_BANS(2), + /** + * Moderation events, such as ban/unban/audit-log. + */ + GUILD_MODERATION(2), /** * Custom emoji and sticker add/update/delete events. */ @@ -377,8 +389,8 @@ public static EnumSet fromEvents(@Nonnull Collection optionMap = options != null ? new CaseInsensitiveMap<>(options.toMap()) : null; - return new AuditLogEntry(type, typeKey, id, targetId, guild, user, webhook, reason, changeMap, optionMap); + return new AuditLogEntry(type, typeKey, id, userId, targetId, guild, user, webhook, 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 new file mode 100644 index 0000000000..035721ddb0 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/internal/handle/GuildAuditLogEntryCreateHandler.java @@ -0,0 +1,56 @@ +/* + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dv8tion.jda.internal.handle; + +import net.dv8tion.jda.api.audit.AuditLogEntry; +import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent; +import net.dv8tion.jda.api.utils.data.DataObject; +import net.dv8tion.jda.internal.JDAImpl; +import net.dv8tion.jda.internal.entities.GuildImpl; + +public class GuildAuditLogEntryCreateHandler extends SocketHandler +{ + public GuildAuditLogEntryCreateHandler(JDAImpl api) + { + super(api); + } + + @Override + protected Long handleInternally(DataObject content) + { + final long id = content.getLong("guild_id"); + if (getJDA().getGuildSetupController().isLocked(id)) + return id; + + GuildImpl guild = (GuildImpl) getJDA().getGuildById(id); + if (guild == null) + { + getJDA().getEventCache().cache(EventCache.Type.GUILD, id, responseNumber, allContent, this::handle); + EventCache.LOG.debug("Received Guild Audit Log Create event for a Guild not yet cached. GuildId: {}", id); + return null; + } + + AuditLogEntry entry = api.getEntityBuilder().createAuditLogEntry(guild, content, null, null); + + api.handleEvent( + new GuildAuditLogEntryCreateEvent( + api, responseNumber, + entry)); + + return null; + } +} diff --git a/src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java b/src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java index 1a77131d01..76ae4ce042 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java @@ -1335,6 +1335,7 @@ protected void setupHandlers() handlers.put("CHANNEL_CREATE", new ChannelCreateHandler(api)); handlers.put("CHANNEL_DELETE", new ChannelDeleteHandler(api)); handlers.put("CHANNEL_UPDATE", new ChannelUpdateHandler(api)); + handlers.put("GUILD_AUDIT_LOG_ENTRY_CREATE", new GuildAuditLogEntryCreateHandler(api)); handlers.put("GUILD_BAN_ADD", new GuildBanHandler(api, true)); handlers.put("GUILD_BAN_REMOVE", new GuildBanHandler(api, false)); handlers.put("GUILD_CREATE", new GuildCreateHandler(api));