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

Implement access and retrieval of message_reference #1686

Merged
merged 19 commits into from
Jul 30, 2021
38 changes: 38 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 @@ -226,6 +226,44 @@ public interface Message extends ISnowflake, Formattable
@Nullable
Message getReferencedMessage();
arynxd marked this conversation as resolved.
Show resolved Hide resolved

/**
* This returns the id of the referenced message, never null
arynxd marked this conversation as resolved.
Show resolved Hide resolved
*
* @throws java.lang.IllegalStateException
* If this message does not have a reference
*
* @return The referenced message id
*/
long getReferencedMessageIdLong();
arynxd marked this conversation as resolved.
Show resolved Hide resolved

/**
* Retrieves the referenced message for this message. If one is already present, it will be returned.
*
* @throws java.lang.IllegalStateException
* If this message does not have a reference
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link net.dv8tion.jda.api.entities.Message}
*/
RestAction<Message> retrieveReferencedMessage();

/**
* This returns the id of the referenced message, never null
*
* @throws java.lang.IllegalStateException
* If this message does not have a reference
*
* @return The referenced message id
*/
@Nonnull
String getReferencedMessageId();

/**
* This determines whether a message has a reference, for use with {@link Message#retrieveReferencedMessage()}
*
* @return Whether this message has a reference attached to it
*/
boolean hasReferencedMessage();
arynxd marked this conversation as resolved.
Show resolved Hide resolved

