From d7abefeaaad28d115e3a0bc5a96af8bdc7de71d6 Mon Sep 17 00:00:00 2001 From: davfsa Date: Wed, 9 Dec 2020 10:42:42 +0100 Subject: [PATCH] Implement role tags (#385) --- hikari/guilds.py | 15 +++++++++++++++ hikari/impl/entity_factory.py | 16 +++++++++++++++- tests/hikari/impl/test_entity_factory.py | 15 +++++++++++++++ tests/hikari/impl/test_rest.py | 3 +++ tests/hikari/test_guilds.py | 3 +++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/hikari/guilds.py b/hikari/guilds.py index d370539728..a3870912e2 100644 --- a/hikari/guilds.py +++ b/hikari/guilds.py @@ -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.""" diff --git a/hikari/impl/entity_factory.py b/hikari/impl/entity_factory.py index 14851f65d8..7f16ff2767 100644 --- a/hikari/impl/entity_factory.py +++ b/hikari/impl/entity_factory.py @@ -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"]), @@ -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 diff --git a/tests/hikari/impl/test_entity_factory.py b/tests/hikari/impl/test_entity_factory.py index a93cf6d43b..f9c14befb7 100644 --- a/tests/hikari/impl/test_entity_factory.py +++ b/tests/hikari/impl/test_entity_factory.py @@ -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): @@ -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 diff --git a/tests/hikari/impl/test_rest.py b/tests/hikari/impl/test_rest.py index 1763dcdb2c..9f092d0500 100644 --- a/tests/hikari/impl/test_rest.py +++ b/tests/hikari/impl/test_rest.py @@ -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, ), diff --git a/tests/hikari/test_guilds.py b/tests/hikari/test_guilds.py index fad763481d..db30e13ad3 100644 --- a/tests/hikari/test_guilds.py +++ b/tests/hikari/test_guilds.py @@ -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):