Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GuildAuditLogEntryCreateEvent #2380

Merged
merged 3 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 the a {@link Guild}.
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
*
* <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_BANS GUILD_BANS} 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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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 @@ -377,7 +378,7 @@ 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))
else if (GuildBanEvent.class.isAssignableFrom(event) || GuildUnbanEvent.class.isAssignableFrom(event) || GuildAuditLogEntryCreateEvent.class.isAssignableFrom(event))
intents.add(GUILD_BANS);
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
else if (GenericEmojiEvent.class.isAssignableFrom(event) || GenericGuildStickerEvent.class.isAssignableFrom(event))
intents.add(GUILD_EMOJIS_AND_STICKERS);
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