From 29d3f5a886545067a18d6d5e7f540074b86d7959 Mon Sep 17 00:00:00 2001 From: Rapptz <rapptz@gmail.com> Date: Sat, 9 Apr 2016 21:40:40 -0400 Subject: [PATCH] Allow Client.edit_profile to work with bot accounts. --- discord/client.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/discord/client.py b/discord/client.py index 4e6b2c336..b6ba71f40 100644 --- a/discord/client.py +++ b/discord/client.py @@ -445,6 +445,7 @@ class Client: def _login_1(self, token): log.info('logging in using static token') self.token = token + self.email = None self.headers['authorization'] = 'Bot {}'.format(self.token) resp = yield from self.session.get(endpoints.ME, headers=self.headers) yield from resp.release() @@ -1448,12 +1449,13 @@ class Client: yield from response.release() @asyncio.coroutine - def edit_profile(self, password, **fields): + def edit_profile(self, password=None, **fields): """|coro| Edits the current profile of the client. - All fields except ``password`` are optional. + If a bot account is used then the password field is optional, + otherwise it is required. The profile is **not** edited in place. @@ -1469,7 +1471,8 @@ class Client: Parameters ----------- password : str - The current password for the client's account. + The current password for the client's account. Not used + for bot accounts. new_password : str The new password you wish to change to. email : str @@ -1486,6 +1489,8 @@ class Client: Editing your profile failed. InvalidArgument Wrong image format passed for ``avatar``. + ClientException + Password is required for non-bot accounts. """ try: @@ -1498,26 +1503,37 @@ class Client: else: avatar = None + not_bot_account = not self.user.bot + if not_bot_account and password is None: + raise ClientException('Password is required for non-bot accounts.') + payload = { 'password': password, - 'new_password': fields.get('new_password'), - 'email': fields.get('email', self.email), 'username': fields.get('username', self.user.name), 'avatar': avatar } + if not_bot_account: + payload['email'] = fields.get('email', self.email) + + if 'new_password' in fields: + payload['new_password'] = fields['new_password'] + + r = yield from self.session.patch(endpoints.ME, data=utils.to_json(payload), headers=self.headers) log.debug(request_logging_format.format(method='PATCH', response=r)) yield from utils._verify_successful_response(r) data = yield from r.json() log.debug(request_success_log.format(response=r, json=payload, data=data)) - self.token = data['token'] - self.email = data['email'] - self.headers['authorization'] = self.token - if self.cache_auth: - self._update_cache(self.email, password) + if not_bot_account: + self.token = data['token'] + self.email = data['email'] + self.headers['authorization'] = self.token + + if self.cache_auth: + self._update_cache(self.email, password) @asyncio.coroutine def change_status(self, game=None, idle=False):