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 1 commit
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
58 changes: 58 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 @@ -2471,6 +2472,9 @@ default MessageAction reply(@Nonnull byte[] data, @Nonnull String name, @Nonnull
@Nonnull
MessageType getType();

@Nullable
Interaction getInteraction();

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

}

class Interaction implements ISnowflake
{

topi314 marked this conversation as resolved.
Show resolved Hide resolved
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;
}

public int getTypeRaw()
{
return type;
}

@Nonnull
public InteractionType getType()
{
return InteractionType.fromKey(getTypeRaw());
}

@Nonnull
public String getName()
{
return name;
}

@Nonnull
public User getUser()
{
return user;
}

@Nullable
public Member getMember()
{
return member;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.commons.collections4.Bag;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -601,4 +602,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 @@ -1270,13 +1270,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, referencedMessage, 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 @@ -1521,6 +1532,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, Message referencedMessage,
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 @@ -300,6 +302,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, null, 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