From d044d7b7815a5b98032ff45c19a25a97dd8b604a Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 27 Sep 2015 17:45:15 +0200 Subject: [PATCH] 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. --- discord/client.py | 37 +++++++++++++++++++++++++++++++++++++ discord/endpoints.py | 1 + 2 files changed, 38 insertions(+) diff --git a/discord/client.py b/discord/client.py index 67fba4ffa..c59f38098 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 6be7e01c7..7266b7751 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'