From b26d5e699de93cab5d91b5a3d10e6259d9c36e38 Mon Sep 17 00:00:00 2001 From: Almighty-Satan <43804777+Almighty-Satan@users.noreply.github.com> Date: Thu, 17 Nov 2022 21:27:23 +0100 Subject: [PATCH] Add application_id support for received messages --- .../net/dv8tion/jda/api/entities/Message.java | 22 +++++++++++++++++++ .../jda/internal/entities/EntityBuilder.java | 5 +++-- .../internal/entities/ReceivedMessage.java | 10 ++++++++- .../jda/internal/entities/SystemMessage.java | 4 ++-- 4 files changed, 36 insertions(+), 5 deletions(-) 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 1826150635..b4b8b7f700 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Message.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Message.java @@ -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. * 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 f5065c2325..d2a476cc8b 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java @@ -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"); @@ -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); } } 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 f8b3b72669..55d25f4629 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java @@ -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; @@ -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 reactions, List attachments, List embeds, List stickers, List components, @@ -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; @@ -509,6 +511,12 @@ public boolean isWebhookMessage() return fromWebhook; } + @Override + public long getApplicationIdLong() + { + return applicationId; + } + @Override public boolean isTTS() { 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 86c4691f9f..93a6faf9c4 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/SystemMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/SystemMessage.java @@ -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 reactions, List attachments, List embeds, List 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); }