Skip to content

Commit

Permalink
add Interaction Object for Messages (discord-jda#1747)
Browse files Browse the repository at this point in the history
Adds `Message#getInteraction() : Message.Interaction`
  • Loading branch information
topi314 committed Sep 5, 2021
1 parent c9c293d commit b1b5c8c
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 3 deletions.
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 @@ -2551,6 +2552,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 @@ -3124,4 +3138,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)
{
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 @@ -618,4 +618,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 @@ -1287,13 +1287,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 @@ -1538,6 +1549,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();
}
else
{
user = createUser(userJson);
}

return new Message.Interaction(id, type, name, user, member);
}

@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 @@ -335,6 +337,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

0 comments on commit b1b5c8c

Please sign in to comment.