aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2015-11-26 19:05:31 -0500
committerRapptz <[email protected]>2015-11-26 19:05:31 -0500
commitea80812fdd336634b8da05853564ece93975af4a (patch)
tree5f0a955b93c7fbb712296300af46c3be8a1e2ab7
parentClient.accept_invite and Client.register now accept invite IDs. (diff)
downloaddiscord.py-ea80812fdd336634b8da05853564ece93975af4a.tar.xz
discord.py-ea80812fdd336634b8da05853564ece93975af4a.zip
Add Client.get_invite to turn a URL to an Invite object.
-rw-r--r--discord/client.py23
-rw-r--r--discord/invite.py10
2 files changed, 31 insertions, 2 deletions
diff --git a/discord/client.py b/discord/client.py
index f44604d5..06e8c6f9 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -1180,6 +1180,29 @@ class Client(object):
data['channel'] = utils.find(lambda ch: ch.id == channel_id, data['server'].channels)
return Invite(**data)
+ def get_invite(self, url):
+ """Returns a :class:`Invite` object from the discord.gg invite URL or ID.
+
+ .. note::
+
+ If the server attribute of the returned invite is ``None`` then that means
+ that you have not joined the server.
+
+ """
+
+ destination = self._resolve_invite(url)
+ rurl = '{0}/invite/{1}'.format(endpoints.API_BASE, destination)
+ response = requests.get(rurl, headers=self.headers)
+ log.debug(request_logging_format.format(response=response))
+ _verify_successful_response(response)
+ data = response.json()
+ server = self.connection._get_server(data['guild']['id'])
+ data['server'] = server
+ ch_id = data['channel']['id']
+ channels = getattr(server, 'channels', [])
+ data['channel'] = utils.find(lambda c: c.id == ch_id, channels)
+ return Invite(**data)
+
def accept_invite(self, invite):
"""Accepts an :class:`Invite`, URL or ID to an invite.
diff --git a/discord/invite.py b/discord/invite.py
index 24af7ce2..21f47a83 100644
--- a/discord/invite.py
+++ b/discord/invite.py
@@ -30,6 +30,9 @@ from .utils import parse_time
class Invite(object):
"""Represents a Discord :class:`Server` or :class:`Channel` invite.
+ Depending on the way this object was created, some of the attributes can
+ have a value of ``None``.
+
Instance attributes:
.. attribute:: max_age
@@ -49,7 +52,8 @@ class Invite(object):
A datetime object denoting the time the invite was created.
.. attribute:: temporary
- A boolean indicating that the invite grants temporary membership. If True, members who joined via this invite will be kicked upon disconnect.
+ A boolean indicating that the invite grants temporary membership.
+ If True, members who joined via this invite will be kicked upon disconnect.
.. attribute:: uses
How many times the invite has been used.
@@ -77,7 +81,9 @@ class Invite(object):
self.uses = kwargs.get('uses')
self.max_uses = kwargs.get('max_uses')
self.xkcd = kwargs.get('xkcdpass')
- self.inviter = User(**kwargs.get('inviter', {}))
+
+ inviter_data = kwargs.get('inviter')
+ self.inviter = None if inviter_data is None else User(**inviter_data)
self.channel = kwargs.get('channel')
@property