diff --git a/src/main/java/net/dv8tion/jda/api/entities/Message.java b/src/main/java/net/dv8tion/jda/api/entities/Message.java index 1c513dfcfc..127eef6e38 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Message.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Message.java @@ -360,6 +360,25 @@ default Message getReferencedMessage() @Nullable Member getMember(); + /** + * Returns the approximate position of this message in a {@link ThreadChannel}. + *
This can be used to estimate the relative position of a message in a thread, by comparing against {@link ThreadChannel#getTotalMessageCount()}. + * + *

Notes: + *

+ * + * @throws IllegalStateException + * If this message was not sent in a {@link ThreadChannel}. + * + * @return The approximate position of this message, or {@code -1} if this message is too old. + * + * @see Discord docs: position property on the message object + */ + int getApproximatePosition(); + /** * Returns the jump-to URL for the received message. Clicking this URL in the Discord client will cause the client to * jump to the specified message. diff --git a/src/main/java/net/dv8tion/jda/internal/entities/AbstractMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/AbstractMessage.java index 2aade7038c..f267f73ba2 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/AbstractMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/AbstractMessage.java @@ -159,6 +159,13 @@ public Member getMember() return null; } + @Override + public int getApproximatePosition() + { + unsupported(); + return 0; + } + @Nonnull @Override public String getJumpUrl() diff --git a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java index 70ae322447..450c427ca4 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java @@ -1751,15 +1751,17 @@ else if (MISSING_CHANNEL.equals(ex.getMessage())) if (guild != null && !jsonObject.isNull("thread")) startedThread = createThreadChannel(guild, jsonObject.getObject("thread"), guild.getIdLong()); + int position = jsonObject.getInt("position", -1); + if (!type.isSystem()) { return new ReceivedMessage(id, channel, type, messageReference, fromWebhook, applicationId, tts, pinned, - content, nonce, user, member, activity, editTime, mentions, reactions, attachments, embeds, stickers, components, flags, messageInteraction, startedThread); + content, nonce, user, member, activity, editTime, mentions, reactions, attachments, embeds, stickers, components, flags, messageInteraction, startedThread, position); } else { return new SystemMessage(id, channel, type, messageReference, fromWebhook, applicationId, tts, pinned, - content, nonce, user, member, activity, editTime, mentions, reactions, attachments, embeds, stickers, flags, startedThread); + content, nonce, user, member, activity, editTime, mentions, reactions, attachments, embeds, stickers, flags, startedThread, position); } } diff --git a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java index 83c7bb52eb..ffacef8177 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java @@ -88,6 +88,7 @@ public class ReceivedMessage extends AbstractMessage protected final int flags; protected final Message.Interaction interaction; protected final ThreadChannel startedThread; + protected final int position; // LAZY EVALUATED protected String altContent = null; @@ -101,7 +102,7 @@ public ReceivedMessage( String content, String nonce, User author, Member member, MessageActivity activity, OffsetDateTime editTime, Mentions mentions, List reactions, List attachments, List embeds, List stickers, List components, - int flags, Message.Interaction interaction, ThreadChannel startedThread) + int flags, Message.Interaction interaction, ThreadChannel startedThread, int position) { super(content, nonce, tts); this.id = id; @@ -125,6 +126,7 @@ public ReceivedMessage( this.flags = flags; this.interaction = interaction; this.startedThread = startedThread; + this.position = position; } private void checkIntent() @@ -332,6 +334,15 @@ public Member getMember() return member; } + @Override + public int getApproximatePosition() + { + if (!getChannelType().isThread()) + throw new IllegalStateException("This message was not sent in a thread."); + + return position; + } + @Nonnull @Override public String getContentStripped() diff --git a/src/main/java/net/dv8tion/jda/internal/entities/SystemMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/SystemMessage.java index 93a6faf9c4..426a5ffdaf 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/SystemMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/SystemMessage.java @@ -40,10 +40,10 @@ public SystemMessage( boolean fromWebhook, long applicationId, boolean tts, boolean pinned, String content, String nonce, User author, Member member, MessageActivity activity, OffsetDateTime editTime, Mentions mentions, List reactions, List attachments, List embeds, - List stickers, int flags, ThreadChannel startedThread) + List stickers, int flags, ThreadChannel startedThread, int position) { super(id, channel, type, messageReference, fromWebhook, applicationId, tts, pinned, content, nonce, author, member, - activity, editTime, mentions, reactions, attachments, embeds, stickers, Collections.emptyList(), flags, null, startedThread); + activity, editTime, mentions, reactions, attachments, embeds, stickers, Collections.emptyList(), flags, null, startedThread, position); } @Nonnull