aboutsummaryrefslogtreecommitdiff
path: root/discord
diff options
context:
space:
mode:
authorHornwitser <[email protected]>2015-09-27 17:45:15 +0200
committerHornwitser <[email protected]>2015-10-06 19:09:16 +0200
commitd044d7b7815a5b98032ff45c19a25a97dd8b604a (patch)
tree075dd06c089a6ede896880ec4c2a4b899a13f214 /discord
parentReconnect when WebSocket dies (diff)
downloaddiscord.py-d044d7b7815a5b98032ff45c19a25a97dd8b604a.tar.xz
discord.py-d044d7b7815a5b98032ff45c19a25a97dd8b604a.zip
Add client register API
When clicking on an invite link without having a Discord account it's possible to create an unclaimed account for joining the conversation quickly. Add register() method to Client that performs and invite based registration of an unclaimed account.
Diffstat (limited to 'discord')
-rw-r--r--discord/client.py37
-rw-r--r--discord/endpoints.py1
2 files changed, 38 insertions, 0 deletions
diff --git a/discord/client.py b/discord/client.py
index 67fba4ff..c59f3809 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -653,6 +653,43 @@ class Client(object):
else:
log.error(request_logging_format.format(name='login', response=r))
+ def register(self, username, invite, fingerprint=None):
+ """Register a new unclaimed account using an invite to a server.
+
+ After this function is called, the client will be logged in to the
+ user created and :attr:`is_logged_in` returns True if no errors
+ occur.
+
+ :param str username: The username to register as.
+ :param str invite: The invite to register with.
+ :param str fingerprint: Unkown API parameter, defaults to None
+ """
+
+ payload = {
+ 'fingerprint': fingerprint,
+ 'username': username,
+ 'invite': invite
+ }
+
+ r = requests.post(endpoints.REGISTER, json=payload)
+
+ if r.status_code == 201:
+ log.info('register returned status code 200')
+ self.email = ''
+
+ body = r.json()
+ self.token = body['token']
+ self.headers['authorization'] = self.token
+
+ gateway = requests.get(endpoints.GATEWAY, headers=self.headers)
+ if gateway.status_code != 200:
+ raise GatewayNotFound()
+ self._create_websocket(gateway.json().get('url'), reconnect=False)
+ self._is_logged_in = True
+ else:
+ log.error(request_logging_format.format(name='register',
+ response=r))
+
def logout(self):
"""Logs out of Discord and closes all connections."""
response = requests.post(endpoints.LOGOUT)
diff --git a/discord/endpoints.py b/discord/endpoints.py
index 6be7e01c..7266b775 100644
--- a/discord/endpoints.py
+++ b/discord/endpoints.py
@@ -28,6 +28,7 @@ BASE = 'https://discordapp.com'
API_BASE = BASE + '/api'
GATEWAY = API_BASE + '/gateway'
USERS = API_BASE + '/users'
+REGISTER = API_BASE + '/auth/register'
LOGIN = API_BASE + '/auth/login'
LOGOUT = API_BASE + '/auth/logout'
SERVERS = API_BASE + '/guilds'