From c2e8c5e2bc722b4f15b05ed9e571fd1daf9a05fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Wed, 3 Aug 2022 13:58:16 +0200 Subject: [PATCH] Add ApplicationInfo#getFlags (#2202) --- .../jda/api/entities/ApplicationInfo.java | 77 ++++++++++++++++++- .../entities/ApplicationInfoImpl.java | 10 ++- .../jda/internal/entities/EntityBuilder.java | 5 +- 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/entities/ApplicationInfo.java b/src/main/java/net/dv8tion/jda/api/entities/ApplicationInfo.java index 5cec302e7c..2b03ba1c30 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/ApplicationInfo.java +++ b/src/main/java/net/dv8tion/jda/api/entities/ApplicationInfo.java @@ -23,7 +23,10 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.EnumSet; +import java.util.List; /** * Represents a Discord Application from its bot's point of view. @@ -348,4 +351,76 @@ default String getInviteUrl(long guildId, @Nullable Permission... permissions) * @return Never-negative long containing offset permissions the default authorization URL is set up with. */ long getPermissionsRaw(); + + /** + * The {@link Flag Flags} set for the application. + *
Modifying the returned EnumSet will have not actually change the flags of the application. + * + * @return {@link EnumSet} of {@link Flag} + */ + @Nonnull + default EnumSet getFlags() + { + return Flag.fromRaw(getFlagsRaw()); + } + + /** + * The raw bitset representing this application's flags. + * + * @return The bitset + */ + long getFlagsRaw(); + + /** + * Flag constants corresponding to the Discord Enum + * + * @see #getFlags() + */ + enum Flag + { + /** Bot can use {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_PRESENCES GatewayIntent.GUILD_PRESENCES} in 100 or more guilds */ + GATEWAY_PRESENCE(1 << 12), + /** Bot can use {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_PRESENCES GatewayIntent.GUILD_PRESENCES} in under 100 guilds */ + GATEWAY_PRESENCE_LIMITED(1 << 13), + /** Bot can use {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_MEMBERS GatewayIntent.GUILD_MEMBERS} in 100 or more guilds */ + GATEWAY_GUILD_MEMBERS(1 << 14), + /** Bot can use {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_MEMBERS GatewayIntent.GUILD_MEMBERS} in under 100 guilds */ + GATEWAY_GUILD_MEMBERS_LIMITED(1 << 15), + /** Indicates unusual growth of an app that prevents verification */ + VERIFICATION_PENDING_GUILD_LIMIT(1 << 16), + /** Indicates if an app is embedded within the Discord client (currently unavailable publicly) */ + EMBEDDED(1 << 17), + /** Bot can use {@link net.dv8tion.jda.api.requests.GatewayIntent#MESSAGE_CONTENT GatewayIntent.MESSAGE_CONTENT} in 100 or more guilds */ + GATEWAY_MESSAGE_CONTENT(1 << 18), + /** Bot can use {@link net.dv8tion.jda.api.requests.GatewayIntent#MESSAGE_CONTENT GatewayIntent.MESSAGE_CONTENT} in under 100 guilds */ + GATEWAY_MESSAGE_CONTENT_LIMITED(1 << 19), + ; + + private final long value; + + Flag(long value) + { + this.value = value; + } + + /** + * Converts the provided bitset to the corresponding enum constants. + * + * @param raw + * The bitset of flags + * + * @return {@link EnumSet} of {@link Flag} + */ + @Nonnull + public static EnumSet fromRaw(long raw) + { + EnumSet set = EnumSet.noneOf(Flag.class); + for (Flag flag : values()) + { + if ((raw & flag.value) != 0) + set.add(flag); + } + return set; + } + } } diff --git a/src/main/java/net/dv8tion/jda/internal/entities/ApplicationInfoImpl.java b/src/main/java/net/dv8tion/jda/internal/entities/ApplicationInfoImpl.java index 3bc78f4dc7..196644c38a 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ApplicationInfoImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ApplicationInfoImpl.java @@ -37,6 +37,7 @@ public class ApplicationInfoImpl implements ApplicationInfo private final boolean doesBotRequireCodeGrant; private final boolean isBotPublic; private final long id; + private final long flags; private final String iconId; private final String description; private final String termsOfServiceUrl; @@ -50,7 +51,7 @@ public class ApplicationInfoImpl implements ApplicationInfo private final List defaultAuthUrlScopes; private String scopes = "bot"; - public ApplicationInfoImpl(JDA api, String description, boolean doesBotRequireCodeGrant, String iconId, long id, + public ApplicationInfoImpl(JDA api, String description, boolean doesBotRequireCodeGrant, String iconId, long id, long flags, boolean isBotPublic, String name, String termsOfServiceUrl, String privacyPolicyUrl, User owner, ApplicationTeam team, List tags, String customAuthUrl, long defaultAuthUrlPerms, List defaultAuthUrlScopes) { @@ -59,6 +60,7 @@ public ApplicationInfoImpl(JDA api, String description, boolean doesBotRequireCo this.doesBotRequireCodeGrant = doesBotRequireCodeGrant; this.iconId = iconId; this.id = id; + this.flags = flags; this.isBotPublic = isBotPublic; this.name = name; this.termsOfServiceUrl = termsOfServiceUrl; @@ -224,6 +226,12 @@ public long getPermissionsRaw() return defaultAuthUrlPerms; } + @Override + public long getFlagsRaw() + { + return flags; + } + @Nonnull @Override public List getScopes() 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 c0569e96b6..cb5c29f452 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java @@ -2136,7 +2136,8 @@ public ApplicationInfo createApplicationInfo(DataObject object) final String privacyPolicyUrl = object.getString("privacy_policy_url", null); final boolean doesBotRequireCodeGrant = object.getBoolean("bot_require_code_grant"); final String iconId = object.getString("icon", null); - final long id = object.getLong("id"); + final long id = object.getUnsignedLong("id"); + final long flags = object.getUnsignedLong("flags", 0); final String name = object.getString("name"); final boolean isBotPublic = object.getBoolean("bot_public"); final User owner = createUser(object.getObject("owner")); @@ -2156,7 +2157,7 @@ public ApplicationInfo createApplicationInfo(DataObject object) .collect(Collectors.toList())) .orElse(Collections.emptyList()); - return new ApplicationInfoImpl(getJDA(), description, doesBotRequireCodeGrant, iconId, id, isBotPublic, name, + return new ApplicationInfoImpl(getJDA(), description, doesBotRequireCodeGrant, iconId, id, flags, isBotPublic, name, termsOfServiceUrl, privacyPolicyUrl, owner, team, tags, customAuthUrl, defaultAuthUrlPerms, defaultAuthUrlScopes); }