Skip to content

Commit

Permalink
Add new message types for automod (#2116)
Browse files Browse the repository at this point in the history
Fixes parsing errors on retrieve message methods
  • Loading branch information
MinnDevelopment authored May 8, 2022
1 parent 975c470 commit f4235d1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/api/entities/EmbedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum EmbedType
VIDEO("video"),
LINK("link"),
RICH("rich"),
AUTO_MODERATION("auto_moderation_message"),
UNKNOWN("");

private final String key;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* <li>If this Message was not sent by the currently logged in account and it was <b>not</b> sent in a
* {@link GuildChannel GuildChannel}.</li>
* <li>If this Message is ephemeral</li>
* <li>If this message type cannot be deleted. (See {@link MessageType#canDelete()})</li>
* </ul>
*
* @return {@link net.dv8tion.jda.api.requests.restaction.AuditableRestAction AuditableRestAction}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/net/dv8tion/jda/api/entities/MessageChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ default List<CompletableFuture<Void>> purgeMessages(@Nonnull Message... messages
* to delete all messages provided. No checks will be done to prevent failures, use {@link java.util.concurrent.CompletionStage#exceptionally(Function)}
* to handle failures.
*
* <p>Any messages that cannot be deleted, as suggested by {@link MessageType#canDelete()}, will be filtered out before making any requests.
*
* <p>For possible ErrorResponses see {@link #purgeMessagesById(long...)}.
*
* @param messages
Expand All @@ -226,10 +228,10 @@ default List<CompletableFuture<Void>> purgeMessages(@Nonnull List<? extends Mess
{
if (messages == null || messages.isEmpty())
return Collections.emptyList();
long[] ids = new long[messages.size()];
for (int i = 0; i < ids.length; i++)
ids[i] = messages.get(i).getIdLong();
return purgeMessagesById(ids);
return purgeMessagesById(messages.stream()
.filter(m -> m.getType().canDelete())
.mapToLong(Message::getIdLong)
.toArray());
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/MessageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public enum MessageType
*/
CONTEXT_COMMAND(23, false),

/**
* This message was created by the automod system.
*
* Messages from this type usually come with custom embeds containing relevant information, the author is the user that triggered the filter.
*/
AUTO_MODERATION_ACTION(24),

/**
* Unknown MessageType.
*/
Expand Down Expand Up @@ -184,6 +191,17 @@ public boolean isSystem()
return system;
}

/**
* Whether messages of this type can be deleted.
* <br>Currently the only type that cannot be deleted is {@link #AUTO_MODERATION_ACTION}.
*
* @return True, if delete is supported
*/
public boolean canDelete()
{
return this != AUTO_MODERATION_ACTION;
}

/**
* Used to retrieve a MessageType based on the Discord id key.
* <br>If the {@code id} provided is not a known id, {@link #UNKNOWN} is returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ public AuditableRestAction<Void> delete()
else if (!sMember.hasPermission(gChan, Permission.MESSAGE_MANAGE))
throw new InsufficientPermissionException(gChan, Permission.MESSAGE_MANAGE);
}
if (!type.canDelete())
throw new IllegalStateException("Cannot delete messages of type " + type);
return channel.deleteMessageById(getIdLong());
}

Expand Down

0 comments on commit f4235d1

Please sign in to comment.