Skip to content

Commit

Permalink
Add ApplicationInfo#getFlags (#2202)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Aug 3, 2022
1 parent b579a8d commit c2e8c5e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
77 changes: 76 additions & 1 deletion src/main/java/net/dv8tion/jda/api/entities/ApplicationInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
* <br>Modifying the returned EnumSet will have not actually change the flags of the application.
*
* @return {@link EnumSet} of {@link Flag}
*/
@Nonnull
default EnumSet<Flag> getFlags()
{
return Flag.fromRaw(getFlagsRaw());
}

/**
* The raw bitset representing this application's flags.
*
* @return The bitset
*/
long getFlagsRaw();

/**
* Flag constants corresponding to the <a href="https://discord.com/developers/docs/resources/application#application-object-application-flags" target="_blank">Discord Enum</a>
*
* @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<Flag> fromRaw(long raw)
{
EnumSet<Flag> set = EnumSet.noneOf(Flag.class);
for (Flag flag : values())
{
if ((raw & flag.value) != 0)
set.add(flag);
}
return set;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,7 +51,7 @@ public class ApplicationInfoImpl implements ApplicationInfo
private final List<String> 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<String> tags, String customAuthUrl, long defaultAuthUrlPerms, List<String> defaultAuthUrlScopes)
{
Expand All @@ -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;
Expand Down Expand Up @@ -224,6 +226,12 @@ public long getPermissionsRaw()
return defaultAuthUrlPerms;
}

@Override
public long getFlagsRaw()
{
return flags;
}

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

Expand Down

0 comments on commit c2e8c5e

Please sign in to comment.