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 support for Role Tags #1342

Closed
wants to merge 7 commits into from
Closed
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
59 changes: 59 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.dv8tion.jda.api.managers.RoleManager;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import net.dv8tion.jda.api.requests.restaction.RoleAction;
import net.dv8tion.jda.api.utils.data.DataObject;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -77,7 +78,10 @@ public interface Role extends IMentionable, IPermissionHolder, Comparable<Role>

/**
* Whether this {@link net.dv8tion.jda.api.entities.Role Role} is managed by an integration
* This is any role where the "This role is managed by an integration..." appears in the role list.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This is any role where the "This role is managed by an integration..." appears in the role list.
* <br>This is any role where the "This role is managed by an integration..." appears in the role list.

* Integration can be: Bot role, Booster role, or a connected partner account role.
*
* @see Role#getRoleType()
* @return True, if this {@link net.dv8tion.jda.api.entities.Role Role} is managed.
Comment on lines +84 to 85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @see Role#getRoleType()
* @return True, if this {@link net.dv8tion.jda.api.entities.Role Role} is managed.
* @return True, if this {@link net.dv8tion.jda.api.entities.Role Role} is managed.
*
* @see Role#getRoleType()

*/
boolean isManaged();
Expand All @@ -97,6 +101,14 @@ public interface Role extends IMentionable, IPermissionHolder, Comparable<Role>
*/
boolean isMentionable();

/**
* Return the type of role this is
* Typically used for finding what kind of managed/integration role this is.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Typically used for finding what kind of managed/integration role this is.
* typically used to determine what kind of managed role this is.

*
* @return this role's type
*/
RoleType getRoleType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RoleType getRoleType();
@Nonnull
RoleType getRoleType();


/**
* The {@code long} representation of the literal permissions that this {@link net.dv8tion.jda.api.entities.Role Role} has.
* <br><b>NOTE:</b> these do not necessarily represent the permissions this role will have in a {@link GuildChannel GuildChannel}.
Expand Down Expand Up @@ -271,4 +283,51 @@ default RoleAction createCopy()
*/
@Nonnull
JDA getJDA();


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

/**
* Enum used to differentiate between the different types of Guild Roles.
*/
enum RoleType
{
/**
* A basic, user-created role.
*/
NORMAL,
/**
* A role created from a bot on join.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* A role created from a bot on join.
* A role created for a bot on join.

*/
BOT,
/**
* A role created from a YouTube Member or Twitch Subscriber integration..\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* A role created from a YouTube Member or Twitch Subscriber integration..\
* A role created for an integration. For example, twitch subscribers

*/
INTEGRATION,
/**
* The Server Booster role.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The Server Booster role.
* The Guild Booster role.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The role is explicitly called "Server Booster" when it's created. So this should be "The Guild's Server Booster" role

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In API terminology were always use the term "guild".

*/
BOOSTER,
/**
* Unknown RoleType.
*/
UNKNOWN;

public static RoleType fromTags(DataObject tags) {
if(tags.hasKey("premium_subscriber"))
{
return RoleType.BOOSTER;
}
else if(tags.hasKey("bot_id"))
{
return RoleType.BOT;
}
else if(tags.hasKey("integration_id"))
{
return RoleType.INTEGRATION;
} else
{
return RoleType.UNKNOWN;
}
}
Comment on lines +314 to +330
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static RoleType fromTags(DataObject tags) {
if(tags.hasKey("premium_subscriber"))
{
return RoleType.BOOSTER;
}
else if(tags.hasKey("bot_id"))
{
return RoleType.BOT;
}
else if(tags.hasKey("integration_id"))
{
return RoleType.INTEGRATION;
} else
{
return RoleType.UNKNOWN;
}
}
public static RoleType fromTags(DataObject tags)
{
if (tags.hasKey("premium_subscriber"))
return RoleType.BOOSTER;
else if (tags.hasKey("bot_id"))
return RoleType.BOT;
else if (tags.hasKey("integration_id"))
return RoleType.INTEGRATION;
else
return RoleType.UNKNOWN;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,14 @@ public Role createRole(GuildImpl guild, DataObject roleJson, long guildId)
.setHoisted(roleJson.getBoolean("hoist"))
.setColor(color == 0 ? Role.DEFAULT_COLOR_RAW : color)
.setMentionable(roleJson.getBoolean("mentionable"));
if(roleJson.hasKey("tags"))
{
role.setType(Role.RoleType.fromTags(roleJson.getObject("tags")));
}
else
{
role.setType(Role.RoleType.NORMAL);
}
Comment on lines +1062 to +1069
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good way to handle tags. You should be storing the tags in a map so that you can also retrieve the associated values. For example, which bot a role belongs to.

Suggested change
if(roleJson.hasKey("tags"))
{
role.setType(Role.RoleType.fromTags(roleJson.getObject("tags")));
}
else
{
role.setType(Role.RoleType.NORMAL);
}
if (roleJson.hasKey("tags"))
role.setType(Role.RoleType.fromTags(roleJson.getObject("tags")));
else
role.setType(Role.RoleType.NORMAL);

if (playbackCache)
getJDA().getEventCache().playbackCache(EventCache.Type.ROLE, id);
return role;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/entities/RoleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class RoleImpl implements Role
private boolean managed;
private boolean hoisted;
private boolean mentionable;
private RoleType roleType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bad way to handle tags.

private long rawPermissions;
private int color;
private int rawPosition;
Expand Down Expand Up @@ -116,6 +117,12 @@ public boolean isMentionable()
return mentionable;
}

@Override
public RoleType getRoleType()
{
return roleType;
}

@Override
public long getPermissionsRaw()
{
Expand Down Expand Up @@ -368,6 +375,12 @@ public RoleImpl setMentionable(boolean mentionable)
return this;
}

public RoleImpl setType(RoleType roleType)
{
this.roleType = roleType;
return this;
}

public RoleImpl setRawPermissions(long rawPermissions)
{
this.rawPermissions = rawPermissions;
Expand Down