Skip to content

Commit

Permalink
Add GuildAuditLogEntryCreateEvent (#2380)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Jan 21, 2023
1 parent c525f6f commit 299f4d0
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 10 deletions.
33 changes: 29 additions & 4 deletions src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<String, AuditLogChange> changes, Map<String, Object> 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;
Expand Down Expand Up @@ -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.
*
* <p>This will not be available for {@link GuildAuditLogEntryCreateEvent}, you can use {@link #getUserIdLong()} instead.
*
* @return Possibly-null User instance
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>This never provides a {@link AuditLogEntry#getUser() responsible user} instance.
* You can use {@link AuditLogEntry#getUserIdLong()} instead.
*
* <p><b>Requirements</b><br>
*
* <p>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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* <p><b>Requirements</b><br>
*
* <p>This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_BANS GUILD_BANS} intent to be enabled.
* <p>This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_MODERATION GUILD_MODERATION} intent to be enabled.
*/
public class GuildBanEvent extends GenericGuildEvent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* <p><b>Requirements</b><br>
*
* <p>This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_BANS GUILD_BANS} intent to be enabled.
* <p>This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_MODERATION GUILD_MODERATION} intent to be enabled.
*/
public class GuildUnbanEvent extends GenericGuildEvent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +55,7 @@
*
* <ol>
* <li><b>GUILD_MEMBERS</b> - This is a <b>privileged</b> 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)</li>
* <li><b>GUILD_BANS</b> - This will only track guild bans and unbans</li>
* <li><b>GUILD_MODERATION</b> - This will only track guild moderation events, such as bans, unbans, and audit-logs.</li>
* <li><b>GUILD_EMOJIS</b> - This will only track custom emoji create/modify/delete. Most bots don't need this since they just use the emoji id anyway.</li>
* <li><b>GUILD_WEBHOOKS</b> - 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.</li>
* <li><b>GUILD_INVITES</b> - This will only track invite create/delete. Most bots don't make use of invites since they are added through OAuth2 authorization by administrators.</li>
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -377,8 +389,8 @@ public static EnumSet<GatewayIntent> fromEvents(@Nonnull Collection<Class<? exte
else if (GenericUserUpdateEvent.class.isAssignableFrom(event) || GenericGuildMemberEvent.class.isAssignableFrom(event) || GuildMemberRemoveEvent.class.isAssignableFrom(event))
intents.add(GUILD_MEMBERS);

else if (GuildBanEvent.class.isAssignableFrom(event) || GuildUnbanEvent.class.isAssignableFrom(event))
intents.add(GUILD_BANS);
else if (GuildBanEvent.class.isAssignableFrom(event) || GuildUnbanEvent.class.isAssignableFrom(event) || GuildAuditLogEntryCreateEvent.class.isAssignableFrom(event))
intents.add(GUILD_MODERATION);
else if (GenericEmojiEvent.class.isAssignableFrom(event) || GenericGuildStickerEvent.class.isAssignableFrom(event))
intents.add(GUILD_EMOJIS_AND_STICKERS);
else if (GenericScheduledEventUpdateEvent.class.isAssignableFrom(event))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,7 @@ public ApplicationTeam createApplicationTeam(DataObject object)
public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson, DataObject userJson, DataObject webhookJson)
{
final long targetId = entryJson.getLong("target_id", 0);
final long userId = entryJson.getLong("user_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");
Expand Down Expand Up @@ -2422,7 +2423,7 @@ public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson,
CaseInsensitiveMap<String, Object> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 299f4d0

Please sign in to comment.