Skip to content

Commit

Permalink
Add thread message positions (#2366)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivia <[email protected]>
  • Loading branch information
freya022 and Chew authored Feb 17, 2023
1 parent 8852b5e commit 92e9f5f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
19 changes: 19 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 @@ -360,6 +360,25 @@ default Message getReferencedMessage()
@Nullable
Member getMember();

/**
* Returns the approximate position of this message in a {@link ThreadChannel}.
* <br>This can be used to estimate the relative position of a message in a thread, by comparing against {@link ThreadChannel#getTotalMessageCount()}.
*
* <p><b>Notes:</b>
* <ul>
* <li>The position might contain gaps or duplicates.</li>
* <li>The position is not set on messages sent earlier than July 19th, 2022, and will return -1.</li>
* </ul>
*
* @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 <a href="https://discord.com/developers/docs/resources/channel#message-object" target="_blank">Discord docs: <code>position</code> property on the message object</a>
*/
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ public Member getMember()
return null;
}

@Override
public int getApproximatePosition()
{
unsupported();
return 0;
}

@Nonnull
@Override
public String getJumpUrl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -101,7 +102,7 @@ public ReceivedMessage(
String content, String nonce, User author, Member member, MessageActivity activity, OffsetDateTime editTime,
Mentions mentions, List<MessageReaction> reactions, List<Attachment> attachments, List<MessageEmbed> embeds,
List<StickerItem> stickers, List<ActionRow> components,
int flags, Message.Interaction interaction, ThreadChannel startedThread)
int flags, Message.Interaction interaction, ThreadChannel startedThread, int position)
{
super(content, nonce, tts);
this.id = id;
Expand All @@ -125,6 +126,7 @@ public ReceivedMessage(
this.flags = flags;
this.interaction = interaction;
this.startedThread = startedThread;
this.position = position;
}

private void checkIntent()
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageReaction> reactions, List<Attachment> attachments, List<MessageEmbed> embeds,
List<StickerItem> stickers, int flags, ThreadChannel startedThread)
List<StickerItem> 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
Expand Down

0 comments on commit 92e9f5f

Please sign in to comment.