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 Object for Messages #1747

Merged
merged 5 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
97 changes: 97 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.exceptions.HttpException;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.interactions.InteractionType;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.Button;
import net.dv8tion.jda.api.interactions.components.ComponentLayout;
Expand Down Expand Up @@ -2491,6 +2492,19 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
@Nonnull
MessageType getType();

/**
* This is sent on the message object when the message is a response to an {@link net.dv8tion.jda.api.interactions.Interaction Interaction} without an existing message.
*
* <p>This means responses to Message Components do not include this property, instead including a message reference object as components always exist on preexisting messages.
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return The {@link net.dv8tion.jda.api.entities.Message.Interaction Interaction} of this message.
*/
@Nullable
Interaction getInteraction();

/**
* Mention constants, useful for use with {@link java.util.regex.Pattern Patterns}
*/
Expand Down Expand Up @@ -3055,4 +3069,87 @@ public boolean isSpoiler()
}

}

/**
* Represents an {@link net.dv8tion.jda.api.interactions.Interaction Interaction} provided with a {@link net.dv8tion.jda.api.entities.Message Message}.
*/
class Interaction implements ISnowflake
{
private final long id;
private final int type;
private final String name;
private final User user;
private final Member member;

public Interaction(long id, int type, String name, User user, Member member)
DV8FromTheWorld marked this conversation as resolved.
Show resolved Hide resolved
{
this.id = id;
this.type = type;
this.name = name;
this.user = user;
this.member = member;
}

@Override
public long getIdLong()
{
return id;
}

/**
* The raw interaction type.
* <br>It is recommended to use {@link #getType()} instead.
*
* @return The raw interaction type
*/
public int getTypeRaw()
{
return type;
}

/**
* The {@link net.dv8tion.jda.api.interactions.InteractionType} for this interaction.
*
* @return The {@link net.dv8tion.jda.api.interactions.InteractionType} or {@link net.dv8tion.jda.api.interactions.InteractionType#UNKNOWN}
*/
@Nonnull
public InteractionType getType()
{
return InteractionType.fromKey(getTypeRaw());
}

/**
* The command name.
*
* @return The command name
*/
@Nonnull
public String getName()
{
return name;
}

/**
* The {@link User} who caused this interaction.
*
* @return The {@link User}
*/
@Nonnull
public User getUser()
{
return user;
}

/**
* The {@link Member} who caused this interaction.
* <br>This is null if the interaction is not from a guild.
*
* @return The {@link Member}
*/
@Nullable
public Member getMember()
{
return member;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,12 @@ public MessageType getType()
unsupported();
return null;
}

@Nullable
@Override
public Message.Interaction getInteraction()
{
unsupported();
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1285,13 +1285,24 @@ else if (MISSING_CHANNEL.equals(ex.getMessage()))
.collect(Collectors.toList());
}

Message.Interaction messageInteraction = null;
if (!jsonObject.isNull("interaction"))
{
GuildImpl guild = null;
if (channel instanceof GuildChannel)
{
guild = (GuildImpl) ((GuildChannel) (channel)).getGuild();
}
messageInteraction = createMessageInteraction(guild, jsonObject.getObject("interaction"));
}

if (type == MessageType.UNKNOWN)
throw new IllegalArgumentException(UNKNOWN_MESSAGE_TYPE);
if (!type.isSystem())
{
message = new ReceivedMessage(id, channel, type, messageReference, fromWebhook,
mentionsEveryone, mentionedUsers, mentionedRoles, tts, pinned,
content, nonce, user, member, activity, editTime, reactions, attachments, embeds, stickers, components, flags);
content, nonce, user, member, activity, editTime, reactions, attachments, embeds, stickers, components, flags, messageInteraction);
}
else
{
Expand Down Expand Up @@ -1536,6 +1547,29 @@ public MessageSticker createSticker(DataObject content)
return new MessageSticker(id, name, description, packId, asset, format, tags);
}

public Message.Interaction createMessageInteraction(GuildImpl guildImpl, DataObject content)
{
final long id = content.getLong("id");
final int type = content.getInt("type");
final String name = content.getString("name");
DataObject userJson = content.getObject("user");
User user = null;
MemberImpl member = null;
if (!content.isNull("member") && guildImpl != null)
{
DataObject memberJson = content.getObject("member");
memberJson.put("user", userJson);
member = createMember(guildImpl, memberJson);
user = member.getUser();
DV8FromTheWorld marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
user = createUser(userJson);
DV8FromTheWorld marked this conversation as resolved.
Show resolved Hide resolved
}

return new Message.Interaction(id, type, name, user, member);
DV8FromTheWorld marked this conversation as resolved.
Show resolved Hide resolved
}

@Nullable
public PermissionOverride createPermissionOverride(DataObject override, AbstractChannelImpl<?, ?> chan)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class ReceivedMessage extends AbstractMessage
protected final TLongSet mentionedUsers;
protected final TLongSet mentionedRoles;
protected final int flags;
protected final Message.Interaction interaction;

protected InteractionHook interactionHook = null; // late-init

Expand All @@ -93,7 +94,7 @@ public ReceivedMessage(
long id, MessageChannel channel, MessageType type, MessageReference messageReference,
boolean fromWebhook, boolean mentionsEveryone, TLongSet mentionedUsers, TLongSet mentionedRoles, boolean tts, boolean pinned,
String content, String nonce, User author, Member member, MessageActivity activity, OffsetDateTime editTime,
List<MessageReaction> reactions, List<Attachment> attachments, List<MessageEmbed> embeds, List<MessageSticker> stickers, List<ActionRow> components, int flags)
List<MessageReaction> reactions, List<Attachment> attachments, List<MessageEmbed> embeds, List<MessageSticker> stickers, List<ActionRow> components, int flags, Message.Interaction interaction)
{
super(content, nonce, tts);
this.id = id;
Expand All @@ -116,6 +117,7 @@ public ReceivedMessage(
this.mentionedUsers = mentionedUsers;
this.mentionedRoles = mentionedRoles;
this.flags = flags;
this.interaction = interaction;
}

public ReceivedMessage withHook(InteractionHook hook)
Expand Down Expand Up @@ -301,6 +303,13 @@ public MessageType getType()
return type;
}

@Nullable
@Override
public Interaction getInteraction()
{
return interaction;
}

@Override
public long getIdLong()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public SystemMessage(
List<MessageReaction> reactions, List<Attachment> attachments, List<MessageEmbed> embeds, List<MessageSticker> stickers, int flags)
{
super(id, channel, type, messageReference, fromWebhook, mentionsEveryone, mentionedUsers, mentionedRoles,
tts, pinned, content, nonce, author, member, activity, editTime, reactions, attachments, embeds, stickers, Collections.emptyList(), flags);
tts, pinned, content, nonce, author, member, activity, editTime, reactions, attachments, embeds, stickers, Collections.emptyList(), flags, null);
}

@Nonnull
Expand Down