Skip to content

Commit 364aa69

Browse files
AstreaTSSi0bs
andauthored
fix/feat: adjust invite obj to respect event variants (#1500)
* fix/feat: adjust invite obj to respect event variants * docs: update docs for event object * revert: keep MISSING for backwards compatibility * style: consistency Co-authored-by: Sophia <41456914+i0bs@users.noreply.github.com> Signed-off-by: Astrea <25420078+AstreaTSS@users.noreply.github.com> --------- Signed-off-by: Astrea <25420078+AstreaTSS@users.noreply.github.com> Co-authored-by: Sophia <41456914+i0bs@users.noreply.github.com>
1 parent b33ab89 commit 364aa69

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

interactions/models/discord/invite.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
if TYPE_CHECKING:
1717
from interactions.client import Client
18-
from interactions.models import TYPE_GUILD_CHANNEL
18+
from interactions.models import TYPE_GUILD_CHANNEL, Guild
1919
from interactions.models.discord.user import User
2020
from interactions.models.discord.snowflake import Snowflake_Type
2121

@@ -25,54 +25,60 @@
2525
@attrs.define(eq=False, order=False, hash=False, kw_only=True)
2626
class Invite(ClientObject):
2727
code: str = attrs.field(repr=True)
28-
"""the invite code (unique ID)"""
28+
"""The invite code (unique ID)"""
2929

3030
# metadata
3131
uses: int = attrs.field(default=0, repr=True)
32-
"""the guild this invite is for"""
32+
"""How many times this invite has been used"""
3333
max_uses: int = attrs.field(repr=False, default=0)
34-
"""max number of times this invite can be used"""
34+
"""Max number of times this invite can be used"""
3535
max_age: int = attrs.field(repr=False, default=0)
36-
"""duration (in seconds) after which the invite expires"""
36+
"""Duration (in seconds) after which the invite expires"""
3737
created_at: Timestamp = attrs.field(default=MISSING, converter=optional_c(timestamp_converter), repr=True)
38-
"""when this invite was created"""
38+
"""When this invite was created"""
3939
temporary: bool = attrs.field(default=False, repr=True)
40-
"""whether this invite only grants temporary membership"""
40+
"""Whether this invite only grants temporary membership"""
4141

4242
# target data
4343
target_type: Optional[Union[InviteTargetType, int]] = attrs.field(
4444
default=None, converter=optional_c(InviteTargetType), repr=True
4545
)
46-
"""the type of target for this voice channel invite"""
46+
"""The type of target for this voice channel invite"""
4747
approximate_presence_count: Optional[int] = attrs.field(repr=False, default=MISSING)
48-
"""approximate count of online members, returned from the `GET /invites/<code>` endpoint when `with_counts` is `True`"""
48+
"""Approximate count of online members, returned when fetching invites with `with_counts` set as `True`"""
4949
approximate_member_count: Optional[int] = attrs.field(repr=False, default=MISSING)
50-
"""approximate count of total members, returned from the `GET /invites/<code>` endpoint when `with_counts` is `True`"""
50+
"""Approximate count of total members, returned when fetching invites with `with_counts` set as `True`"""
5151
scheduled_event: Optional["Snowflake_Type"] = attrs.field(
5252
default=None, converter=optional_c(to_snowflake), repr=True
5353
)
54-
"""guild scheduled event data, only included if `guild_scheduled_event_id` contains a valid guild scheduled event id"""
54+
"""Guild scheduled event data, only included if `guild_scheduled_event_id` contains a valid guild scheduled event id"""
5555
expires_at: Optional[Timestamp] = attrs.field(default=None, converter=optional_c(timestamp_converter), repr=True)
56-
"""the expiration date of this invite, returned from the `GET /invites/<code>` endpoint when `with_expiration` is `True`"""
56+
"""The expiration date of this invite, returned when fetching invites with `with_expiration` set as `True`"""
5757
stage_instance: Optional[StageInstance] = attrs.field(repr=False, default=None)
58-
"""stage instance data if there is a public Stage instance in the Stage channel this invite is for (deprecated)"""
58+
"""Stage instance data if there is a public Stage instance in the Stage channel this invite is for (deprecated)"""
5959
target_application: Optional[dict] = attrs.field(repr=False, default=None)
60-
"""the embedded application to open for this voice channel embedded application invite"""
60+
"""The embedded application to open for this voice channel embedded application invite"""
6161
guild_preview: Optional[GuildPreview] = attrs.field(repr=False, default=MISSING)
62-
"""the guild this invite is for"""
62+
"""The guild this invite is for - not given in invite events"""
6363

6464
# internal for props
6565
_channel_id: "Snowflake_Type" = attrs.field(converter=to_snowflake, repr=True)
66+
_guild_id: Optional["Snowflake_Type"] = attrs.field(default=None, converter=optional_c(to_snowflake), repr=True)
6667
_inviter_id: Optional["Snowflake_Type"] = attrs.field(default=None, converter=optional_c(to_snowflake), repr=True)
6768
_target_user_id: Optional["Snowflake_Type"] = attrs.field(
6869
repr=False, default=None, converter=optional_c(to_snowflake)
6970
)
7071

7172
@property
72-
def channel(self) -> "TYPE_GUILD_CHANNEL":
73-
"""The channel the invite is for."""
73+
def channel(self) -> Optional["TYPE_GUILD_CHANNEL"]:
74+
"""The cached channel the invite is for."""
7475
return self._client.cache.get_channel(self._channel_id)
7576

77+
@property
78+
def guild(self) -> Optional["Guild"]:
79+
"""The cached guild the invite is."""
80+
return self._client.cache.get_guild(self._guild_id) if self._guild_id else None
81+
7682
@property
7783
def inviter(self) -> Optional["User"]:
7884
"""The user that created the invite or None."""
@@ -95,16 +101,23 @@ def _process_dict(cls, data: Dict[str, Any], client: "Client") -> Dict[str, Any]
95101
data["scheduled_event"] = data["target_event_id"]
96102

97103
if channel := data.pop("channel", None):
98-
# invite metadata does not contain enough info to create a channel object
104+
client.cache.place_channel_data(channel)
99105
data["channel_id"] = channel["id"]
100106

101107
if guild := data.pop("guild", None):
102108
data["guild_preview"] = GuildPreview.from_dict(guild, client)
109+
data["guild_id"] = guild["id"]
110+
elif guild_id := data.pop("guild_id", None):
111+
data["guild_id"] = guild_id
103112

104113
if inviter := data.pop("inviter", None):
105114
inviter = client.cache.place_user_data(inviter)
106115
data["inviter_id"] = inviter.id
107116

117+
if target_user := data.pop("target_user", None):
118+
target_user = client.cache.place_user_data(target_user)
119+
data["target_user_id"] = target_user.id
120+
108121
return data
109122

110123
def __str__(self) -> str:

0 commit comments

Comments
 (0)