/**
* An immutable list of all mentioned {@link net.dv8tion.jda.api.entities.User Users}.
* <br>If no user was mentioned, this list is empty. Elements are sorted in order of appearance. This only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.time.OffsetDateTime;
import java.util.*;

import org.jetbrains.annotations.NotNull;

public abstract class AbstractMessage implements Message
{
protected static final String UNSUPPORTED = "This operation is not supported for Messages of this type!";
Expand Down Expand Up @@ -110,6 +112,34 @@ public Message getReferencedMessage()
return null;
}

@NotNull
arynxd marked this conversation as resolved.
Show resolved Hide resolved
@Override
public String getReferencedMessageId()
{
unsupported();
return null;
}

@Override
public long getReferencedMessageIdLong()
{
unsupported();
arynxd marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

@Override
public RestAction<Message> retrieveReferencedMessage()
{
unsupported();
return null;
}

@Override
public boolean hasReferencedMessage()
{
return false;
}

@Nonnull
@Override
public Bag<User> getMentionedUsersBag()
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1232,15 +1232,29 @@ else if (MISSING_CHANNEL.equals(ex.getMessage()))

if (type == MessageType.UNKNOWN)
throw new IllegalArgumentException(UNKNOWN_MESSAGE_TYPE);

arynxd marked this conversation as resolved.
Show resolved Hide resolved
MessageReference messageReference = null;

if (!jsonObject.isNull("message_reference"))
{
DataObject messageReferenceJson = jsonObject.getObject("message_reference");

messageReference = new MessageReference(
messageReferenceJson.getLong("message_id"),
messageReferenceJson.getLong("channel_id"),
referencedMessage
);
}
arynxd marked this conversation as resolved.
Show resolved Hide resolved

if (!type.isSystem())
{
message = new ReceivedMessage(id, channel, type, referencedMessage, fromWebhook,
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);
}
else
{
message = new SystemMessage(id, channel, type, fromWebhook,
message = new SystemMessage(id, channel, type, messageReference, fromWebhook,
mentionsEveryone, mentionedUsers, mentionedRoles, tts, pinned,
content, nonce, user, member, activity, editTime, reactions, attachments, embeds, stickers, flags);
return message; // We don't need to parse mentions for system messages, they are always empty anyway
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.dv8tion.jda.internal.entities;
arynxd marked this conversation as resolved.
Show resolved Hide resolved

import net.dv8tion.jda.api.entities.Message;

public class MessageReference
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class level docs, and most of the methods are missing annotations

{
private final long messageId;
private final long channelId;
private final Message referencedMessage;

public MessageReference(long messageId, long channelId, Message referencedMessage)
{
this.messageId = messageId;
this.channelId = channelId;
this.referencedMessage = referencedMessage;
}

public Message getReferencedMessage()
{
return referencedMessage;
}

public long getMessageIdLong()
{
return messageId;
}

public long getChannelIdLong()
{
return channelId;
}


public String getMessageId()
{
return Long.toUnsignedString(getMessageIdLong());
}

public String getChannelId()
{
return Long.toUnsignedString(getChannelIdLong());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.requests.CompletedRestAction;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl;
import net.dv8tion.jda.internal.requests.restaction.MessageActionImpl;
Expand All @@ -57,6 +58,7 @@ public class ReceivedMessage extends AbstractMessage
protected final long id;
protected final MessageType type;
protected final MessageChannel channel;
protected final MessageReference messageReference;
protected final Message referencedMessage;
protected final boolean fromWebhook;
protected final boolean mentionsEveryone;
Expand Down Expand Up @@ -88,15 +90,17 @@ public class ReceivedMessage extends AbstractMessage
protected List<String> invites = null;

public ReceivedMessage(
long id, MessageChannel channel, MessageType type, Message referencedMessage,
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)
{
super(content, nonce, tts);
this.id = id;
this.channel = channel;
this.referencedMessage = referencedMessage;
this.messageReference = messageReference;
this.referencedMessage = (messageReference != null) ? messageReference.getReferencedMessage() : null;
arynxd marked this conversation as resolved.
Show resolved Hide resolved

arynxd marked this conversation as resolved.
Show resolved Hide resolved
this.type = type;
this.api = (channel != null) ? (JDAImpl) channel.getJDA() : null;
this.fromWebhook = fromWebhook;
Expand Down Expand Up @@ -135,6 +139,52 @@ public Message getReferencedMessage()
return referencedMessage;
}

@Override
public long getReferencedMessageIdLong()
{
if (messageReference == null)
{
throw new IllegalStateException("Cannot get an id for a non-existent reference.");
arynxd marked this conversation as resolved.
Show resolved Hide resolved
}
return messageReference.getMessageIdLong();
}


@Nonnull
@Override
public String getReferencedMessageId()
{
if (messageReference == null)
{
throw new IllegalStateException("Cannot get an id for a non-existent reference.");
}

return Long.toUnsignedString(getReferencedMessageIdLong());
}

@Override
public boolean hasReferencedMessage()
{
return messageReference != null;
}

@Override
public RestAction<Message> retrieveReferencedMessage()
{
if (messageReference == null) {
throw new IllegalStateException("Cannot get a retrieve a message for a non-existent reference.");
}

if (referencedMessage != null) {
return new CompletedRestAction<>(getJDA(), referencedMessage);
}
arynxd marked this conversation as resolved.
Show resolved Hide resolved

Route.CompiledRoute route = Route.Messages.GET_MESSAGE.compile(messageReference.getChannelId(), getReferencedMessageId());
JDAImpl jda = (JDAImpl) getJDA();
return new RestActionImpl<>(jda, route,
(response, request) -> jda.getEntityBuilder().createMessage(response.getObject(), getChannel(), false));
}

@Override
public boolean isPinned()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
public class SystemMessage extends ReceivedMessage
{
public SystemMessage(
long id, MessageChannel channel, MessageType type,
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, int flags)
{
super(id, channel, type, null, fromWebhook, mentionsEveryone, mentionedUsers, mentionedRoles,
super(id, channel, type, messageReference, fromWebhook, mentionsEveryone, mentionedUsers, mentionedRoles,
tts, pinned, content, nonce, author, member, activity, editTime, reactions, attachments, embeds, stickers, Collections.emptyList(), flags);
}


arynxd marked this conversation as resolved.
Show resolved Hide resolved
@Nonnull
@Override
public RestAction<Void> pin()
Expand Down