aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2020-01-20 07:06:12 -0500
committerRapptz <[email protected]>2020-01-20 07:14:27 -0500
commit6ed0ae7d965f8179e5c3f2a310b297511ebd532c (patch)
tree691cd7e583262797b2e007b9d001db2cd5dd67f2
parent[commands] Allow @ prefixed usernames in DM contexts for UserConverter (diff)
downloaddiscord.py-6ed0ae7d965f8179e5c3f2a310b297511ebd532c.tar.xz
discord.py-6ed0ae7d965f8179e5c3f2a310b297511ebd532c.zip
Fix fetching invites for a GroupChannel.
Closes #2394 Fixes #2383
-rw-r--r--discord/invite.py35
1 files changed, 23 insertions, 12 deletions
diff --git a/discord/invite.py b/discord/invite.py
index 6e65cf23..51244d0e 100644
--- a/discord/invite.py
+++ b/discord/invite.py
@@ -229,8 +229,8 @@ class Invite(Hashable):
How long the before the invite expires in seconds. A value of 0 indicates that it doesn't expire.
code: :class:`str`
The URL fragment used for the invite.
- guild: Union[:class:`Guild`, :class:`Object`, :class:`PartialInviteGuild`]
- The guild the invite is for.
+ guild: Optional[Union[:class:`Guild`, :class:`Object`, :class:`PartialInviteGuild`]]
+ The guild the invite is for. Can be ``None`` if it's from a group direct message.
revoked: :class:`bool`
Indicates if the invite has been revoked.
created_at: :class:`datetime.datetime`
@@ -278,17 +278,28 @@ class Invite(Hashable):
@classmethod
def from_incomplete(cls, *, state, data):
- guild_id = int(data['guild']['id'])
- channel_id = int(data['channel']['id'])
- guild = state._get_guild(guild_id)
- if guild is not None:
- channel = guild.get_channel(channel_id)
+ try:
+ guild_id = int(data['guild']['id'])
+ except KeyError:
+ # If we're here, then this is a group DM
+ guild = None
else:
- channel_data = data['channel']
- guild_data = data['guild']
- channel_type = try_enum(ChannelType, channel_data['type'])
- channel = PartialInviteChannel(id=channel_id, name=channel_data['name'], type=channel_type)
- guild = PartialInviteGuild(state, guild_data, guild_id)
+ guild = state._get_guild(guild_id)
+ if guild is None:
+ # If it's not cached, then it has to be a partial guild
+ guild_data = data['guild']
+ guild = PartialInviteGuild(state, guild_data, guild_id)
+
+ # As far as I know, invites always need a channel
+ # So this should never raise.
+ channel_data = data['channel']
+ channel_id = int(channel_data['id'])
+ channel_type = try_enum(ChannelType, channel_data['type'])
+ channel = PartialInviteChannel(id=channel_id, name=channel_data['name'], type=channel_type)
+ if guild is not None:
+ # Upgrade the partial data if applicable
+ channel = guild.get_channel(channel_id) or channel
+
data['guild'] = guild
data['channel'] = channel
return cls(state=state, data=data)