Skip to content

Commit

Permalink
Add interaction related message flags (#1790)
Browse files Browse the repository at this point in the history
Adds support for `Message#isEphemeral() : boolean` and `Message#getFlagsRaw() : long`
  • Loading branch information
freya022 committed Sep 5, 2021
1 parent 187fffc commit 0cec14e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 29 deletions.
103 changes: 86 additions & 17 deletions src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -1615,8 +1615,11 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* does not have {@link net.dv8tion.jda.api.Permission#MESSAGE_MANAGE Permission.MESSAGE_MANAGE} in
* the channel.
* @throws java.lang.IllegalStateException
* If this Message was not sent by the currently logged in account and it was <b>not</b> sent in a
* {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.
* <ul>
* <li>If this Message was not sent by the currently logged in account and it was <b>not</b> sent in a
* {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.</li>
* <li>If this Message is ephemeral</li>
* </ul>
*
* @return {@link net.dv8tion.jda.api.requests.restaction.AuditableRestAction AuditableRestAction}
*
Expand Down Expand Up @@ -1680,6 +1683,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* <li>Missing {@link net.dv8tion.jda.api.Permission#MESSAGE_MANAGE Permission.MESSAGE_MANAGE}.
* <br>Required to actually pin the Message.</li>
* </ul>
* @throws IllegalStateException
* If this Message is ephemeral
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*/
Expand Down Expand Up @@ -1719,6 +1724,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* <li>Missing {@link net.dv8tion.jda.api.Permission#MESSAGE_MANAGE Permission.MESSAGE_MANAGE}.
* <br>Required to actually pin the Message.</li>
* </ul>
* @throws IllegalStateException
* If this Message is ephemeral
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*/
Expand Down Expand Up @@ -1781,6 +1788,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* <li>If the provided {@link net.dv8tion.jda.api.entities.Emote Emote} cannot be used in the current channel.
* See {@link Emote#canInteract(User, MessageChannel)} or {@link Emote#canInteract(Member)} for more information.</li>
* </ul>
* @throws IllegalStateException
* If this message is ephemeral
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*/
Expand Down Expand Up @@ -1850,6 +1859,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* </ul>
* @throws java.lang.IllegalArgumentException
* If the provided unicode emoji is null or empty.
* @throws IllegalStateException
* If this Message is ephemeral
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*/
Expand Down Expand Up @@ -1888,8 +1899,11 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* and the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MESSAGE_MANAGE Permission.MESSAGE_MANAGE}
* in the channel.
* @throws java.lang.IllegalStateException
* If this message was <b>not</b> sent in a
* {@link net.dv8tion.jda.api.entities.Guild Guild}.
* <ul>
* <li>If this message was <b>not</b> sent in a {@link net.dv8tion.jda.api.entities.Guild Guild}.</li>
* <li>If this message is ephemeral</li>
* </ul>
*
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*/
Expand Down Expand Up @@ -1937,8 +1951,11 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* @throws IllegalArgumentException
* If provided with null
* @throws java.lang.IllegalStateException
* If this message was <b>not</b> sent in a
* {@link net.dv8tion.jda.api.entities.Guild Guild}.
* <ul>
* <li>If this message was <b>not</b> sent in a {@link net.dv8tion.jda.api.entities.Guild Guild}.</li>
* <li>If this message is ephemeral</li>
* </ul>
*
*
* @return {@link RestAction}
*
Expand Down Expand Up @@ -1976,8 +1993,10 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* @throws IllegalArgumentException
* If provided with null
* @throws java.lang.IllegalStateException
* If this message was <b>not</b> sent in a
* {@link net.dv8tion.jda.api.entities.Guild Guild}.
* <ul>
* <li>If this message was <b>not</b> sent in a {@link net.dv8tion.jda.api.entities.Guild Guild}.</li>
* <li>If this message is ephemeral</li>
* </ul>
*
* @return {@link RestAction}
*
Expand Down Expand Up @@ -2027,6 +2046,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* <li>If the provided {@link net.dv8tion.jda.api.entities.Emote Emote} cannot be used in the current channel.
* See {@link Emote#canInteract(User, MessageChannel)} or {@link Emote#canInteract(Member)} for more information.</li>
* </ul>
* @throws IllegalStateException
* If this is an ephemeral message
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*
Expand Down Expand Up @@ -2086,9 +2107,13 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* <li>If the provided user is null</li>
* </ul>
* @throws java.lang.IllegalStateException
* If this message was <b>not</b> sent in a
* <ul>
* <li>If this message was <b>not</b> sent in a
* {@link net.dv8tion.jda.api.entities.Guild Guild}
* <b>and</b> the given user is <b>not</b> the {@link net.dv8tion.jda.api.entities.SelfUser SelfUser}.
* <b>and</b> the given user is <b>not</b> the {@link net.dv8tion.jda.api.entities.SelfUser SelfUser}.</li>
* <li>If this message is ephemeral</li>
* </ul>
*
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*
Expand Down Expand Up @@ -2146,6 +2171,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* and the logged in account does not have {@link net.dv8tion.jda.api.Permission#MESSAGE_HISTORY Permission.MESSAGE_HISTORY}
* @throws java.lang.IllegalArgumentException
* If the provided unicode emoji is null or empty.
* @throws IllegalStateException
* If this is an ephemeral message
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*
Expand Down Expand Up @@ -2206,9 +2233,14 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* @throws java.lang.IllegalArgumentException
* If the provided unicode emoji is null or empty or if the provided user is null.
* @throws java.lang.IllegalStateException
* If this message was <b>not</b> sent in a
* {@link net.dv8tion.jda.api.entities.Guild Guild}
* <b>and</b> the given user is <b>not</b> the {@link net.dv8tion.jda.api.entities.SelfUser SelfUser}.
* If this message:
* <ul>
* <li>Was <b>not</b> sent in a
* {@link net.dv8tion.jda.api.entities.Guild Guild}
* <b>and</b> the given user is <b>not</b> the {@link net.dv8tion.jda.api.entities.SelfUser SelfUser}.</li>
* <li>Is ephemeral</li>
* </ul>
*
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link java.lang.Void}
*
Expand Down Expand Up @@ -2250,7 +2282,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* logged in account does not have {@link net.dv8tion.jda.api.Permission#MESSAGE_HISTORY Permission.MESSAGE_HISTORY} in the channel.
* @throws java.lang.IllegalArgumentException
* If the provided {@link net.dv8tion.jda.api.entities.Emote Emote} is null.
*
* @throws IllegalStateException
* If this Message is ephemeral
* @return The {@link net.dv8tion.jda.api.requests.restaction.pagination.ReactionPaginationAction ReactionPaginationAction} of the emote's users.
*
* @since 4.1.0
Expand Down Expand Up @@ -2293,7 +2326,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* logged in account does not have {@link net.dv8tion.jda.api.Permission#MESSAGE_HISTORY Permission.MESSAGE_HISTORY} in the channel.
* @throws java.lang.IllegalArgumentException
* If the provided unicode emoji is null or empty.
*
* @throws IllegalStateException
* If this Message is ephemeral
* @return The {@link net.dv8tion.jda.api.requests.restaction.pagination.ReactionPaginationAction ReactionPaginationAction} of the emoji's users.
*
* @since 4.1.0
Expand Down Expand Up @@ -2403,6 +2437,8 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* @throws net.dv8tion.jda.api.exceptions.PermissionException
* If the MessageChannel this message was sent in was a {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel}
* and the message was not sent by the currently logged in account.
* @throws IllegalStateException
* If this Message is ephemeral
* @return {@link net.dv8tion.jda.api.requests.restaction.AuditableRestAction AuditableRestAction} - Type: {@link java.lang.Void}
* @see #isSuppressedEmbeds()
*/
Expand Down Expand Up @@ -2438,7 +2474,10 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
* @throws java.lang.UnsupportedOperationException
* If this is a system message
* @throws IllegalStateException
* If the channel is not a text or news channel. See {@link TextChannel#isNews()}.
* <ul>
* <li>If the channel is not a text or news channel. See {@link TextChannel#isNews()}.</li>
* <li>If the message is ephemeral.</li>
* </ul>
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have
* {@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL Permission.VIEW_CHANNEL} in this channel
Expand Down Expand Up @@ -2476,6 +2515,27 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
@Nonnull
EnumSet<MessageFlag> getFlags();

/**
* Returns the raw message flags of this message
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
* @return The raw message flags
* @see #getFlags()
*/
long getFlagsRaw();

/**
* Whether this message is ephemeral.
* <br>The message being ephemeral means it is only visible to the bot and the interacting user
* <br>This is a shortcut method for checking if {@link #getFlags()} contains {@link MessageFlag#EPHEMERAL}
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
* @return Whether the message is ephemeral
*/
boolean isEphemeral();

/**
* This specifies the {@link net.dv8tion.jda.api.entities.MessageType MessageType} of this Message.
*
Expand Down Expand Up @@ -2583,7 +2643,16 @@ enum MessageFlag
/**
* Indicates, that this Message came from the urgent message system
*/
URGENT(4);
URGENT(4),
/**
* Indicates, that this Message is ephemeral, the Message is only visible to the bot and the interacting user
* @see Message#isEphemeral
*/
EPHEMERAL(6),
/**
* Indicates, that this Message is an interaction response and the bot is "thinking"
*/
LOADING(7);

private final int value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Component getComponent()
return interaction.getComponent();
}

@Nullable
@Nonnull
@Override
public Message getMessage()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ default Button getComponent()

/**
* Update the button with a new button instance.
* <br>This only works for non-ephemeral messages where {@link #getMessage()} is available!
*
* <p>If this interaction is already acknowledged this will use {@link #getHook()}
* and otherwise {@link #editComponents(Collection)} directly to acknowledge the interaction.
Expand All @@ -71,8 +70,6 @@ default Button getComponent()
default RestAction<Void> editButton(@Nullable Button newButton)
{
Message message = getMessage();
if (message == null)
throw new IllegalStateException("Cannot update button for ephemeral messages! Discord does not provide enough information to perform the update.");
List<ActionRow> components = new ArrayList<>(message.getActionRows());
ComponentLayout.updateComponent(components, getComponentId(), newButton);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ public interface ComponentInteraction extends Interaction

/**
* The {@link Message} instance.
* <br>This is null on interactions for ephemeral messages.
*
* @return The {@link Message}, or null if this message is ephemeral
* @return The {@link Message}
*/
@Nullable
@Nonnull
Message getMessage();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ default List<SelectOption> getSelectedOptions()

/**
* Update the selection menu with a new selection menu instance.
* <br>This only works for non-ephemeral messages where {@link #getMessage()} is available!
*
* <p>If this interaction is already acknowledged this will use {@link #getHook()}
* and otherwise {@link #editComponents(Collection)} directly to acknowledge the interaction.
Expand All @@ -106,8 +105,6 @@ default List<SelectOption> getSelectedOptions()
default RestAction<Void> editSelectionMenu(@Nullable SelectionMenu newMenu)
{
Message message = getMessage();
if (message == null)
throw new IllegalStateException("Cannot update selection menu for ephemeral messages! Discord does not provide enough information to perform the update.");
List<ActionRow> components = new ArrayList<>(message.getActionRows());
ComponentLayout.updateComponent(components, getComponentId(), newMenu);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,20 @@ public EnumSet<MessageFlag> getFlags()
return null;
}

@Override
public long getFlagsRaw()
{
unsupported();
return 0;
}

@Override
public boolean isEphemeral()
{
unsupported();
return false;
}

@Nonnull
@Override
public MessageType getType()
Expand Down
Loading

0 comments on commit 0cec14e

Please sign in to comment.