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 interaction related message flags #1790

Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ebc29cc
Add MessageFlag#EPHEMERAL and MessageFlag#LOADING
freya022 Aug 27, 2021
fd08d8a
Fix ComponentInteraction#getMessage docs
freya022 Aug 27, 2021
8b66729
Merge branch 'DV8FromTheWorld:development' into feature/add-missing-m…
freya022 Aug 27, 2021
8f823ae
Merge remote-tracking branch 'origin/feature/add-missing-message-flag…
freya022 Aug 27, 2021
3c885cf
Enable ButtonInteraction#editButton and SelectionMenuInteraction#edit…
freya022 Aug 27, 2021
430b303
Update src/main/java/net/dv8tion/jda/api/entities/Message.java
freya022 Aug 30, 2021
3c2d228
Update src/main/java/net/dv8tion/jda/api/entities/Message.java
freya022 Aug 30, 2021
a533125
Add MessageFlag#EPHEMERAL and MessageFlag#LOADING
freya022 Aug 27, 2021
a9c2fcb
Fix ComponentInteraction#getMessage docs
freya022 Aug 27, 2021
6cb8412
Enable ButtonInteraction#editButton and SelectionMenuInteraction#edit…
freya022 Aug 27, 2021
8f48ecc
Update src/main/java/net/dv8tion/jda/api/entities/Message.java
freya022 Aug 30, 2021
620a5d3
Update src/main/java/net/dv8tion/jda/api/entities/Message.java
freya022 Aug 30, 2021
804d2f3
Merge remote-tracking branch 'origin/feature/add-missing-message-flag…
freya022 Aug 30, 2021
6c95138
Add exception docs to Message#isEphemeral
freya022 Aug 30, 2021
06fb296
Update src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessag…
freya022 Aug 30, 2021
650dda6
Add missing ephemeral checks (basically on all methods that uses a me…
freya022 Aug 30, 2021
f7b29de
Add Message#getFlagsRaw
freya022 Aug 30, 2021
d968a2c
Better exception wording
freya022 Aug 30, 2021
d59afa8
Update src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessag…
freya022 Aug 31, 2021
e37a87b
Update src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessag…
freya022 Aug 31, 2021
bba6eb9
Update src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessag…
freya022 Aug 31, 2021
3f59126
More better wording
freya022 Aug 31, 2021
b013c36
Remove ephemeral check from Message#getJumpUrl
freya022 Aug 31, 2021
76d993c
Add exceptions to docs
freya022 Aug 31, 2021
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
32 changes: 31 additions & 1 deletion src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -2476,6 +2476,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();
freya022 marked this conversation as resolved.
Show resolved Hide resolved

/**
* This specifies the {@link net.dv8tion.jda.api.entities.MessageType MessageType} of this Message.
*
Expand Down Expand Up @@ -2583,7 +2604,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
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,29 @@ public boolean isPinned()
@Override
public RestAction<Void> pin()
{
if (isEphemeral())
throw new IllegalStateException("Cannot pin this message as it is ephemeral.");
freya022 marked this conversation as resolved.
Show resolved Hide resolved

return channel.pinMessageById(getId());
}

@Nonnull
@Override
public RestAction<Void> unpin()
{
if (isEphemeral())
throw new IllegalStateException("Cannot unpin this message as it is ephemeral.");

return channel.unpinMessageById(getId());
}

@Nonnull
@Override
public RestAction<Void> addReaction(@Nonnull Emote emote)
{
if (isEphemeral())
throw new IllegalStateException("Cannot add reactions on this message as it is ephemeral.");

Checks.notNull(emote, "Emote");

boolean missingReaction = reactions.stream()
Expand All @@ -181,13 +190,18 @@ public RestAction<Void> addReaction(@Nonnull Emote emote)
@Override
public RestAction<Void> addReaction(@Nonnull String unicode)
{
if (isEphemeral())
throw new IllegalStateException("Cannot react on this message as it is ephemeral.");

return channel.addReactionById(getId(), unicode);
}

@Nonnull
@Override
public RestAction<Void> clearReactions()
{
if (isEphemeral())
throw new IllegalStateException("Cannot clear reactions on this message as it is ephemeral.");
if (!isFromGuild())
throw new IllegalStateException("Cannot clear reactions from a message in a Group or PrivateChannel.");
return getTextChannel().clearReactionsById(getId());
Expand All @@ -197,6 +211,8 @@ public RestAction<Void> clearReactions()
@Override
public RestAction<Void> clearReactions(@Nonnull String unicode)
{
if (isEphemeral())
throw new IllegalStateException("Cannot clear reactions on this message as it is ephemeral.");
if (!isFromGuild())
throw new IllegalStateException("Cannot clear reactions from a message in a Group or PrivateChannel.");
return getTextChannel().clearReactionsById(getId(), unicode);
Expand All @@ -206,6 +222,8 @@ public RestAction<Void> clearReactions(@Nonnull String unicode)
@Override
public RestAction<Void> clearReactions(@Nonnull Emote emote)
{
if (isEphemeral())
throw new IllegalStateException("Cannot clear reactions on this message as it is ephemeral.");
if (!isFromGuild())
throw new IllegalStateException("Cannot clear reactions from a message in a Group or PrivateChannel.");
return getTextChannel().clearReactionsById(getId(), emote);
Expand All @@ -215,6 +233,9 @@ public RestAction<Void> clearReactions(@Nonnull Emote emote)
@Override
public RestAction<Void> removeReaction(@Nonnull Emote emote)
{
if (isEphemeral())
throw new IllegalStateException("Cannot remove reactions on this message as it is ephemeral.");

return channel.removeReactionById(getId(), emote);
}

Expand All @@ -223,6 +244,10 @@ public RestAction<Void> removeReaction(@Nonnull Emote emote)
public RestAction<Void> removeReaction(@Nonnull Emote emote, @Nonnull User user)
{
Checks.notNull(user, "User"); // to prevent NPEs

if (isEphemeral())
throw new IllegalStateException("Cannot remove reactions on this message as it is ephemeral.");

// check if the passed user is the SelfUser, then the ChannelType doesn't matter and
// we can safely remove that
if (user.equals(getJDA().getSelfUser()))
Expand All @@ -237,6 +262,9 @@ public RestAction<Void> removeReaction(@Nonnull Emote emote, @Nonnull User user)
@Override
public RestAction<Void> removeReaction(@Nonnull String unicode)
{
if (isEphemeral())
throw new IllegalStateException("Cannot remove reactions on this message as it is ephemeral.");

return channel.removeReactionById(getId(), unicode);
}

Expand All @@ -248,6 +276,8 @@ public RestAction<Void> removeReaction(@Nonnull String unicode, @Nonnull User us
if (user.equals(getJDA().getSelfUser()))
return channel.removeReactionById(getIdLong(), unicode);

if (isEphemeral())
throw new IllegalStateException("Cannot remove reactions on this message as it is ephemeral.");
if (!isFromGuild())
throw new IllegalStateException("Cannot remove reactions of others from a message in a Group or PrivateChannel.");
return getTextChannel().removeReactionById(getId(), unicode, user);
Expand All @@ -257,13 +287,19 @@ public RestAction<Void> removeReaction(@Nonnull String unicode, @Nonnull User us
@Override
public ReactionPaginationAction retrieveReactionUsers(@Nonnull Emote emote)
{
if (isEphemeral())
throw new IllegalStateException("Cannot retrieve reactions on this message as it is ephemeral.");

return channel.retrieveReactionUsersById(id, emote);
}

@Nonnull
@Override
public ReactionPaginationAction retrieveReactionUsers(@Nonnull String unicode)
{
if (isEphemeral())
throw new IllegalStateException("Cannot retrieve reactions on this message as it is ephemeral.");

return channel.retrieveReactionUsersById(id, unicode);
}

Expand Down Expand Up @@ -311,6 +347,9 @@ public long getIdLong()
@Override
public String getJumpUrl()
{
if (isEphemeral())
throw new IllegalStateException("Cannot get the jump URL on this message as it is ephemeral.");

return String.format("https://discord.com/channels/%s/%s/%s", isFromGuild() ? getGuild().getId() : "@me", getChannel().getId(), getId());
}

Expand Down Expand Up @@ -856,6 +895,9 @@ private void checkUser()
@Override
public AuditableRestAction<Void> delete()
{
if (isEphemeral())
throw new IllegalStateException("Cannot delete this message as it is ephemeral.");

if (!getJDA().getSelfUser().equals(getAuthor()))
{
if (isFromType(ChannelType.PRIVATE))
Expand All @@ -873,6 +915,9 @@ else if (!getGuild().getSelfMember()
@Override
public AuditableRestAction<Void> suppressEmbeds(boolean suppressed)
{
if (isEphemeral())
throw new IllegalStateException("Cannot suppress embeds on this message as it is ephemeral.");
freya022 marked this conversation as resolved.
Show resolved Hide resolved

if (!getJDA().getSelfUser().equals(getAuthor()))
{
if (isFromType(ChannelType.PRIVATE))
Expand All @@ -895,6 +940,9 @@ else if (!getGuild().getSelfMember().hasPermission(getTextChannel(), Permission.
@Override
public RestAction<Message> crosspost()
{
if (isEphemeral())
throw new IllegalStateException("Cannot suppress embeds on this message as it is ephemeral.");

if (getFlags().contains(MessageFlag.CROSSPOSTED))
return new CompletedRestAction<>(getJDA(), this);
TextChannel textChannel = getTextChannel();
Expand All @@ -918,6 +966,18 @@ public EnumSet<MessageFlag> getFlags()
return MessageFlag.fromBitField(flags);
}

@Override
public long getFlagsRaw()
{
return flags;
}

@Override
public boolean isEphemeral()
{
return (this.flags & MessageFlag.EPHEMERAL.getValue()) != 0;
}

@Override
public boolean equals(Object o)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.dv8tion.jda.internal.requests.restaction.interactions.UpdateInteractionActionImpl;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public abstract class ComponentInteractionImpl extends InteractionImpl implements ComponentInteraction
{
Expand Down Expand Up @@ -58,7 +57,7 @@ public String getComponentId()
return customId;
}

@Nullable
@Nonnull
@Override
public Message getMessage()
{
Expand Down