Skip to content

Commit 91c4262

Browse files
committed
feat: support linked roles
1 parent ac5c055 commit 91c4262

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

interactions/client/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
get_object_name,
1313
get_parameters,
1414
maybe_coroutine,
15+
nulled_boolean_get,
1516
wrap_partial,
1617
)
1718
from .serializer import (
@@ -71,6 +72,7 @@
7172
"get_object_name",
7273
"get_parameters",
7374
"maybe_coroutine",
75+
"nulled_boolean_get",
7476
"wrap_partial",
7577
"dict_filter",
7678
"dict_filter_none",

interactions/client/utils/misc_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"get_event_name",
2222
"get_object_name",
2323
"maybe_coroutine",
24+
"nulled_boolean_get",
2425
)
2526

2627
mention_reg = re.compile(r"@(everyone|here|[!&]?[0-9]{17,20})")
@@ -245,3 +246,20 @@ def disable_components(*components: "BaseComponent") -> list["BaseComponent"]:
245246
else:
246247
component.disabled = True
247248
return list(components)
249+
250+
251+
def nulled_boolean_get(data: dict[str, Any], key: str) -> bool:
252+
"""
253+
Gets a boolean value from a dictionary, but treats None as True.
254+
255+
Args:
256+
data: The dictionary to get the value from
257+
key: The key to get the value from
258+
259+
Returns:
260+
The boolean value of the key
261+
"""
262+
# discord tags are weird, when they are None they are True, when they are True they are True and when they are False they are False
263+
if key in data:
264+
return True if data[key] is None else bool(data[key])
265+
return False

interactions/models/discord/role.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import attrs
55

66
from interactions.client.const import MISSING, T, Missing
7+
from interactions.client.utils import nulled_boolean_get
78
from interactions.client.utils.attr_converters import optional as optional_c
89
from interactions.client.utils.serializer import dict_filter
910
from interactions.models.discord.asset import Asset
@@ -55,6 +56,7 @@ class Role(DiscordObject):
5556
)
5657
_bot_id: "Snowflake_Type | None" = attrs.field(repr=False, default=None)
5758
_integration_id: "Snowflake_Type | None" = attrs.field(repr=False, default=None) # todo integration object?
59+
_guild_connections: bool = attrs.field(repr=False, default=False)
5860

5961
def __lt__(self: "Role", other: "Role") -> bool:
6062
if not isinstance(self, Role) or not isinstance(other, Role):
@@ -85,13 +87,9 @@ def _process_dict(cls, data: dict[str, Any], client: "Client") -> dict[str, Any]
8587
if icon_hash := data.get("icon"):
8688
data["icon"] = Asset.from_path_hash(client, f"role-icons/{data['id']}/{{}}", icon_hash)
8789

88-
if "premium_subscriber" in data:
89-
if data["premium_subscriber"] is None:
90-
data["premium_subscriber"] = True
91-
else:
92-
data["premium_subscriber"] = bool(data["premium_subscriber"])
93-
else:
94-
data["premium_subscriber"] = False
90+
data["premium_subscriber"] = nulled_boolean_get(data, "premium_subscriber")
91+
data["guild_connections"] = nulled_boolean_get(data, "guild_connections")
92+
data["available_for_purchase"] = nulled_boolean_get(data, "available_for_purchase")
9593

9694
return data
9795

@@ -134,6 +132,11 @@ def bot_managed(self) -> bool:
134132
"""Is this role owned/managed by a bot."""
135133
return self._bot_id is not None
136134

135+
@property
136+
def is_linked_role(self) -> bool:
137+
"""Is this role a linked role."""
138+
return self._guild_connections
139+
137140
@property
138141
def mention(self) -> str:
139142
"""Returns a string that would mention the role."""

0 commit comments

Comments
 (0)