Skip to content

Commit

Permalink
Implement role tags (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
davfsa authored Dec 9, 2020
1 parent ac70db5 commit d7abefe
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
15 changes: 15 additions & 0 deletions hikari/guilds.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,21 @@ class Role(PartialRole):
and increase as you go up the hierarchy.
"""

bot_id: typing.Optional[snowflakes.Snowflake] = attr.ib(eq=False, hash=False, repr=True)
"""The ID of the bot this role belongs to.
If `builtins.None`, this is not a bot role.
"""

integration_id: typing.Optional[snowflakes.Snowflake] = attr.ib(eq=False, hash=False, repr=True)
"""The ID of the integration this role belongs to.
If `builtins.None`, this is not a integration role.
"""

is_premium_subscriber_role: bool = attr.ib(eq=False, hash=False, repr=True)
"""Whether this role is the guild's nitro subscriber role."""

@property
def colour(self) -> colours.Colour:
"""Alias for the `color` field."""
Expand Down
16 changes: 15 additions & 1 deletion hikari/impl/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,18 @@ def deserialize_role(
*,
guild_id: snowflakes.Snowflake,
) -> guild_models.Role:
bot_id: typing.Optional[snowflakes.Snowflake] = None
integration_id: typing.Optional[snowflakes.Snowflake] = None
is_premium_subscriber_role: bool = False
if "tags" in payload:
tags_payload = payload["tags"]
if "bot_id" in tags_payload:
bot_id = snowflakes.Snowflake(tags_payload["bot_id"])
if "integration_id" in tags_payload:
integration_id = snowflakes.Snowflake(tags_payload["integration_id"])
if "premium_subscriber" in tags_payload:
is_premium_subscriber_role = True

return guild_models.Role(
app=self._app,
id=snowflakes.Snowflake(payload["id"]),
Expand All @@ -1027,10 +1039,12 @@ def deserialize_role(
color=color_models.Color(payload["color"]),
is_hoisted=payload["hoist"],
position=int(payload["position"]),
# https://github.com/discord/discord-api-docs/pull/1843/commits/470677363ba88fbc1fe79228821146c6d6b488b9
permissions=permission_models.Permissions(int(payload["permissions"])),
is_managed=payload["managed"],
is_mentionable=payload["mentionable"],
bot_id=bot_id,
integration_id=integration_id,
is_premium_subscriber_role=is_premium_subscriber_role,
)

@staticmethod
Expand Down
15 changes: 15 additions & 0 deletions tests/hikari/impl/test_entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,11 @@ def guild_role_payload(self):
"permissions": "66321471",
"managed": False,
"mentionable": False,
"tags": {
"bot_id": "123",
"integration_id": "456",
"premium_subscriber": None,
},
}

def test_deserialize_role(self, entity_factory_impl, mock_app, guild_role_payload):
Expand All @@ -1533,8 +1538,18 @@ def test_deserialize_role(self, entity_factory_impl, mock_app, guild_role_payloa
assert guild_role.permissions == permission_models.Permissions(66_321_471)
assert guild_role.is_managed is False
assert guild_role.is_mentionable is False
assert guild_role.bot_id == 123
assert guild_role.integration_id == 456
assert guild_role.is_premium_subscriber_role is True
assert isinstance(guild_role, guild_models.Role)

def test_deserialize_role_with_missing_or_unset_fields(self, entity_factory_impl, guild_role_payload):
del guild_role_payload["tags"]
guild_role = entity_factory_impl.deserialize_role(guild_role_payload, guild_id=snowflakes.Snowflake(76534453))
assert guild_role.bot_id is None
assert guild_role.integration_id is None
assert guild_role.is_premium_subscriber_role is False

def test_deserialize_partial_integration(self, entity_factory_impl, partial_integration_payload):
partial_integration = entity_factory_impl.deserialize_partial_integration(partial_integration_payload)
assert partial_integration.id == 4949494949
Expand Down
3 changes: 3 additions & 0 deletions tests/hikari/impl/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,9 @@ async def test_edit_permission_overwrites(self, rest_client):
is_mentionable=True,
permissions=0,
position=0,
bot_id=None,
integration_id=None,
is_premium_subscriber_role=False,
),
channels.PermissionOverwriteType.ROLE,
),
Expand Down
3 changes: 3 additions & 0 deletions tests/hikari/test_guilds.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ def model(self, mock_app):
is_mentionable=True,
permissions=permissions.Permissions.CONNECT,
position=12,
bot_id=None,
integration_id=None,
is_premium_subscriber_role=False,
)

def test_colour_property(self, model):
Expand Down

0 comments on commit d7abefe

Please sign in to comment.