Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ApplicationInfo#getFlags #2202

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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