Skip to content

Commit

Permalink
Add application_id support for received messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Almighty-Satan committed Nov 23, 2022
1 parent 7d6775c commit b26d5e6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
22 changes: 22 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 @@ -483,6 +483,28 @@ default boolean isFromGuild()
*/
boolean isWebhookMessage();

/**
* If this message is from an application-owned {@link net.dv8tion.jda.api.entities.Webhook Webhook} or
* is a response to an {@link net.dv8tion.jda.api.interactions.Interaction Interaction}, this will return
* the application's id.
*
* @return The application's id or {@code null} if this message was not sent by an application
*/
@Nullable
default String getApplicationId()
{
return getApplicationIdLong() == 0 ? null : Long.toUnsignedString(getApplicationIdLong());
}

/**
* If this message is from an application-owned {@link net.dv8tion.jda.api.entities.Webhook Webhook} or
* is a response to an {@link net.dv8tion.jda.api.interactions.Interaction Interaction}, this will return
* the application's id.
*
* @return The application's id or 0 if this message was not sent by an application
*/
long getApplicationIdLong();

/**
* Returns the {@link net.dv8tion.jda.api.entities.channel.middleman.MessageChannel MessageChannel} that this message was sent in.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,7 @@ private ReceivedMessage createMessage0(DataObject jsonObject, @Nonnull MessageCh

final String content = jsonObject.getString("content", "");
final boolean fromWebhook = jsonObject.hasKey("webhook_id");
final long applicationId = jsonObject.getUnsignedLong("application_id", 0);
final boolean pinned = jsonObject.getBoolean("pinned");
final boolean tts = jsonObject.getBoolean("tts");
final boolean mentionsEveryone = jsonObject.getBoolean("mention_everyone");
Expand Down Expand Up @@ -1753,12 +1754,12 @@ else if (MISSING_CHANNEL.equals(ex.getMessage()))

if (!type.isSystem())
{
return new ReceivedMessage(id, channel, type, messageReference, fromWebhook, tts, pinned,
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);
}
else
{
return new SystemMessage(id, channel, type, messageReference, fromWebhook, tts, pinned,
return new SystemMessage(id, channel, type, messageReference, fromWebhook, applicationId, tts, pinned,
content, nonce, user, member, activity, editTime, mentions, reactions, attachments, embeds, stickers, flags, startedThread);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class ReceivedMessage extends AbstractMessage
protected final MessageChannel channel;
protected final MessageReference messageReference;
protected final boolean fromWebhook;
protected final long applicationId;
protected final boolean pinned;
protected final User author;
protected final Member member;
Expand All @@ -96,7 +97,7 @@ public class ReceivedMessage extends AbstractMessage

public ReceivedMessage(
long id, MessageChannel channel, MessageType type, MessageReference messageReference,
boolean fromWebhook, boolean tts, boolean pinned,
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, List<ActionRow> components,
Expand All @@ -109,6 +110,7 @@ public ReceivedMessage(
this.type = type;
this.api = (JDAImpl) channel.getJDA();
this.fromWebhook = fromWebhook;
this.applicationId = applicationId;
this.pinned = pinned;
this.author = author;
this.member = member;
Expand Down Expand Up @@ -509,6 +511,12 @@ public boolean isWebhookMessage()
return fromWebhook;
}

@Override
public long getApplicationIdLong()
{
return applicationId;
}

@Override
public boolean isTTS()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public class SystemMessage extends ReceivedMessage
{
public SystemMessage(
long id, MessageChannel channel, MessageType type, MessageReference messageReference,
boolean fromWebhook, boolean tts, boolean pinned,
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)
{
super(id, channel, type, messageReference, fromWebhook, tts, pinned, content, nonce, author, member,
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);
}

Expand Down

0 comments on commit b26d5e6

Please sign in to comment.