From f4235d13d2c81aa0ed9aa4be1a315eb82c47554d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Sun, 8 May 2022 14:26:23 +0200 Subject: [PATCH] Add new message types for automod (#2116) Fixes parsing errors on retrieve message methods --- .../dv8tion/jda/api/entities/EmbedType.java | 1 + .../net/dv8tion/jda/api/entities/Message.java | 1 + .../jda/api/entities/MessageChannel.java | 10 ++++++---- .../dv8tion/jda/api/entities/MessageType.java | 18 ++++++++++++++++++ .../jda/internal/entities/ReceivedMessage.java | 2 ++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/entities/EmbedType.java b/src/main/java/net/dv8tion/jda/api/entities/EmbedType.java index 08f5a51388..bfcd676d00 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/EmbedType.java +++ b/src/main/java/net/dv8tion/jda/api/entities/EmbedType.java @@ -28,6 +28,7 @@ public enum EmbedType VIDEO("video"), LINK("link"), RICH("rich"), + AUTO_MODERATION("auto_moderation_message"), UNKNOWN(""); private final String key; diff --git a/src/main/java/net/dv8tion/jda/api/entities/Message.java b/src/main/java/net/dv8tion/jda/api/entities/Message.java index 786e174168..0aef2f7bb4 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Message.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Message.java @@ -1394,6 +1394,7 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull *
  • If this Message was not sent by the currently logged in account and it was not sent in a * {@link GuildChannel GuildChannel}.
  • *
  • If this Message is ephemeral
  • + *
  • If this message type cannot be deleted. (See {@link MessageType#canDelete()})
  • * * * @return {@link net.dv8tion.jda.api.requests.restaction.AuditableRestAction AuditableRestAction} diff --git a/src/main/java/net/dv8tion/jda/api/entities/MessageChannel.java b/src/main/java/net/dv8tion/jda/api/entities/MessageChannel.java index 82b096c445..377550273b 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/MessageChannel.java +++ b/src/main/java/net/dv8tion/jda/api/entities/MessageChannel.java @@ -207,6 +207,8 @@ default List> 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. * + *

    Any messages that cannot be deleted, as suggested by {@link MessageType#canDelete()}, will be filtered out before making any requests. + * *

    For possible ErrorResponses see {@link #purgeMessagesById(long...)}. * * @param messages @@ -226,10 +228,10 @@ default List> purgeMessages(@Nonnull List m.getType().canDelete()) + .mapToLong(Message::getIdLong) + .toArray()); } /** diff --git a/src/main/java/net/dv8tion/jda/api/entities/MessageType.java b/src/main/java/net/dv8tion/jda/api/entities/MessageType.java index a109a04084..a42553241f 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/MessageType.java +++ b/src/main/java/net/dv8tion/jda/api/entities/MessageType.java @@ -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. */ @@ -184,6 +191,17 @@ public boolean isSystem() return system; } + /** + * Whether messages of this type can be deleted. + *
    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. *
    If the {@code id} provided is not a known id, {@link #UNKNOWN} is returned diff --git a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java index b6177fdaaf..8d858e5060 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java @@ -640,6 +640,8 @@ public AuditableRestAction 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()); }